-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_db.py
More file actions
60 lines (46 loc) · 2.25 KB
/
migrate_db.py
File metadata and controls
60 lines (46 loc) · 2.25 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
60
"""
Database Migration Script
=========================
Adds is_admin column to existing User table
"""
from app import app, db
from sqlalchemy import text
def migrate_database():
"""Add is_admin column to User table if it doesn't exist"""
with app.app_context():
print("=" * 60)
print("DATABASE MIGRATION - Adding is_admin column")
print("=" * 60)
try:
# Check if column exists
with db.engine.connect() as conn:
result = conn.execute(text("PRAGMA table_info(User)"))
columns = [row[1] for row in result]
print(f"\nCurrent User table columns: {columns}")
if 'is_admin' in columns:
print("\n✅ is_admin column already exists!")
else:
print("\n⚠️ is_admin column missing. Adding it now...")
# Add the column with default value False
conn.execute(text("ALTER TABLE User ADD COLUMN is_admin BOOLEAN DEFAULT 0"))
conn.commit()
print("✅ is_admin column added successfully!")
# Verify the change
result = conn.execute(text("PRAGMA table_info(User)"))
columns = [row[1] for row in result]
print(f"\nUpdated User table columns: {columns}")
# Show all users
result = conn.execute(text("SELECT user_id, name, email, is_admin FROM User"))
users = result.fetchall()
print(f"\n📊 Current users in database: {len(users)}")
for user in users:
print(f" - {user[1]} ({user[2]}) - Admin: {bool(user[3])}")
print("\n" + "=" * 60)
print("✅ Migration completed successfully!")
print("=" * 60)
except Exception as e:
print(f"\n❌ Migration error: {e}")
print("\nIf you see 'duplicate column name', the migration already ran.")
print("You can safely ignore this error.")
if __name__ == '__main__':
migrate_database()