-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_database.py
More file actions
59 lines (48 loc) · 1.8 KB
/
reset_database.py
File metadata and controls
59 lines (48 loc) · 1.8 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""
Script to reset the database by dropping all tables and creating fresh ones.
This will delete all data!
"""
import sqlite3
import os
from app.database import engine, Base
from app.models import User, Post, Comment, Like
def reset_database():
"""Drop all tables and recreate them"""
print("⚠️ WARNING: This will delete all data in the database!")
print("Database file:", os.path.abspath("users.db"))
# Connect to database
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
# Get all table names
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = cursor.fetchall()
if tables:
print(f"Found {len(tables)} existing tables:")
for table in tables:
print(f" - {table[0]}")
# Drop all tables
print("\nDropping all tables...")
for table in tables:
table_name = table[0]
if table_name != 'sqlite_sequence': # Don't drop sqlite_sequence
cursor.execute(f"DROP TABLE IF EXISTS '{table_name}'")
print(f" Dropped: {table_name}")
conn.commit()
print("✅ All tables dropped successfully!")
else:
print("No existing tables found.")
# Create all tables fresh
print("\nCreating fresh tables...")
Base.metadata.create_all(bind=engine)
print("✅ All tables created successfully!")
# Verify tables were created
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
new_tables = cursor.fetchall()
print(f"\nNew tables created ({len(new_tables)}):")
for table in new_tables:
print(f" - {table[0]}")
conn.close()
print("\n🎉 Database reset complete!")
if __name__ == "__main__":
reset_database()