From 35b2635b6228bf110557ba05ecb09f613674c714 Mon Sep 17 00:00:00 2001 From: Thomas Hintz Date: Sat, 21 Feb 2026 10:55:42 -0800 Subject: [PATCH] Allow cleaning the db. --- src/Dockerfile | 1 + src/Makefile | 3 +++ src/db-clean.sql | 22 ++++++++++++++++++++++ src/db.scm | 29 ++++++++++++++++++++++++++--- src/run.scm | 6 +++++- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/db-clean.sql diff --git a/src/Dockerfile b/src/Dockerfile index fbbda73..5573a35 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -65,6 +65,7 @@ COPY --from=buildeggs /var/nassella/nassella-run /var COPY nassella-latest.tar nassella-latest.tar COPY root-key root-key COPY db-init.sql db-init.sql +COPY db-clean.sql db-clean.sql # ENTRYPOINT ["ls"] # CMD ["/usr/local/lib/chicken/11"] diff --git a/src/Makefile b/src/Makefile index 77e924c..03054aa 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,3 +9,6 @@ dockerpush: local: docker run -p 8080:8080 --net=host --rm nassella/b0.0.1 + +localclean: + docker run -p 8080:8080 --net=host --rm nassella/b0.0.1 --clean diff --git a/src/db-clean.sql b/src/db-clean.sql new file mode 100644 index 0000000..667c38d --- /dev/null +++ b/src/db-clean.sql @@ -0,0 +1,22 @@ +drop index user_terraform_state_user_id_instance_id_idx; +drop table user_terraform_state; + +drop index deployments_user_id_instance_id_idx; +drop table deployments; + +drop type deployment_status; + +drop index user_app_configs_user_id_instance_id_idx; +drop table user_app_configs; + +drop index user_selected_apps_user_id_instance_id_idx; +drop table user_selected_apps; + +drop index user_service_configs_user_id_instance_id_idx; +drop table user_service_configs; + +drop index instances_user_id_instance_id_idx; +drop table instances; + +drop index users_auth_user_id_idx; +drop table users; diff --git a/src/db.scm b/src/db.scm index 4a35324..c2ad084 100644 --- a/src/db.scm +++ b/src/db.scm @@ -3,7 +3,8 @@ connection-spec ;;functions - with-db with-db/transaction db-init + with-db with-db/transaction + db-init db-clean create-user delete-user create-instance get-user-instances @@ -527,18 +528,40 @@ returning users.user_id;" "" (user-decrypt-from-db (alist-ref 'state_backup_enc res) user-key user-iv user-id))))))) +(debug-log (current-error-port)) + (define (db-init) (with-db/transaction (lambda (db) (if (value-at (query db "SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'users');")) - #t + (begin + (log-to (debug-log) "database already initialized") + #t) (begin (log-to (debug-log) "tables not found in db. Creating...") (for-each (lambda (statement) (query db (conc statement ";"))) (string-split (with-input-from-file "db-init.sql" read-string) ";")) - (log-to (debug-log) "table completion complete")))))) + (log-to (debug-log) "table creation finished") + (log-to (debug-log) "creating test user") + (create-user db 1 "me@example.com" "username") + (log-to (debug-log) "test user creation finished")))))) + +(define (db-clean) + (with-db/transaction + (lambda (db) + (if (value-at (query db "SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'users');")) + (begin + (log-to (debug-log) "cleaning database") + (for-each + (lambda (statement) + (query db (conc statement ";"))) + (string-split (with-input-from-file "db-clean.sql" read-string) ";")) + (log-to (debug-log) "database cleaning complete")) + (begin + (log-to (debug-log) "tables not found, not cleaning") + #t))))) ;; (with-db/transaction (lambda (db) (get-user-deployments db 1))) ;; (with-db/transaction (lambda (db) (get-most-recent-deployment-progress db 7))) diff --git a/src/run.scm b/src/run.scm index ee1db0c..8b33b6f 100644 --- a/src/run.scm +++ b/src/run.scm @@ -1,5 +1,7 @@ (include "nassella") -(import spiffy schematra) +(import (chicken process-context) + spiffy + schematra) (debug-log (current-error-port)) @@ -8,6 +10,8 @@ (lambda () (log-to (debug-log) "starting server") (log-to (debug-log) "initializing db") + (if (member "--clean" (command-line-arguments) equal?) + (db-clean)) (db-init) (log-to (debug-log) "db initialization complete") (start-server)))