Files
SupplierSystem/migrations/20261070_orders_resupply_unique.sql
Ubuntu 8c2f9e33e7
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(迁移): 20261070 改为逐租户 schema 建索引(orders 在租户 schema,原写法 42P01)
2026-10-20 21:17:28 +08:00

21 lines
699 B
SQL

-- 同一原单只允许存在一张「未完结」的补送单(从数据库层防重复建补送单)
-- ⚠️ orders 是**租户 schema** 下的表(不是 public),必须逐 schema 建索引。
DO $$
DECLARE
sch text;
BEGIN
FOR sch IN
SELECT schema_name FROM information_schema.schemata
WHERE schema_name LIKE 'tenant\_%' AND schema_name <> 'tenant_template'
LOOP
EXECUTE format(
'CREATE UNIQUE INDEX IF NOT EXISTS uq_orders_resupply_per_original
ON %I.orders (branch_code, linked_order_code)
WHERE source = ''return_resupply''
AND linked_order_code IS NOT NULL
AND order_status < 90',
sch
);
END LOOP;
END $$;