Allow cleaning the db.
This commit is contained in:
@@ -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"]
|
||||
|
||||
@@ -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
|
||||
|
||||
22
src/db-clean.sql
Normal file
22
src/db-clean.sql
Normal file
@@ -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;
|
||||
29
src/db.scm
29
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)))
|
||||
|
||||
@@ -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)))
|
||||
|
||||
Reference in New Issue
Block a user