run_create_final_model_tables.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. 执行最终版模型表结构迁移脚本
  3. 运行方式:
  4. python scripts/run_create_final_model_tables.py
  5. 功能:
  6. - DROP 并重建 models_new、model_price_new、crawler_sync_log 表
  7. - ⚠️ 会清空上述三张表的所有数据,请确认目标数据库后再执行
  8. """
  9. import sys
  10. from pathlib import Path
  11. project_root = Path(__file__).parent.parent
  12. sys.path.insert(0, str(project_root))
  13. from app.database import engine
  14. from sqlalchemy import text
  15. MIGRATION_FILE = project_root / "migrations" / "create_final_model_tables.sql"
  16. def main():
  17. print("=" * 60)
  18. print("最终版模型表结构迁移")
  19. print("=" * 60)
  20. print(f"SQL 文件: {MIGRATION_FILE.name}")
  21. print()
  22. print("⚠️ 此操作将 DROP 并重建以下表:")
  23. print(" - aigcspace.models_new")
  24. print(" - aigcspace.model_price_new")
  25. print(" - aigcspace.crawler_sync_log")
  26. print()
  27. confirm = input("确认执行?(yes/no): ")
  28. if confirm.strip().lower() != "yes":
  29. print("已取消。")
  30. sys.exit(0)
  31. if not MIGRATION_FILE.exists():
  32. print(f"❌ 文件不存在: {MIGRATION_FILE}")
  33. sys.exit(1)
  34. sql = MIGRATION_FILE.read_text(encoding="utf-8")
  35. try:
  36. with engine.connect() as conn:
  37. conn.execute(text(sql))
  38. conn.commit()
  39. print("\n✅ 迁移执行成功")
  40. print(" 已创建:models_new / model_price_new / crawler_sync_log 及相关索引")
  41. except Exception as e:
  42. print(f"\n❌ 执行失败: {e}")
  43. sys.exit(1)
  44. if __name__ == "__main__":
  45. main()