atst/script/reset_database.py
dandds 7e468f8ac6 Rename script for resetting the database.
Renamed this script because it's current name is misleading. It does not
just remove sample data; it truncates every table except the alembic
version table and `permission_sets`.
2019-11-18 10:17:44 -05:00

36 lines
922 B
Python

# Add root application dir to the python path
import os
import sys
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(parent_dir)
import sqlalchemy
from atst.database import db
from atst.app import make_config, make_app
def reset_database():
conn = db.engine.connect()
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))
trans.commit()
if __name__ == "__main__":
config = make_config({"DISABLE_CRL_CHECK": True, "DEBUG": False})
app = make_app(config)
with app.app_context():
reset_database()