Ahmed-Raza/phase-3-backend
0
1from database import engine
2from sqlmodel import SQLModel
3from sqlalchemy import text
4
5# Import ALL your models
6from models.user import User
7
8def reset_database():
9 """Drop all tables and recreate them with the correct schema"""
10 print("๐๏ธ Dropping all existing tables...")
11
12 # Drop specific tables
13 with engine.connect() as conn:
14 try:
15 conn.execute(text('DROP TABLE IF EXISTS "message" CASCADE;'))
16 conn.execute(text('DROP TABLE IF EXISTS "conversation" CASCADE;'))
17 conn.execute(text('DROP TABLE IF EXISTS "task" CASCADE;'))
18 conn.execute(text('DROP TABLE IF EXISTS "tasks" CASCADE;'))
19 conn.execute(text('DROP TABLE IF EXISTS "user" CASCADE;'))
20 conn.execute(text('DROP TABLE IF EXISTS "users" CASCADE;'))
21 conn.execute(text('DROP TABLE IF EXISTS "alembic_version" CASCADE;'))
22 conn.commit()
23 print("โ
All tables dropped!")
24 except Exception as e:
25 print(f"Note: {e}")
26
27 print("\nโ
Creating all tables with correct schema...")
28 SQLModel.metadata.create_all(engine)
29
30 print("\n๐ Database reset complete!")
31
32 # Verify
33 from sqlalchemy import inspect
34 inspector = inspect(engine)
35 tables = inspector.get_table_names()
36 print(f"\n๐ Tables created: {tables}")
37
38 if 'user' in tables:
39 columns = inspector.get_columns('user')
40 print("\n๐ค User table columns:")
41 for col in columns:
42 nullable = "NULL" if col.get('nullable', True) else "NOT NULL"
43 print(f" - {col['name']:<20} {str(col['type']):<20} {nullable}")
44
45if __name__ == "__main__":
46 confirmation = input("โ ๏ธ WARNING: This will delete ALL data. Continue? (yes/no): ")
47 if confirmation.lower() == 'yes':
48 reset_database()
49 else:
50 print("โ Cancelled.")