drop_log_table_fkeys.py 871 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. """
  3. 清理 api_call_log 和 ai_message 表的所有外键约束
  4. 使用方法:
  5. python scripts/drop_log_table_fkeys.py
  6. """
  7. import sys
  8. from pathlib import Path
  9. sys.path.append(str(Path(__file__).parent.parent))
  10. from sqlalchemy import text
  11. from app.database import engine
  12. SQL_FILE = Path(__file__).parent.parent / "migrations" / "drop_log_table_fkeys.sql"
  13. def main():
  14. print("开始清理外键约束...")
  15. sql = SQL_FILE.read_text(encoding="utf-8")
  16. try:
  17. with engine.begin() as conn:
  18. conn.execute(text(sql))
  19. print("✅ 外键清理完成")
  20. print(" - aigcspace.api_call_log 所有外键已移除")
  21. print(" - aigcspace.ai_message 所有外键已移除")
  22. except Exception as e:
  23. print(f"❌ 执行失败: {e}")
  24. sys.exit(1)
  25. if __name__ == "__main__":
  26. main()