Files
SupplierSystem/migrations/20261061_tenant_business_tables_drift.sql
T
Ubuntu 5fa2bbfd55
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(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

213 lines
7.2 KiB
PL/PgSQL
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.
-- T4 / DATA-5(2026-10-19 T4):tenant_business_tables() 名单漂移 + 双向守卫。
--
-- 现状(迁移前):名单 119 项 —— 缺 main_system_sku_mappings(20261018 之前)、
-- 且含两张幽灵表:quote_sheet_change_requests(已被 20261020 主动 DROP)、
-- supplier_price_agreements(全库不存在、本仓库无代码引用)。
-- 20261018 之后名单已补 msm,但仍残留两个幽灵名 ⇒ 新建分公司克隆会去找不存在的表。
--
-- 本迁移:
-- ① 重发名单:117 项 = 模板当前 117 张表(补 msm、删两个幽灵名);
-- ② 重定义 assert_tenant_template_matches_business_tables() 为**双向**守卫:
-- 模板 ⊆ 名单(旧) + 名单 ⊆ 模板(新,新装库漏登记/多登记都中断迁移);
-- ③ 末尾先 ensure_tenant_schemas_match_template() 给已有租户补表,再跑断言。
-- 幂等:CREATE OR REPLACE + 迁移末尾 ensure/assert 均幂等;不改文件名/顺序。
CREATE OR REPLACE FUNCTION public.tenant_business_tables()
RETURNS TEXT[]
LANGUAGE sql
IMMUTABLE
AS $$
SELECT ARRAY[
'main_system_sku_mappings',
'after_sale_returns',
'after_sales',
'ai_prompt_overrides',
'ai_prompt_revisions',
'ai_prompt_templates',
'alias_recognition_logs',
'allocation_tasks',
'assistant_conversations',
'assistant_messages',
'assistant_turns',
'bid_awards',
'bid_invitations',
'bid_prices',
'bid_quotes',
'bid_round_items',
'bid_rounds',
'categories',
'cost_adjustments',
'cost_details',
'customer_alias_stats',
'customer_delivery_assignments',
'customer_portal_users',
'customer_processing_aliases',
'customer_quote_sheet_customers',
'customer_quote_sheet_items',
'customer_quote_sheets',
'customer_quote_versions',
'customer_recipients',
'customer_sku_aliases',
'customer_sku_remarks',
'customer_skus',
'customer_statements',
'customers',
'delivery_location_history',
'delivery_person_locations',
'delivery_person_users',
'delivery_persons',
'delivery_schedules',
'delivery_signatures',
'delivery_tasks',
'exception_tickets',
'fixed_costs',
'free_cost_invoice_links',
'free_cost_invoices',
'free_cost_payment_vouchers',
'import_mapping_memories',
'inspection_loss_confirmations',
'inspection_photo_archive_entries',
'inspection_photo_archives',
'inspection_returns',
'inspection_review_logs',
'inspections',
'inventory',
'inventory_batch_report_matches',
'inventory_batch_reports',
'inventory_logs',
'inventory_reservations',
'invoice_statement_links',
'invoices',
'market_compare_exclusions',
'market_external_catalog',
'market_price_history',
'market_price_indices',
'market_prices',
'market_sku_external_bindings',
'notifications',
'operation_logs',
'order_allocation_batches',
'order_item_fulfillments',
'order_item_returns',
'order_items',
'orders',
'parse_session_items',
'parse_sessions',
'picking_assign_rules',
'picking_containers',
'picking_items',
'picking_scan_logs',
'picking_tasks',
'price_invert_events',
'print_jobs',
'processing_demand_items',
'processing_demand_picks',
'processing_logs',
'processing_tasks',
'product_catalog',
'product_substitutions',
'purchase_orders',
'purchaser_users',
'quality_report_prep_links',
'quality_reports',
'quote_price_proposals',
'seasonal_recommendations',
'self_purchase_stock_inbounds',
'sign_records',
'sku_direct_delivery_customers',
'sku_suppliers',
'skus',
'smart_classification_rules',
'statement_disputes',
'statement_lines',
'statement_payments',
'supplier_categories',
'supplier_prep_amend_requests',
'supplier_preparations',
'supplier_rating_snapshots',
'supplier_statements',
'supplier_users',
'suppliers',
'system_configs',
'unit_conversions',
'user_preferences',
'voice_recognition_logs',
'warehouse_locations',
'warehouses',
'weigh_devices'
]
$$;
COMMENT ON FUNCTION public.tenant_business_tables() IS
'租户 schema 业务表名单(117 张,= tenant_template 表集合;新增/删除表必须同步本函数,末尾双向断言会中断迁移)。';
CREATE OR REPLACE FUNCTION public.assert_tenant_template_matches_business_tables()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_leftover text;
v_missing_on_template 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;
-- ② 名单 ⊆ 模板(新方向):名单里有、模板却没有 ⇒ 幽灵项/漏建表,克隆会失败
SELECT string_agg(t, ', ' ORDER BY t)
INTO v_missing_on_template
FROM unnest(public.tenant_business_tables()) AS t
WHERE to_regclass(format('tenant_template.%I', t)) IS NULL;
IF v_missing_on_template IS NOT NULL THEN
RAISE EXCEPTION
'tenant_business_tables() 含 tenant_template 不存在的表: %(幽灵项或漏建模板表)',
v_missing_on_template;
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';
-- 兼容显式命名(AC-T4.3.3):反向守卫也可单独调用
CREATE OR REPLACE FUNCTION public.assert_business_tables_match_tenant_template()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM public.assert_tenant_template_matches_business_tables();
END;
$$;
-- 给已有租户补模板表(msm 已存在则为 no-op),再跑双向断言。
-- DO 块形式:与其它迁移一致,便于幂等重放/集中报错(函数本身幂等)。
-- P1(T4 红队复攻,2026-10-19):顺序必须是 **先双向 assert、后 ensure**!
-- 若先 ensure:ensure 会把"名单有、template 缺、public 有"的平台表克隆进 template,
-- 随后的反向断言看到 template 已含该表 ⇒ 多登记/幽灵项被漏检(红线)。
DO $$
BEGIN
PERFORM public.assert_tenant_template_matches_business_tables();
PERFORM public.ensure_tenant_schemas_match_template();
-- ensure 后再断言一次:确保补表后仍严格一致(补表不应引入漂移)
PERFORM public.assert_tenant_template_matches_business_tables();
END $$;