"""
修复用户表缺失字段
"""
from app import create_app
from models import db

def fix_fields():
    app = create_app()
    with app.app_context():
        with db.engine.connect() as conn:
            try:
                # 添加 email_verified_at
                conn.execute(db.text("""
                    ALTER TABLE users
                    ADD COLUMN email_verified_at DATETIME COMMENT '邮箱验证时间'
                """))
                conn.commit()
                print("[OK] 添加 email_verified_at")
            except Exception as e:
                print(f"[SKIP] email_verified_at: {e}")

            try:
                # 添加 email_verify_token
                conn.execute(db.text("""
                    ALTER TABLE users
                    ADD COLUMN email_verify_token VARCHAR(100) COMMENT '邮箱验证令牌'
                """))
                conn.commit()
                print("[OK] 添加 email_verify_token")
            except Exception as e:
                print(f"[SKIP] email_verify_token: {e}")

            try:
                # 添加 email_verify_token_expires
                conn.execute(db.text("""
                    ALTER TABLE users
                    ADD COLUMN email_verify_token_expires DATETIME COMMENT '验证令牌过期时间'
                """))
                conn.commit()
                print("[OK] 添加 email_verify_token_expires")
            except Exception as e:
                print(f"[SKIP] email_verify_token_expires: {e}")

            print("\n[SUCCESS] 字段修复完成！")

if __name__ == '__main__':
    fix_fields()
