from app import app
from extensions import db
from sqlalchemy import text

def migrate():
    with app.app_context():
        print("🔧 正在为 users 表增加 role 字段...")
        try:
            # 使用原生 SQL 增加字段
            db.session.execute(text('ALTER TABLE users ADD COLUMN IF NOT EXISTS role VARCHAR(20) DEFAULT \'user\''))
            db.session.commit()
            print("✅ 字段增加成功")
            
            # 设置管理员（可选，方便您测试）
            # db.session.execute(text("UPDATE users SET role = 'admin' WHERE phone = '您的手机号'"))
            # db.session.commit()
            
        except Exception as e:
            print(f"❌ 迁移失败: {e}")
            db.session.rollback()

if __name__ == '__main__':
    migrate()