Files
SupplierSystem/migrations/20260922_drop_legacy_cost_details_sku_uq.sql
Ubuntu deb3e72750
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
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

58 lines
2.7 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 删除 (order_code, sku_code, cost_source) 上的**遗留**唯一索引(缺 order_item_id IS NULL 谓词)。
--
-- 现象(2026-09-22,BR004):同一订单同一 SKU「删除后重新新增」一行时,保存订单明细报
-- 500 duplicate key value violates unique constraint "cost_details_order_code_sku_code_coalesce_idx"
-- 根因:成本已迁移到「订单明细」粒度(见 20260901_cost_details_order_item_granularity.sql),
-- 但该迁移按**索引名**删旧索引,且名字写的是 uq_cost_details_purchase_order_sku_source;
-- 而 20260818 在部分 schema 里实际建出的名字是 cost_details_order_code_sku_code_coalesce_idx
-- → DROP INDEX IF EXISTS 静默跳过,SKU 级唯一索引留了下来,把新明细行的成本行挡在门外。
-- 实测:仅 tenant_br004 存在该遗留索引(legacy_public / BR001-003 / tenant_template 均只有正确索引)。
--
-- 本迁移**按索引定义**删除(沿用 20260818 的稳健做法):只要 purchase 条件下
-- (order_code, sku_code, COALESCE(cost_source,'')) 唯一、且**不含** order_item_id IS NULL 谓词,
-- 就视为遗留。删后:
-- - order_item_id IS NULL 的历史行仍由 uq_cost_details_purchase_legacy_sku_source 保证唯一;
-- - order_item_id NOT NULL 的明细行由 uq_cost_details_purchase_item_source 保证唯一;
-- - NULL 行与明支行同 (order, sku, source) 可以并存 —— 这正是 20260901 的既定语义。
-- 不改任何数据、不改读取口径,可重复执行。
DO $$
DECLARE
sch text;
idx record;
BEGIN
FOR sch IN
SELECT nspname
FROM pg_namespace
WHERE nspname IN ('public', 'legacy_public', 'tenant_template')
OR nspname LIKE 'tenant_%'
LOOP
IF NOT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = sch AND table_name = 'cost_details'
) THEN
CONTINUE;
END IF;
FOR idx IN
SELECT i.relname AS index_name
FROM pg_index x
JOIN pg_class t ON t.oid = x.indrelid
JOIN pg_namespace n ON n.oid = t.relnamespace
JOIN pg_class i ON i.oid = x.indexrelid
WHERE n.nspname = sch
AND t.relname = 'cost_details'
AND x.indisunique
AND pg_get_indexdef(x.indexrelid) LIKE '%(order_code, sku_code%'
AND pg_get_indexdef(x.indexrelid) LIKE '%cost_source%'
AND pg_get_indexdef(x.indexrelid) LIKE '%purchase%'
AND pg_get_indexdef(x.indexrelid) NOT LIKE '%order_item_id IS NULL%'
LOOP
RAISE NOTICE 'drop legacy cost_details sku-level unique index: %.%', sch, idx.index_name;
EXECUTE format('DROP INDEX IF EXISTS %I.%I', sch, idx.index_name);
END LOOP;
END LOOP;
END $$;
SELECT public.ensure_tenant_schemas_match_template();