#!/usr/bin/env python3 """为 points_consumption_log 表添加 updated_at 字段""" import pymysql import sys # 数据库配置 DB_CONFIG = { 'host': '192.168.1.206', 'port': 3306, 'user': 'root', 'password': 'test123456', 'database': 'shudao_test', 'charset': 'utf8mb4' } def add_updated_at_column(): """添加 updated_at 字段""" conn = None try: print("连接数据库...") conn = pymysql.connect(**DB_CONFIG) cursor = conn.cursor() # 检查字段是否已存在 cursor.execute(""" SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'shudao_test' AND TABLE_NAME = 'points_consumption_log' AND COLUMN_NAME = 'updated_at' """) exists = cursor.fetchone()[0] if exists: print("✓ updated_at 字段已存在,无需添加") return True # 添加字段 print("添加 updated_at 字段...") cursor.execute(""" ALTER TABLE points_consumption_log ADD COLUMN updated_at INT DEFAULT 0 COMMENT 'Unix时间戳' AFTER created_at """) conn.commit() print("✓ updated_at 字段添加成功") # 验证 cursor.execute("SHOW COLUMNS FROM points_consumption_log LIKE 'updated_at'") result = cursor.fetchone() if result: print(f"✓ 验证成功: {result}") return True else: print("❌ 验证失败:字段未找到") return False except Exception as e: print(f"❌ 错误: {e}") if conn: conn.rollback() return False finally: if conn: conn.close() if __name__ == "__main__": print("=" * 60) print("为 points_consumption_log 表添加 updated_at 字段") print("=" * 60) success = add_updated_at_column() sys.exit(0 if success else 1)