From 019ec08d65e20416721f81feb24ade885d7c27ff Mon Sep 17 00:00:00 2001 From: Rick Anderson Date: Mon, 2 Dec 2024 19:17:39 -0800 Subject: [PATCH] Master script for automation to deploy an empty DB, schema, and seeing data --- database/utility/STR_DSS_Database_Create.sql | 39 ++++++++++++++++++++ database/utility/STR_DSS_Master.bat | 8 ++++ 2 files changed, 47 insertions(+) create mode 100644 database/utility/STR_DSS_Database_Create.sql create mode 100644 database/utility/STR_DSS_Master.bat diff --git a/database/utility/STR_DSS_Database_Create.sql b/database/utility/STR_DSS_Database_Create.sql new file mode 100644 index 00000000..6b4fe933 --- /dev/null +++ b/database/utility/STR_DSS_Database_Create.sql @@ -0,0 +1,39 @@ +-- Check if DB exists and drop if it does +DO +$$ +DECLARE + db_name TEXT := 'strdssdev'; +BEGIN + -- Check if the database exists + IF EXISTS ( + SELECT 1 FROM pg_database + WHERE datname = db_name + ) THEN + -- Terminate active connections to the database + PERFORM pg_terminate_backend(pid) + FROM pg_stat_activity + WHERE datname = db_name; + END IF; +END; +$$; + +DROP DATABASE IF EXISTS strdssdev; + +-- Check if strdss role exists and drop if it does +DO +$$ +BEGIN + IF EXISTS ( + SELECT 1 FROM pg_roles + WHERE rolname = 'strdssdev' + ) THEN + EXECUTE format('DROP ROLE %I', 'strdssdev'); + END IF; +END +$$; + +CREATE ROLE strdssdev WITH LOGIN PASSWORD 'postgres'; +CREATE DATABASE strdssdev; +ALTER DATABASE strdssdev OWNER TO strdssdev; +\c strdssdev +CREATE EXTENSION postgis; \ No newline at end of file diff --git a/database/utility/STR_DSS_Master.bat b/database/utility/STR_DSS_Master.bat new file mode 100644 index 00000000..4e916f8b --- /dev/null +++ b/database/utility/STR_DSS_Master.bat @@ -0,0 +1,8 @@ +rem This script must be run from the House-Policy-STRDSS/database/utility folder +rem by replacing 'rem' with '#', this file will run with /bin/sh under Linux +docker cp ../database/utility house-policy-strdss-strdss-db-1:/utility +docker cp ../database/ddl house-policy-strdss-strdss-db-1:/ddl +docker cp ../database/seeding house-policy-strdss-strdss-db-1:/seeding +docker exec -i house-policy-strdss-strdss-db-1 psql -U postgres -f utility/STR_DSS_Database_Create.sql +rem The SQL script below must be the correct sprint migration script for the current sprint (i.e. STR_DSS_Migration_Sprint_.sql) +docker exec -i house-policy-strdss-strdss-db-1 psql -U postgres -d strdssdev -f utility/STR_DSS_Migration_Sprint_18.sql