run_migrations_v2.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. 执行V2版本数据库迁移脚本
  3. 运行方式:
  4. python scripts/run_migrations_v2.py
  5. 功能:
  6. - 执行4个V2版本的数据库迁移文件
  7. - 创建新表:asr_recognition_v2, audio_synthesis_v2, voice_clone_v2, long_text_audio
  8. - 不影响现有表和服务
  9. """
  10. import os
  11. import sys
  12. from pathlib import Path
  13. # 添加项目根目录到Python路径
  14. project_root = Path(__file__).parent.parent
  15. sys.path.insert(0, str(project_root))
  16. from app.database import engine
  17. from sqlalchemy import text
  18. def run_migration_file(filepath: str) -> bool:
  19. """执行单个迁移文件"""
  20. try:
  21. with open(filepath, 'r', encoding='utf-8') as f:
  22. sql_content = f.read()
  23. with engine.connect() as conn:
  24. # 执行SQL(可能包含多个语句)
  25. conn.execute(text(sql_content))
  26. conn.commit()
  27. print(f"✅ 成功执行: {os.path.basename(filepath)}")
  28. return True
  29. except Exception as e:
  30. print(f"❌ 执行失败: {os.path.basename(filepath)}")
  31. print(f" 错误信息: {e}")
  32. return False
  33. def main():
  34. """主函数"""
  35. print("=" * 60)
  36. print("开始执行V2版本数据库迁移")
  37. print("=" * 60)
  38. # 迁移文件列表(按顺序执行)
  39. migration_files = [
  40. "migrations/030_create_asr_recognition_v2_table.sql",
  41. "migrations/031_create_audio_synthesis_v2_table.sql",
  42. "migrations/032_create_voice_clone_v2_table.sql",
  43. "migrations/033_create_long_text_audio_table.sql",
  44. ]
  45. success_count = 0
  46. failed_count = 0
  47. for migration_file in migration_files:
  48. filepath = project_root / migration_file
  49. if not filepath.exists():
  50. print(f"⚠️ 文件不存在: {migration_file}")
  51. failed_count += 1
  52. continue
  53. print(f"\n执行迁移: {migration_file}")
  54. if run_migration_file(str(filepath)):
  55. success_count += 1
  56. else:
  57. failed_count += 1
  58. print("\n" + "=" * 60)
  59. print(f"迁移完成: 成功 {success_count} 个, 失败 {failed_count} 个")
  60. print("=" * 60)
  61. if failed_count > 0:
  62. print("\n⚠️ 部分迁移失败,请检查错误信息")
  63. sys.exit(1)
  64. else:
  65. print("\n✅ 所有迁移成功执行!")
  66. print("\n新表已创建:")
  67. print(" - asr_recognition_v2 (语音识别V2)")
  68. print(" - audio_synthesis_v2 (语音合成V2)")
  69. print(" - voice_clone_v2 (声音克隆V2)")
  70. print(" - long_text_audio (长文本转音频)")
  71. print("\n现在可以启动服务并测试V2 API端点")
  72. if __name__ == "__main__":
  73. main()