Files
SupplierSystem/migrations/20261042_inventory_shelf_life.sql
T
雨天 33f2f7b75a
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
feat(库存): 修改弹窗改录保质期,有效期由生产日期+保质期派生
新增 inventory.shelf_life VARCHAR(32)(迁移 20261042,遍历 public/tenant_template/所有 tenant_*),存保质期原文(7天/6个月/1年)。

库存管理「修改」不再手填有效期:只要动了保质期或生产日期,就用 deriveExpiryDate 重算 expiry_date(推不出则清空)。解析与推算提到 server/src/utils/shelfLife.ts 与入库链路共用(月=30天、年=360天)。

入库链路(batchStockInboundRows)随行落 shelf_life 原文(原先算完即弃);列表行映射补 shelf_life;弹窗字段与三语文案同步。不做历史回填。
2026-10-13 13:42:15 +08:00

68 lines
2.8 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-14)
--
-- 背景:保质期可能是「7天」「6个月」「1年」等**原文**,单位有 天/月/年 三种;
-- 有效期不再由人直接填,而是由「生产日期 + 保质期原文」推算(见 server/src/utils/shelfLife.ts)。
-- 故批次上要能**存原文**,才能在「库存管理 → 修改」弹窗回显、并在生产日期变化时重算有效期。
--
-- 落点:`inventory.shelf_life`(可空,历史批次为空)。`inventory` 是**每租户一张表**,
-- 故遍历 public + tenant_template + 所有 tenant_* schema 逐库加列(沿用
-- migrations/20261028_warehouse_storage_zone.sql 的写法)。
-- 注:legacy_public 为归档 schema,沿用既有做法跳过。
--
-- 不做历史回填(回填会改 FIFO 排序语义,属库存冻结面;且「保质期」不是能从 expiry_date 反推的原文)。
--
-- 设计:docs/design/20261028-库存管理修改弹窗扩展批次与商品属性.md(「口径变更」节)
-- ============================================================
-- 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.inventory', sch)) IS NOT NULL THEN
EXECUTE format(
'ALTER TABLE %I.inventory ADD COLUMN IF NOT EXISTS shelf_life VARCHAR(32)',
sch
);
EXECUTE format(
'COMMENT ON COLUMN %I.inventory.shelf_life IS %L',
sch,
'批次保质期原文(如 7天/6个月/1年;单位 天/月/年)。可空;有效期 expiry_date 由 生产日期+保质期 推算'
);
END IF;
END LOOP;
END $$;
-- ============================================================
-- 2) 响亮失败守卫:public / tenant_template 的 inventory 上必须有该列
-- ============================================================
DO $$
BEGIN
IF to_regclass('public.inventory') IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'inventory' AND column_name = 'shelf_life'
) THEN
RAISE EXCEPTION '迁移未生效:public.inventory.shelf_life 不存在';
END IF;
IF to_regclass('tenant_template.inventory') IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_schema = 'tenant_template' AND table_name = 'inventory' AND column_name = 'shelf_life'
) THEN
RAISE EXCEPTION '迁移未生效:tenant_template.inventory.shelf_life 不存在';
END IF;
END $$;