修复(迁移): 193 改为函数内守卫(WHEN 子句不支持 INSERT/DELETE 的 OLD/NEW)
This commit is contained in:
@@ -93,11 +93,16 @@ test('路由变更重算只在实际改变路由事实时触发(迁移 193)'
|
||||
new URL('../../db/migrations/193_sku_route_refresh_trigger_scope.sql', import.meta.url),
|
||||
'utf8',
|
||||
)
|
||||
// 触发器必须在,且必须带 WHEN 守卫:订单入库每次都 upsert 路由,
|
||||
// 无变化写入如果也重算,热门 SKU 会超过 statement_timeout 拖死入库。
|
||||
assert.ok(triggerScopeMigration.includes('CREATE TRIGGER trg_sku_route_refresh_fulfillments'))
|
||||
assert.ok(triggerScopeMigration.includes('WHEN ('))
|
||||
assert.ok(!triggerScopeMigration.includes('TG_OP'), 'WHEN 子句不能引用 TG_OP')
|
||||
// 触发器本身保持原样(单触发器覆盖 INSERT/UPDATE/DELETE),守卫写在函数内部:
|
||||
// WHEN 子句在 INSERT/DELETE 上不能引用 OLD/NEW,只有函数里能读 TG_OP。
|
||||
assert.ok(
|
||||
triggerScopeMigration.includes(
|
||||
'CREATE OR REPLACE FUNCTION refresh_fulfillments_for_sku_route_change',
|
||||
),
|
||||
)
|
||||
assert.ok(triggerScopeMigration.includes("IF TG_OP = 'UPDATE'"))
|
||||
assert.ok(triggerScopeMigration.includes('IS NOT DISTINCT FROM'))
|
||||
assert.ok(triggerScopeMigration.includes('RETURN NULL'))
|
||||
for (const column of [
|
||||
'channel',
|
||||
'enabled',
|
||||
@@ -107,10 +112,10 @@ test('路由变更重算只在实际改变路由事实时触发(迁移 193)'
|
||||
'canonical_product_id',
|
||||
]) {
|
||||
assert.ok(
|
||||
triggerScopeMigration.includes(`OLD.${column} IS DISTINCT FROM NEW.${column}`),
|
||||
`WHEN 守卫必须比较 ${column}`,
|
||||
triggerScopeMigration.includes(`OLD.${column} IS NOT DISTINCT FROM NEW.${column}`),
|
||||
`守卫必须比较 ${column}`,
|
||||
)
|
||||
}
|
||||
// updated_at 只是写入痕迹,不能作为重算条件。
|
||||
assert.ok(!triggerScopeMigration.includes('OLD.updated_at IS DISTINCT FROM'))
|
||||
assert.ok(!triggerScopeMigration.includes('OLD.updated_at'))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user