From cd3cb7b61436d23dee1346ca926e98a177b59921 Mon Sep 17 00:00:00 2001 From: dandds Date: Thu, 21 Nov 2019 15:34:35 -0500 Subject: [PATCH] Adjust script to drop all tables and reset from scratch. This updates the script for resetting the database so that it drops and recreates all the tables, instead of disabling Postgres triggers and truncating most of the tables. The latter strategy requires superuser permissions in Postgres that the db user we manage in Azure does not have. The script now: - drops the tables - reruns the alembic migrations - reseeds the permission sets --- script/__init__.py | 0 script/reset_database.py | 18 ++++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 script/__init__.py diff --git a/script/__init__.py b/script/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/script/reset_database.py b/script/reset_database.py index 2a53c132..cfa63298 100644 --- a/script/reset_database.py +++ b/script/reset_database.py @@ -6,7 +6,9 @@ parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) sys.path.append(parent_dir) import sqlalchemy +from alembic import config as alembic_config +from seed_roles import seed_roles from atst.database import db from atst.app import make_config, make_app @@ -17,16 +19,16 @@ def reset_database(): meta = sqlalchemy.MetaData(bind=conn, reflect=True) trans = conn.begin() - retained_tables = ["alembic_version", "permission_sets"] - - for t in meta.sorted_tables: - if str(t) not in retained_tables: - conn.execute("ALTER TABLE {} DISABLE trigger ALL;".format(t)) - conn.execute(t.delete()) - conn.execute("ALTER TABLE {} ENABLE trigger ALL;".format(t)) - + # drop all tables + meta.drop_all() trans.commit() + # rerun the migrations + alembic_config.main(argv=["upgrade", "head"]) + + # seed the permission sets + seed_roles() + if __name__ == "__main__": config = make_config({"DISABLE_CRL_CHECK": True, "DEBUG": False})