from sqlalchemy import create_engine, text from config import Config def migrate(): # 从 URI 解析连接参数 uri = Config.SQLALCHEMY_DATABASE_URI print(f"正在手动连接数据库进行迁移 (SQLAlchemy)... ") engine = create_engine(uri) try: with engine.connect() as conn: # 添加 api_key 字段 print("🔧 正在检查并添加 users.api_key 字段...") conn.execute(text("ALTER TABLE users ADD COLUMN IF NOT EXISTS api_key VARCHAR(255);")) conn.commit() print("✅ 数据库字段 users.api_key 处理成功") except Exception as e: print(f"❌ 迁移失败: {e}") if __name__ == "__main__": migrate()