fix: 修复不完整的商品目录迁移 (#144)

This commit is contained in:
chengma
2026-08-11 11:26:40 +08:00
parent cc595c164e
commit fef97cf5f3
4 changed files with 120 additions and 3 deletions
+30 -1
View File
@@ -19,7 +19,7 @@ import (
"cmautobuy/admin/spec"
)
const mysqlSchemaVersion = 9
const mysqlSchemaVersion = 10
// OpenMySQL 打开生产 MySQL 8 数据库。错误信息绝不包含完整 DSN 或密码。
func OpenMySQL(cfg config.DatabaseConfig) (*sql.DB, error) {
@@ -531,10 +531,39 @@ func MigrateMySQL(db *sql.DB) error {
if _, err := db.Exec(`INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)`, 9, time.Now().UTC().Format(time.RFC3339Nano)); err != nil {
return fmt.Errorf("记录 MySQL schema v9 失败: %w", err)
}
current = 9
}
if current < 10 {
if err := migrateMySQLV10(db); err != nil {
return fmt.Errorf("执行 MySQL schema v10 失败: %w", err)
}
if err := checkMySQLV10Shape(db); err != nil {
return fmt.Errorf("MySQL schema v10 自检失败,未记录版本: %w", err)
}
if _, err := db.Exec(`INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)`, 10, time.Now().UTC().Format(time.RFC3339Nano)); err != nil {
return fmt.Errorf("记录 MySQL schema v10 失败: %w", err)
}
}
return CheckMySQLSchema(db)
}
// migrateMySQLV10 修复曾在 v8 开发中间状态记录过版本的数据库。
// v8 迁移本身逐项检查列和索引且回填只处理空值,因此可安全重放;
// 历史 v8/v9 版本记录保持不变,完整自检通过后再追加 v10。
func migrateMySQLV10(db *sql.DB) error {
if err := migrateMySQLV8(db); err != nil {
return fmt.Errorf("修复商品目录 SKU v8 结构失败: %w", err)
}
return nil
}
func checkMySQLV10Shape(db *sql.DB) error {
if err := checkMySQLV8Shape(db); err != nil {
return err
}
return checkMySQLV9Shape(db)
}
func migrateMySQLV9(db *sql.DB) error {
columns := []struct{ table, name, ddl string }{
{"shopee_products", "image_url", `ALTER TABLE shopee_products ADD COLUMN image_url VARCHAR(2048) NULL AFTER main_sku_code`},