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`.
This commit is contained in:
35
script/reset_database.py
Normal file
35
script/reset_database.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# 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()
|
Reference in New Issue
Block a user