feat: 增加顺运宝店铺筛选 (#151)
This commit is contained in:
+38
-1
@@ -265,7 +265,7 @@ var migrations = [][]string{
|
||||
// 背景见 #20:v1 曾经被原地改写而不是新增版本,导致已经建过库的机器
|
||||
// (user_version 已经越过 v1)永远不会重跑改写后的语句,程序拿着一个
|
||||
// 和代码对不上的库静默启动。
|
||||
const schemaVersion = 8
|
||||
const schemaVersion = 9
|
||||
|
||||
// migrationV4 给 PDD 商品增加店铺名。
|
||||
//
|
||||
@@ -380,6 +380,18 @@ var migrationV8 = []string{
|
||||
ON syb_sync_runs(started_at DESC, run_id DESC);`,
|
||||
}
|
||||
|
||||
// migrationV9 把顺运宝原始 JSON 里的货运单店铺名提升为可展示、可筛选的独立列。
|
||||
// 原始 syb_data 永久保留;无效 JSON、缺字段和纯空白值都安全地保持 NULL。
|
||||
var migrationV9 = []string{
|
||||
`ALTER TABLE syb_orders ADD COLUMN shop_name TEXT;`,
|
||||
`UPDATE syb_orders
|
||||
SET shop_name = CASE
|
||||
WHEN json_valid(syb_data) THEN NULLIF(TRIM(json_extract(syb_data, '$.stock.shopName')), '')
|
||||
ELSE NULL
|
||||
END
|
||||
WHERE shop_name IS NULL;`,
|
||||
}
|
||||
|
||||
// Migrate 把数据库升到最新版本。
|
||||
// 已经是最新的就什么都不做,可以重复调用。
|
||||
func Migrate(db *sql.DB) error {
|
||||
@@ -466,6 +478,14 @@ func Migrate(db *sql.DB) error {
|
||||
if err := runSQLMigration(db, 8, migrationV8); err != nil {
|
||||
return err
|
||||
}
|
||||
reached = 8
|
||||
}
|
||||
|
||||
// v9 增加顺运宝店铺列并从仍保留的原始 JSON 回填。
|
||||
if reached < 9 {
|
||||
if err := runSQLMigration(db, 9, migrationV9); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -940,6 +960,12 @@ var requiredColumns = map[string][]string{
|
||||
"syb_sync_runs": {"user_id", "date_from", "date_to", "status", "cursor_advanced", "started_at", "finished_at"},
|
||||
}
|
||||
|
||||
// SQLite 当前测试库比 MySQL v1 多走一版迁移;这里单独检查,避免 MySQL
|
||||
// 在执行 v1 自检时提前要求尚未由 v11 创建的列。
|
||||
var sqliteOnlyRequiredColumns = map[string][]string{
|
||||
"syb_orders": {"shop_name"},
|
||||
}
|
||||
|
||||
// CheckSchema 在 Migrate 成功后调用,确认代码依赖的表都在。
|
||||
//
|
||||
// [必须] 缺表就返回错误,调用方要**拒绝启动**,不是打个警告继续跑。
|
||||
@@ -996,5 +1022,16 @@ func CheckSchema(db *sql.DB) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
for table, columns := range sqliteOnlyRequiredColumns {
|
||||
existingColumns, err := tableColumnSet(db, table)
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查数据表 %s 的列失败: %w", table, err)
|
||||
}
|
||||
for _, column := range columns {
|
||||
if !existingColumns[column] {
|
||||
return fmt.Errorf("数据库结构与本程序不匹配:数据表 %s 缺少列 %s", table, column)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user