Files
SupplierSystem/migrations/20261050_order_items_spec.sql
T
雨天 ff158081b0
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
订单详情: 展示商品规格(先落库再展示)
order_items 原本无 spec 列,录单填的规格被丢弃(建单 INSERT 不含该列)。本轮加列 + 建单写入 + 详情表商品名后加只读「规格」列(三语)。

- 迁移 20261050:各 schema 给 order_items 加 spec VARCHAR(60)(可空、不回溯)
- 建单 INSERT 落 spec(trim / 60 字上限)
- 详情表 colCount 15/12→16/13、合计行 colSpan 6→7,契约测试钉住手写列对齐
- 详情读数 select('*') 已带出 spec,无需改查询

另入库「订单入库补字段」设计草案(待拍板,未实现)。
2026-10-16 16:28:24 +08:00

71 lines
2.9 KiB
SQL
Raw 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.
-- 订单行「商品规格」列(2026-10-16)
--
-- 背景(开发者报障):「录单时存在规格了(客户写在品名里的 500g/袋、600g*12瓶…),
-- 需要在订单详情列表展示这个商品的规格」。
-- 实测:录单(Web)已能解析/人工改「规格」并随 items 提交(`CustomerOrder.tsx`),
-- 但 `order_items` **没有 spec 列**、建单 INSERT 也不含它 ⇒ **规格被丢弃**
-- (`src/components/order/orderEntryShared.ts` 自述「不落库」)。
--
-- 本迁移只加列;建单写入与详情展示见:
-- 设计 docs/design/20261016-订单详情展示商品规格.md
--
-- 落点:`order_items.spec`(可空,存量订单为 NULL —— **不回溯**)。
-- `order_items` 是**每租户一张表**,故遍历 public + tenant_template + 所有 tenant_*
-- (沿用 migrations/20261042_inventory_shelf_life.sql 的写法;legacy_public 为归档 schema,跳过)。
-- 长度 60 与录单输入上限一致(`CustomerOrder.tsx` 的 `.slice(0, 60)`)。
--
-- ⚠️ 不新增表 ⇒ 不触「新业务表须写进 public.tenant_business_tables()」那条守卫。
-- ============================================================
-- 1) 加列(遍历所有 schema)
-- ============================================================
DO $$
DECLARE
sch text;
BEGIN
FOR sch IN
SELECT nspname
FROM pg_namespace
WHERE nspname IN ('public', 'tenant_template')
OR nspname LIKE 'tenant_%'
ORDER BY CASE nspname
WHEN 'public' THEN 0
WHEN 'tenant_template' THEN 1
ELSE 2
END, nspname
LOOP
IF to_regclass(format('%I.order_items', sch)) IS NOT NULL THEN
EXECUTE format(
'ALTER TABLE %I.order_items ADD COLUMN IF NOT EXISTS spec VARCHAR(60)',
sch
);
EXECUTE format(
'COMMENT ON COLUMN %I.order_items.spec IS %L',
sch,
'订单行商品规格(录单录入/人工修正的原文,如 500g/袋;60 字上限)。可空;不参与金额/成本计算'
);
END IF;
END LOOP;
END $$;
-- ============================================================
-- 2) 响亮失败守卫:public / tenant_template 的 order_items 上必须有该列
-- ============================================================
DO $$
BEGIN
IF to_regclass('public.order_items') IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'order_items' AND column_name = 'spec'
) THEN
RAISE EXCEPTION '迁移未生效:public.order_items.spec 不存在';
END IF;
IF to_regclass('tenant_template.order_items') IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_schema = 'tenant_template' AND table_name = 'order_items' AND column_name = 'spec'
) THEN
RAISE EXCEPTION '迁移未生效:tenant_template.order_items.spec 不存在';
END IF;
END $$;