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
This commit is contained in:
dandds 2019-11-21 15:34:35 -05:00
parent 3f146c7da8
commit cd3cb7b614
2 changed files with 10 additions and 8 deletions

0
script/__init__.py Normal file
View File

View File

@ -6,7 +6,9 @@ parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(parent_dir) sys.path.append(parent_dir)
import sqlalchemy import sqlalchemy
from alembic import config as alembic_config
from seed_roles import seed_roles
from atst.database import db from atst.database import db
from atst.app import make_config, make_app from atst.app import make_config, make_app
@ -17,16 +19,16 @@ def reset_database():
meta = sqlalchemy.MetaData(bind=conn, reflect=True) meta = sqlalchemy.MetaData(bind=conn, reflect=True)
trans = conn.begin() trans = conn.begin()
retained_tables = ["alembic_version", "permission_sets"] # drop all tables
meta.drop_all()
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))
trans.commit() trans.commit()
# rerun the migrations
alembic_config.main(argv=["upgrade", "head"])
# seed the permission sets
seed_roles()
if __name__ == "__main__": if __name__ == "__main__":
config = make_config({"DISABLE_CRL_CHECK": True, "DEBUG": False}) config = make_config({"DISABLE_CRL_CHECK": True, "DEBUG": False})