migrate_add_email_qq_fields.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. 数据库迁移脚本:为 users 表添加 email 和 qq_openid 字段
  3. 运行: python migrate_add_email_qq_fields.py
  4. """
  5. import asyncio
  6. import aiomysql
  7. from config import settings
  8. import sys
  9. async def migrate_add_email_qq_fields():
  10. """为 users 表添加 email 和 qq_openid 字段"""
  11. try:
  12. # 创建数据库连接
  13. conn = await aiomysql.connect(
  14. host=settings.MYSQL_HOST,
  15. port=settings.MYSQL_PORT,
  16. user=settings.MYSQL_USER,
  17. password=settings.MYSQL_PASSWORD,
  18. db=settings.MYSQL_DATABASE,
  19. charset='utf8mb4'
  20. )
  21. async with conn.cursor() as cursor:
  22. # 检查 users 表是否存在
  23. await cursor.execute("""
  24. SELECT COUNT(*) FROM information_schema.tables
  25. WHERE table_schema = %s AND table_name = 'users'
  26. """, (settings.MYSQL_DATABASE,))
  27. table_exists = (await cursor.fetchone())[0] > 0
  28. if not table_exists:
  29. print("❌ users 表不存在,请先创建表")
  30. conn.close()
  31. return False
  32. # 检查是否已存在 email 列
  33. await cursor.execute("""
  34. SELECT COUNT(*) FROM information_schema.columns
  35. WHERE table_schema = %s
  36. AND table_name = 'users'
  37. AND column_name = 'email'
  38. """, (settings.MYSQL_DATABASE,))
  39. has_email = (await cursor.fetchone())[0] > 0
  40. # 检查是否已存在 qq_openid 列
  41. await cursor.execute("""
  42. SELECT COUNT(*) FROM information_schema.columns
  43. WHERE table_schema = %s
  44. AND table_name = 'users'
  45. AND column_name = 'qq_openid'
  46. """, (settings.MYSQL_DATABASE,))
  47. has_qq_openid = (await cursor.fetchone())[0] > 0
  48. if has_email and has_qq_openid:
  49. print("✅ email 和 qq_openid 字段已存在,无需迁移")
  50. conn.close()
  51. return True
  52. # 添加 email 字段(如果不存在)
  53. if not has_email:
  54. print("🔄 添加 email 字段...")
  55. await cursor.execute("""
  56. ALTER TABLE users
  57. ADD COLUMN email VARCHAR(200) UNIQUE NULL COMMENT '邮箱' AFTER phone
  58. """)
  59. await cursor.execute("""
  60. CREATE INDEX idx_email ON users(email)
  61. """)
  62. print("✅ email 字段添加成功")
  63. # 添加 qq_openid 字段(如果不存在)
  64. if not has_qq_openid:
  65. print("🔄 添加 qq_openid 字段...")
  66. await cursor.execute("""
  67. ALTER TABLE users
  68. ADD COLUMN qq_openid VARCHAR(100) UNIQUE NULL COMMENT 'QQ OpenID' AFTER wechat_openid
  69. """)
  70. await cursor.execute("""
  71. CREATE INDEX idx_qq_openid ON users(qq_openid)
  72. """)
  73. print("✅ qq_openid 字段添加成功")
  74. await conn.commit()
  75. print("✅ 迁移完成")
  76. conn.close()
  77. return True
  78. except Exception as e:
  79. import traceback
  80. print(f"❌ 迁移失败: {e}")
  81. traceback.print_exc()
  82. return False
  83. if __name__ == "__main__":
  84. # 确认是否继续
  85. if len(sys.argv) > 1 and sys.argv[1] == "--force":
  86. confirm = "y"
  87. else:
  88. confirm = input("\n是否继续迁移 users 表添加 email 和 qq_openid 字段? (y/n): ")
  89. if confirm.lower() != 'y':
  90. sys.exit(0)
  91. # 执行迁移
  92. success = asyncio.run(migrate_add_email_qq_fields())
  93. if not success:
  94. sys.exit(1)