Files
SupplierSystem/migrations/20260915_tenant_business_tables_assert.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

48 lines
2.3 KiB
PL/PgSQL
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.
-- 租户表清单守卫:把「模板 ⊆ 克隆名单」断言抽成可复用函数,每次迁移运行末尾自动调用
-- 设计:docs/design/20260915-tenant-table-list-guard.md
-- 背景:2026-09-10 新建 BR004 时 tenant_business_tables() 只有 93 个表名、模板已有 104 张,
-- 导致 BR004 少 3 张表(import_mapping_memories / inventory_reservations / supplier_price_agreements)。
-- 原断言只在 20260910_tenant_clone_missing_tables.sql 里跑过一次,之后靠人记。
--
-- 新增业务表的标准动作(缺一不可):
-- ① 迁移里建表(tenant_template + 各租户 schema)
-- ② 同步本函数依赖的名单:CREATE OR REPLACE FUNCTION public.tenant_business_tables()
-- ③ 迁移末尾调用 public.ensure_tenant_schemas_match_template() 给已有分公司补建
-- ④ 断言自动校验(本迁移建立后由 migrate.js 在每次迁移末尾执行;也可手工 SELECT)
CREATE OR REPLACE FUNCTION public.assert_tenant_template_matches_business_tables()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_leftover text;
BEGIN
-- 新库/未初始化模板:不阻断
IF to_regclass('tenant_template.orders') IS NULL
AND to_regclass('tenant_template.product_catalog') IS NULL THEN
RAISE NOTICE 'tenant_template 不存在或为空,跳过 tenant_business_tables() 校验';
RETURN;
END IF;
SELECT string_agg(c.relname, ', ' ORDER BY c.relname)
INTO v_leftover
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname = 'tenant_template'
AND NOT (c.relname = ANY (public.tenant_business_tables()));
IF v_leftover IS NOT NULL THEN
RAISE EXCEPTION
'tenant_template 仍有未列入 tenant_business_tables() 的表: %(新增表未同步名单 → 新建分公司会缺表)',
v_leftover;
END IF;
END;
$$;
COMMENT ON FUNCTION public.assert_tenant_template_matches_business_tables() IS
'校验 tenant_template 的普通表全部列入 tenant_business_tables();未列入即抛错(新增表必须同步名单)。见 docs/design/20260915-tenant-table-list-guard.md';
-- 自检:本迁移在事务内执行,失败则回滚且不写入 schema_migrations
SELECT public.assert_tenant_template_matches_business_tables();