""" 应用 user_consumption 表的迁移脚本 运行方式: python scripts/apply_user_consumption_migration.py """ import sys from pathlib import Path from sqlalchemy import text # 确保可以导入 app 包(将项目根目录加入 sys.path) project_root = Path(__file__).parent.parent sys.path.insert(0, str(project_root)) from app.database import engine def main(): sql_file = project_root / "migrations" / "030_create_user_consumption.sql" if not sql_file.exists(): print(f"❌ 迁移文件不存在: {sql_file}") sys.exit(1) sql_content = sql_file.read_text(encoding="utf-8") try: with engine.connect() as conn: # 直接一次性执行整个 SQL 文件,避免将包含 DO$$/函数块 的语句错误拆分 conn.execute(text(sql_content)) conn.commit() print("✅ 已成功创建/更新表: aigcspace.user_consumption") except Exception as e: print("❌ 执行迁移失败") print(f"错误信息: {e}") sys.exit(1) if __name__ == "__main__": main()