fix: 追加原始SKU字段兼容迁移 (#259)
This commit is contained in:
@@ -20,7 +20,7 @@ import (
|
||||
"cmautobuy/admin/spec"
|
||||
)
|
||||
|
||||
const mysqlSchemaVersion = 28
|
||||
const mysqlSchemaVersion = 29
|
||||
|
||||
// OpenMySQL 打开生产 MySQL 8 数据库。错误信息绝不包含完整 DSN 或密码。
|
||||
func OpenMySQL(cfg config.DatabaseConfig) (*sql.DB, error) {
|
||||
@@ -759,9 +759,39 @@ func MigrateMySQL(db *sql.DB) error {
|
||||
return fmt.Errorf("记录 MySQL schema v28 失败: %w", err)
|
||||
}
|
||||
}
|
||||
if current < 29 {
|
||||
if err := migrateMySQLV29(db); err != nil {
|
||||
return fmt.Errorf("执行 MySQL schema v29 失败: %w", err)
|
||||
}
|
||||
if err := checkMySQLV29Shape(db); err != nil {
|
||||
return fmt.Errorf("MySQL schema v29 自检失败,未记录版本: %w", err)
|
||||
}
|
||||
if _, err := db.Exec(`INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)`, 29, time.Now().UTC().Format(time.RFC3339Nano)); err != nil {
|
||||
return fmt.Errorf("记录 MySQL schema v29 失败: %w", err)
|
||||
}
|
||||
}
|
||||
return CheckMySQLSchema(db)
|
||||
}
|
||||
|
||||
// migrateMySQLV29 修复曾由 v28 中间版本按表默认排序规则创建的原始 SKU 字段。
|
||||
// 已经是最终结构时直接返回,兼容空库、v27 升级和已记录 v28 的数据库。
|
||||
func migrateMySQLV29(db *sql.DB) error {
|
||||
var dataType, nullable, collation string
|
||||
var length sql.NullInt64
|
||||
if err := db.QueryRow(`SELECT data_type,character_maximum_length,is_nullable,collation_name
|
||||
FROM information_schema.columns WHERE table_schema=DATABASE()
|
||||
AND table_name='syb_inner_code_records' AND column_name='source_sku_raw'`).
|
||||
Scan(&dataType, &length, &nullable, &collation); err != nil {
|
||||
return fmt.Errorf("读取档口入库码原始 SKU 字段结构失败: %w", err)
|
||||
}
|
||||
if dataType == "varchar" && length.Valid && length.Int64 == 500 && nullable == "YES" && collation == "utf8mb4_bin" {
|
||||
return nil
|
||||
}
|
||||
_, err := db.Exec(`ALTER TABLE syb_inner_code_records
|
||||
MODIFY COLUMN source_sku_raw VARCHAR(500) COLLATE utf8mb4_bin NULL`)
|
||||
return err
|
||||
}
|
||||
|
||||
// migrateMySQLV28 保留 Excel 原始 SKU,作为确定性匹配的可审计证据。
|
||||
func migrateMySQLV28(db *sql.DB) error {
|
||||
exists, err := mysqlColumnExists(db, "syb_inner_code_records", "source_sku_raw")
|
||||
@@ -2376,6 +2406,13 @@ func CheckMySQLSchema(db *sql.DB) error {
|
||||
if err := checkMySQLV27Shape(db); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := checkMySQLV28Shape(db); err != nil {
|
||||
return err
|
||||
}
|
||||
return checkMySQLV29Shape(db)
|
||||
}
|
||||
|
||||
func checkMySQLV29Shape(db *sql.DB) error {
|
||||
return checkMySQLV28Shape(db)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user