Allow cleaning the db.

This commit is contained in:
2026-02-21 10:55:42 -08:00
parent 9d5b8b9f6c
commit 35b2635b62
5 changed files with 57 additions and 4 deletions

View File

@@ -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)))