2026-01-16 22:24:14 +08:00
|
|
|
from sqlalchemy import create_engine, text
|
2026-01-12 00:53:31 +08:00
|
|
|
from config import Config
|
|
|
|
|
|
|
|
|
|
def migrate():
|
|
|
|
|
# 从 URI 解析连接参数
|
|
|
|
|
uri = Config.SQLALCHEMY_DATABASE_URI
|
2026-01-16 22:24:14 +08:00
|
|
|
print(f"正在手动连接数据库进行迁移 (SQLAlchemy)... ")
|
|
|
|
|
|
|
|
|
|
engine = create_engine(uri)
|
2026-01-12 00:53:31 +08:00
|
|
|
|
|
|
|
|
try:
|
2026-01-16 22:24:14 +08:00
|
|
|
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 处理成功")
|
2026-01-12 00:53:31 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
print(f"❌ 迁移失败: {e}")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
migrate()
|