-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_password.py
More file actions
32 lines (26 loc) · 1.15 KB
/
migrate_password.py
File metadata and controls
32 lines (26 loc) · 1.15 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
from app import app, db
from models import User
from sqlalchemy import text
def migrate_passwords():
with app.app_context():
try:
# Check if password_hash column exists
result = db.session.execute(text("PRAGMA table_info(user)"))
columns = [row[1] for row in result]
if 'password_hash' not in columns:
# Add the new column
db.session.execute(text('ALTER TABLE user ADD COLUMN password_hash VARCHAR(128)'))
# Migrate existing passwords (if any)
users = User.query.all()
for user in users:
if hasattr(user, 'password'):
user.set_password(user.password)
db.session.commit()
print("[SUCCESS] Updated user table with password hashing")
else:
print("[INFO] password_hash column already exists")
except Exception as e:
db.session.rollback()
print(f"[ERROR] During password migration: {e}")
if __name__ == '__main__':
migrate_passwords()