159 lines
4.0 KiB
Python
159 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""数据库迁移脚本 - 简化版"""
|
|
|
|
import sys
|
|
import pymysql
|
|
|
|
# 数据库配置(从 lib/db.ts 获取)
|
|
DB_CONFIG = {
|
|
'host': '56b4c23f6853c.gz.cdb.myqcloud.com',
|
|
'port': 14413,
|
|
'user': 'cdb_outerroot',
|
|
'password': 'Zhiqun1984',
|
|
'database': 'soul_miniprogram',
|
|
'charset': 'utf8mb4'
|
|
}
|
|
|
|
def execute_sql(cursor, sql, description):
|
|
"""执行SQL并打印结果"""
|
|
try:
|
|
cursor.execute(sql)
|
|
print(f"[OK] {description}")
|
|
return True
|
|
except pymysql.err.OperationalError as e:
|
|
if 'Duplicate' in str(e) or 'already exists' in str(e):
|
|
print(f"[SKIP] {description} (already exists)")
|
|
return True
|
|
else:
|
|
print(f"[ERROR] {description}: {e}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"[ERROR] {description}: {e}")
|
|
return False
|
|
|
|
def main():
|
|
print("="*60)
|
|
print("Database Migration: referral_bindings table upgrade")
|
|
print("="*60)
|
|
print()
|
|
|
|
try:
|
|
# 连接数据库
|
|
print("Connecting to database...")
|
|
conn = pymysql.connect(**DB_CONFIG)
|
|
cursor = conn.cursor()
|
|
print(f"[OK] Connected to: {DB_CONFIG['database']}")
|
|
print()
|
|
except Exception as e:
|
|
print(f"[ERROR] Database connection failed: {e}")
|
|
sys.exit(1)
|
|
|
|
# 1. 添加新字段
|
|
print("Step 1: Adding new fields")
|
|
print("-"*60)
|
|
|
|
execute_sql(
|
|
cursor,
|
|
"""ALTER TABLE referral_bindings
|
|
ADD COLUMN last_purchase_date DATETIME NULL COMMENT 'Last purchase time'""",
|
|
"Add field: last_purchase_date"
|
|
)
|
|
|
|
execute_sql(
|
|
cursor,
|
|
"""ALTER TABLE referral_bindings
|
|
ADD COLUMN purchase_count INT DEFAULT 0 COMMENT 'Purchase count'""",
|
|
"Add field: purchase_count"
|
|
)
|
|
|
|
execute_sql(
|
|
cursor,
|
|
"""ALTER TABLE referral_bindings
|
|
ADD COLUMN total_commission DECIMAL(10,2) DEFAULT 0.00 COMMENT 'Total commission'""",
|
|
"Add field: total_commission"
|
|
)
|
|
|
|
print()
|
|
|
|
# 2. 添加索引
|
|
print("Step 2: Adding indexes")
|
|
print("-"*60)
|
|
|
|
execute_sql(
|
|
cursor,
|
|
"""ALTER TABLE referral_bindings
|
|
ADD INDEX idx_referee_status (referee_id, status)""",
|
|
"Add index: idx_referee_status"
|
|
)
|
|
|
|
execute_sql(
|
|
cursor,
|
|
"""ALTER TABLE referral_bindings
|
|
ADD INDEX idx_expiry_purchase (expiry_date, purchase_count, status)""",
|
|
"Add index: idx_expiry_purchase"
|
|
)
|
|
|
|
print()
|
|
|
|
# 3. 修改 status 枚举
|
|
print("Step 3: Updating status enum")
|
|
print("-"*60)
|
|
|
|
execute_sql(
|
|
cursor,
|
|
"""ALTER TABLE referral_bindings
|
|
MODIFY COLUMN status ENUM('active', 'converted', 'expired', 'cancelled')
|
|
DEFAULT 'active' COMMENT 'Binding status'""",
|
|
"Update status enum type"
|
|
)
|
|
|
|
print()
|
|
|
|
# 提交更改
|
|
conn.commit()
|
|
|
|
# 4. 验证字段
|
|
print("Step 4: Verifying migration")
|
|
print("-"*60)
|
|
|
|
cursor.execute("SHOW COLUMNS FROM referral_bindings")
|
|
columns = cursor.fetchall()
|
|
|
|
required_fields = ['last_purchase_date', 'purchase_count', 'total_commission']
|
|
found_fields = [col[0] for col in columns]
|
|
|
|
for field in required_fields:
|
|
if field in found_fields:
|
|
print(f"[OK] Field {field} exists")
|
|
else:
|
|
print(f"[ERROR] Field {field} not found")
|
|
|
|
print()
|
|
|
|
# 5. 显示索引
|
|
print("Step 5: Current indexes")
|
|
print("-"*60)
|
|
|
|
cursor.execute("SHOW INDEX FROM referral_bindings")
|
|
indexes = cursor.fetchall()
|
|
|
|
index_names = set()
|
|
for idx in indexes:
|
|
if idx[2] not in index_names:
|
|
print(f" - {idx[2]} ({idx[4]})")
|
|
index_names.add(idx[2])
|
|
|
|
print()
|
|
|
|
# 关闭连接
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
print("="*60)
|
|
print("[OK] Migration completed!")
|
|
print("="*60)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|