-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_db.py
More file actions
35 lines (30 loc) · 1.27 KB
/
reset_db.py
File metadata and controls
35 lines (30 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Script to reset the database and alembic version table.
Use this when you need to start fresh with migrations.
"""
from app.db.session import engine
from sqlalchemy import text
def reset_database():
"""Drop all tables and alembic_version to start fresh"""
with engine.connect() as conn:
# Start a transaction
trans = conn.begin()
try:
print("Dropping alembic_version table...")
conn.execute(text("DROP TABLE IF EXISTS alembic_version CASCADE"))
print("Dropping application tables...")
conn.execute(text("DROP TABLE IF EXISTS messages CASCADE"))
conn.execute(text("DROP TABLE IF EXISTS files CASCADE"))
conn.execute(text("DROP TABLE IF EXISTS conversations CASCADE"))
conn.execute(text("DROP TABLE IF EXISTS users CASCADE"))
trans.commit()
print("✅ Database reset successfully!")
print("\nNow run:")
print(" poetry run alembic revision --autogenerate -m 'Create initial tables'")
print(" poetry run alembic upgrade head")
except Exception as e:
trans.rollback()
print(f"❌ Error: {e}")
raise
if __name__ == "__main__":
reset_database()