| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- """
- 应用 invoice 和 invoice_item 表的迁移脚本(036)
- 运行方式:
- python scripts/apply_invoice_tables_migration.py
- 说明:
- - 读取 migrations/036_create_invoice_tables.sql 并执行
- - 使用 app.database.engine 连接数据库
- - 需要 backend/.env 正确配置数据库连接
- """
- from pathlib import Path
- import sys
- from sqlalchemy import text
- # 确保可以找到 app 包
- project_root = Path(__file__).parent.parent
- sys.path.insert(0, str(project_root))
- from app.database import engine # noqa: E402
- def main() -> None:
- sql_file = project_root / "migrations" / "036_create_invoice_tables.sql"
- if not sql_file.exists():
- print(f"❌ 迁移文件不存在: {sql_file}")
- return
- sql_content = sql_file.read_text(encoding="utf-8")
- try:
- with engine.connect() as conn:
- conn.execute(text(sql_content))
- conn.commit()
- print("✅ 已成功创建/更新表: aigcspace.invoice / aigcspace.invoice_item")
- except Exception as exc: # noqa: BLE001
- print("❌ 执行迁移失败")
- print(f"错误信息: {exc}")
- if __name__ == "__main__":
- main()
|