feat: 建立全局店铺跨渠道管理 (#208 #209 #210 #211)

This commit is contained in:
chengma
2026-08-13 11:15:54 +08:00
parent 901b37a8cb
commit 8f707722ba
28 changed files with 1160 additions and 500 deletions
@@ -875,6 +875,57 @@ func TestMySQLMigrate_V14升级V17回填元数据规格并增加软删除(t *tes
}
}
func TestMySQLMigrate_V17升级V18迁移全局店铺并精确回填(t *testing.T) {
db := openMySQLMigrationTestDB(t)
defer db.Close()
cleanMySQLTestSchema(t, db)
defer cleanMySQLTestSchema(t, db)
if err := MigrateMySQL(db); err != nil {
t.Fatal(err)
}
// 退回真实 v17 形状,再放入旧准入配置和两侧历史数据。
mustExec(t, db, `ALTER TABLE shopee_products DROP FOREIGN KEY fk_shopee_products_shop`)
mustExec(t, db, `ALTER TABLE syb_orders DROP FOREIGN KEY fk_syb_orders_shop`)
mustExec(t, db, `ALTER TABLE shopee_products DROP COLUMN shop_id`)
mustExec(t, db, `ALTER TABLE syb_orders DROP COLUMN shop_id`)
mustExec(t, db, `DROP TABLE shop_channel_aliases`)
mustExec(t, db, `DROP TABLE shops`)
mustExec(t, db, `DELETE FROM schema_migrations WHERE version=18`)
now := "2026-08-13T02:00:00Z"
mustExec(t, db, `INSERT INTO users(user_id,username,password_hash,role,status,password_changed_at,created_at,updated_at)
VALUES('V18-USER','v18-user','x','admin','active',?,?,?)`, now, now, now)
mustExec(t, db, `INSERT INTO syb_allowed_shops(shop_id,shop_name,normalized_name,enabled,created_by_user_id,created_at,updated_at)
VALUES('V18-SHOP','精确店铺','精确店铺',1,'V18-USER',?,?)`, now, now)
mustExec(t, db, `INSERT INTO syb_orders(syb_id,order_no,shop_name,title,quantity,syb_data,created_at,updated_at)
VALUES('V18-ORDER','O-V18','精确店铺','商品',1,'{}',?,?)`, now, now)
mustExec(t, db, `INSERT INTO shopee_products(goods_id,title,shopee_shop_name,source,created_at,updated_at)
VALUES('V18-PRODUCT','商品','精确店铺','api',?,?),('V18-UNLINKED','商品','其他店铺','api',?,?)`, now, now, now, now)
if err := MigrateMySQL(db); err != nil {
t.Fatalf("v17 升级 v18 失败: %v", err)
}
if err := MigrateMySQL(db); err != nil {
t.Fatalf("v18 重放失败: %v", err)
}
if err := checkMySQLV18Shape(db); err != nil {
t.Fatalf("v18 结构错误: %v", err)
}
for _, query := range []string{
`SELECT shop_id FROM syb_orders WHERE syb_id='V18-ORDER'`,
`SELECT shop_id FROM shopee_products WHERE goods_id='V18-PRODUCT'`,
} {
var shopID string
if err := db.QueryRow(query).Scan(&shopID); err != nil || shopID != "V18-SHOP" {
t.Fatalf("历史精确回填错误: shop=%q err=%v", shopID, err)
}
}
var unlinked sql.NullString
if err := db.QueryRow(`SELECT shop_id FROM shopee_products WHERE goods_id='V18-UNLINKED'`).Scan(&unlinked); err != nil || unlinked.Valid {
t.Fatalf("无法确认的店铺不应猜测关联: %+v err=%v", unlinked, err)
}
}
func openMySQLMigrationTestDB(t *testing.T) *sql.DB {
t.Helper()
if os.Getenv("CMAUTOBUY_MYSQL_TEST") != "1" {