#!/usr/bin/env python3 """ Database migration to add password reset fields to users table. Run this once to add the new columns for password reset functionality. """ import sys from app import app, db def migrate(): """Add password reset columns to users table""" with app.app_context(): try: # Check if columns already exist from sqlalchemy import inspect inspector = inspect(db.engine) columns = [col['name'] for col in inspector.get_columns('users')] if 'reset_token' in columns and 'reset_token_expiry' in columns: print("✓ Password reset columns already exist") return True # Add the new columns using raw SQL with db.engine.connect() as conn: if 'reset_token' not in columns: print("Adding reset_token column...") conn.execute(db.text( "ALTER TABLE users ADD COLUMN reset_token VARCHAR(100) UNIQUE" )) conn.execute(db.text( "CREATE INDEX IF NOT EXISTS ix_users_reset_token ON users(reset_token)" )) conn.commit() if 'reset_token_expiry' not in columns: print("Adding reset_token_expiry column...") conn.execute(db.text( "ALTER TABLE users ADD COLUMN reset_token_expiry TIMESTAMP" )) conn.commit() print("✓ Password reset columns added successfully") return True except Exception as e: print(f"✗ Migration failed: {e}") import traceback traceback.print_exc() return False if __name__ == '__main__': print("Running password reset migration...") success = migrate() sys.exit(0 if success else 1)