migrate_ai_fields.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import pymysql
  2. db_config = {
  3. "host": "rm-f8ze60yirdj8786u2.mysql.rds.aliyuncs.com",
  4. "port": 3306,
  5. "user": "root",
  6. "password": "csqz@20255",
  7. "db": "csqz-client",
  8. "charset": "utf8mb4",
  9. "cursorclass": pymysql.cursors.DictCursor
  10. }
  11. def migrate():
  12. conn = pymysql.connect(**db_config)
  13. try:
  14. with conn.cursor() as cursor:
  15. # 1. Add ai_status and ai_content to genealogy_records
  16. print("Checking genealogy_records table...")
  17. cursor.execute("DESCRIBE genealogy_records")
  18. columns = [col['Field'] for col in cursor.fetchall()]
  19. if 'ai_status' not in columns:
  20. print("Adding ai_status column...")
  21. cursor.execute("ALTER TABLE genealogy_records ADD COLUMN ai_status INT DEFAULT 0 COMMENT '0:未开始, 1:处理中, 2:成功, 3:失败'")
  22. if 'ai_content' not in columns:
  23. print("Adding ai_content column...")
  24. cursor.execute("ALTER TABLE genealogy_records ADD COLUMN ai_content LONGTEXT COMMENT 'AI解析结果JSON'")
  25. # 2. Add source_record_id to family_member_info
  26. print("Checking family_member_info table...")
  27. cursor.execute("DESCRIBE family_member_info")
  28. member_columns = [col['Field'] for col in cursor.fetchall()]
  29. if 'source_record_id' not in member_columns:
  30. print("Adding source_record_id column...")
  31. cursor.execute("ALTER TABLE family_member_info ADD COLUMN source_record_id INT DEFAULT NULL COMMENT '关联的原始图片记录ID'")
  32. conn.commit()
  33. print("Migration completed successfully.")
  34. except Exception as e:
  35. print(f"Migration failed: {e}")
  36. conn.rollback()
  37. finally:
  38. conn.close()
  39. if __name__ == "__main__":
  40. migrate()