fix: 多件档口入库码逐件回写 (#250)
This commit is contained in:
@@ -20,7 +20,7 @@ import (
|
||||
"cmautobuy/admin/spec"
|
||||
)
|
||||
|
||||
const mysqlSchemaVersion = 26
|
||||
const mysqlSchemaVersion = 27
|
||||
|
||||
// OpenMySQL 打开生产 MySQL 8 数据库。错误信息绝不包含完整 DSN 或密码。
|
||||
func OpenMySQL(cfg config.DatabaseConfig) (*sql.DB, error) {
|
||||
@@ -737,9 +737,34 @@ func MigrateMySQL(db *sql.DB) error {
|
||||
return fmt.Errorf("记录 MySQL schema v26 失败: %w", err)
|
||||
}
|
||||
}
|
||||
if current < 27 {
|
||||
if err := migrateMySQLV27(db); err != nil {
|
||||
return fmt.Errorf("执行 MySQL schema v27 失败: %w", err)
|
||||
}
|
||||
if err := checkMySQLV27Shape(db); err != nil {
|
||||
return fmt.Errorf("MySQL schema v27 自检失败,未记录版本: %w", err)
|
||||
}
|
||||
if _, err := db.Exec(`INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)`, 27, time.Now().UTC().Format(time.RFC3339Nano)); err != nil {
|
||||
return fmt.Errorf("记录 MySQL schema v27 失败: %w", err)
|
||||
}
|
||||
}
|
||||
return CheckMySQLSchema(db)
|
||||
}
|
||||
|
||||
// migrateMySQLV27 保存多件档口入库码逐件远端检查点,不拆分新的业务表。
|
||||
func migrateMySQLV27(db *sql.DB) error {
|
||||
exists, err := mysqlColumnExists(db, "syb_inner_code_records", "remote_items_json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
_, err = db.Exec(`ALTER TABLE syb_inner_code_records
|
||||
ADD COLUMN remote_items_json JSON NULL AFTER remote_inner_code`)
|
||||
return err
|
||||
}
|
||||
|
||||
// migrateMySQLV26 建立采购运行时规格解析审计。一个记录同时保存候选观察和最终决策,
|
||||
// 不覆盖 PDD 商品主数据,也不通过外键阻止任务的既有硬删除流程。
|
||||
func migrateMySQLV26(db *sql.DB) error {
|
||||
@@ -2320,7 +2345,23 @@ func CheckMySQLSchema(db *sql.DB) error {
|
||||
if err := checkMySQLV25Shape(db); err != nil {
|
||||
return err
|
||||
}
|
||||
return checkMySQLV26Shape(db)
|
||||
if err := checkMySQLV26Shape(db); err != nil {
|
||||
return err
|
||||
}
|
||||
return checkMySQLV27Shape(db)
|
||||
}
|
||||
|
||||
func checkMySQLV27Shape(db *sql.DB) error {
|
||||
var dataType, nullable string
|
||||
if err := db.QueryRow(`SELECT data_type,is_nullable FROM information_schema.columns
|
||||
WHERE table_schema=DATABASE() AND table_name='syb_inner_code_records' AND column_name='remote_items_json'`).
|
||||
Scan(&dataType, &nullable); err != nil {
|
||||
return fmt.Errorf("检查档口入库码逐件检查点字段失败: %w", err)
|
||||
}
|
||||
if dataType != "json" || nullable != "YES" {
|
||||
return fmt.Errorf("档口入库码逐件检查点字段 remote_items_json 结构不正确")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMySQLV26Shape(db *sql.DB) error {
|
||||
|
||||
Reference in New Issue
Block a user