-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmigrate_database.py
More file actions
executable file
·74 lines (55 loc) · 2.5 KB
/
migrate_database.py
File metadata and controls
executable file
·74 lines (55 loc) · 2.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""
Database Migration Script for IT Asset Manager
Adds new columns for password reset functionality
"""
import sqlite3
import os
from datetime import datetime
def migrate_database():
db_path = 'instance/it_assets.db'
if not os.path.exists(db_path):
print("❌ Database file not found. Please run the application first to create the database.")
return
print("🔄 Starting database migration...")
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check if columns already exist
cursor.execute("PRAGMA table_info(user)")
columns = [column[1] for column in cursor.fetchall()]
migrations_needed = []
if 'email' not in columns:
migrations_needed.append("ALTER TABLE user ADD COLUMN email VARCHAR(120)")
if 'reset_token' not in columns:
migrations_needed.append("ALTER TABLE user ADD COLUMN reset_token VARCHAR(100)")
if 'reset_token_expiry' not in columns:
migrations_needed.append("ALTER TABLE user ADD COLUMN reset_token_expiry DATETIME")
if 'created_at' not in columns:
migrations_needed.append("ALTER TABLE user ADD COLUMN created_at DATETIME")
if not migrations_needed:
print("✅ Database is already up to date!")
return
print(f"📝 Applying {len(migrations_needed)} migrations...")
for migration in migrations_needed:
print(f" Executing: {migration}")
cursor.execute(migration)
# Update existing admin user with email if it doesn't have one
cursor.execute("SELECT id, email FROM user WHERE username = 'admin'")
admin_user = cursor.fetchone()
if admin_user and not admin_user[1]: # If admin exists but has no email
cursor.execute(
"UPDATE user SET email = ?, created_at = ? WHERE username = 'admin'",
('admin@itassetmanager.local', datetime.utcnow())
)
print(" Updated admin user with default email")
conn.commit()
print("✅ Database migration completed successfully!")
print("🎯 You can now use the password reset functionality.")
except Exception as e:
print(f"❌ Migration failed: {str(e)}")
conn.rollback()
finally:
conn.close()
if __name__ == '__main__':
migrate_database()