来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
289 lines
8.9 KiB
PL/PgSQL
289 lines
8.9 KiB
PL/PgSQL
-- 补迁阶段 5 遗漏业务表:order_item_fulfillments 等
|
||
-- 根因:表留 public,FK 指向 legacy_public.order_items,ALS 新明细在 tenant_* → FK 失败
|
||
|
||
-- 1) 扩展业务表清单
|
||
CREATE OR REPLACE FUNCTION public.tenant_business_tables()
|
||
RETURNS TEXT[]
|
||
LANGUAGE sql
|
||
IMMUTABLE
|
||
AS $$
|
||
SELECT ARRAY[
|
||
'after_sales',
|
||
'alias_recognition_logs',
|
||
'allocation_tasks',
|
||
'bid_prices',
|
||
'categories',
|
||
'cost_adjustments',
|
||
'cost_details',
|
||
'customer_delivery_assignments',
|
||
'customer_image_aliases',
|
||
'customer_quote_versions',
|
||
'customer_quote_sheets',
|
||
'customer_quote_sheet_items',
|
||
'customer_quote_sheet_customers',
|
||
'customer_sku_aliases',
|
||
'customer_processing_aliases',
|
||
'customer_skus',
|
||
'customer_statements',
|
||
'statement_lines',
|
||
'statement_disputes',
|
||
'statement_payments',
|
||
'customers',
|
||
'delivery_person_users',
|
||
'delivery_persons',
|
||
'delivery_person_locations',
|
||
'delivery_location_history',
|
||
'delivery_tasks',
|
||
'exception_tickets',
|
||
'fixed_costs',
|
||
'inspections',
|
||
'inspection_loss_confirmations',
|
||
'inspection_returns',
|
||
'inspection_review_logs',
|
||
'inventory',
|
||
'inventory_logs',
|
||
'invoices',
|
||
'notifications',
|
||
'operation_logs',
|
||
'order_item_fulfillments',
|
||
'order_items',
|
||
'orders',
|
||
'picking_assign_rules',
|
||
'picking_items',
|
||
'picking_tasks',
|
||
'picking_containers',
|
||
'picking_scan_logs',
|
||
'processing_logs',
|
||
'processing_tasks',
|
||
'product_catalog',
|
||
'purchase_orders',
|
||
'purchaser_users',
|
||
'sign_records',
|
||
'sku_aliases',
|
||
'sku_suppliers',
|
||
'skus',
|
||
'smart_classification_rules',
|
||
'supplier_categories',
|
||
'supplier_preparations',
|
||
'supplier_statements',
|
||
'supplier_rating_snapshots',
|
||
'product_substitutions',
|
||
'delivery_signatures',
|
||
'customer_recipients',
|
||
'customer_portal_users',
|
||
'market_prices',
|
||
'market_compare_exclusions',
|
||
'market_price_indices',
|
||
'seasonal_recommendations',
|
||
'order_allocation_batches',
|
||
'delivery_schedules',
|
||
'supplier_users',
|
||
'suppliers',
|
||
'system_configs',
|
||
'warehouses',
|
||
'warehouse_locations'
|
||
]::text[];
|
||
$$;
|
||
|
||
-- 2) 补齐各 tenant / template 缺表结构
|
||
SELECT public.ensure_tenant_schemas_match_template();
|
||
|
||
-- 3) 按父表归属迁入各 active/pending tenant
|
||
DO $$
|
||
DECLARE
|
||
r RECORD;
|
||
sch TEXT;
|
||
n INTEGER;
|
||
BEGIN
|
||
FOR r IN
|
||
SELECT branch_code, schema_name
|
||
FROM public.tenant_schema_registry
|
||
ORDER BY branch_code
|
||
LOOP
|
||
sch := r.schema_name;
|
||
IF sch IS NULL OR sch !~ '^tenant_[a-z0-9_-]{2,30}$' THEN
|
||
CONTINUE;
|
||
END IF;
|
||
|
||
-- order_item_fulfillments ← 本 tenant 的 order_items
|
||
IF to_regclass(format('%I.order_item_fulfillments', sch)) IS NOT NULL
|
||
AND to_regclass('public.order_item_fulfillments') IS NOT NULL THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.order_item_fulfillments
|
||
SELECT f.*
|
||
FROM public.order_item_fulfillments f
|
||
WHERE EXISTS (
|
||
SELECT 1 FROM %I.order_items oi WHERE oi.id = f.order_item_id
|
||
)
|
||
ON CONFLICT DO NOTHING',
|
||
sch, sch
|
||
);
|
||
GET DIAGNOSTICS n = ROW_COUNT;
|
||
RAISE NOTICE 'migrated order_item_fulfillments -> % : %', sch, n;
|
||
END IF;
|
||
|
||
-- statement_payments(有 branch_code)
|
||
IF to_regclass(format('%I.statement_payments', sch)) IS NOT NULL
|
||
AND to_regclass('public.statement_payments') IS NOT NULL THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.statement_payments
|
||
SELECT * FROM public.statement_payments
|
||
WHERE branch_code IS NOT DISTINCT FROM %L
|
||
ON CONFLICT DO NOTHING',
|
||
sch, r.branch_code
|
||
);
|
||
GET DIAGNOSTICS n = ROW_COUNT;
|
||
RAISE NOTICE 'migrated statement_payments -> % : %', sch, n;
|
||
END IF;
|
||
|
||
-- delivery_person_locations
|
||
IF to_regclass(format('%I.delivery_person_locations', sch)) IS NOT NULL
|
||
AND to_regclass('public.delivery_person_locations') IS NOT NULL
|
||
AND to_regclass(format('%I.delivery_persons', sch)) IS NOT NULL THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.delivery_person_locations
|
||
SELECT t.*
|
||
FROM public.delivery_person_locations t
|
||
WHERE EXISTS (
|
||
SELECT 1 FROM %I.delivery_persons d WHERE d.person_code = t.person_code
|
||
)
|
||
ON CONFLICT DO NOTHING',
|
||
sch, sch
|
||
);
|
||
GET DIAGNOSTICS n = ROW_COUNT;
|
||
RAISE NOTICE 'migrated delivery_person_locations -> % : %', sch, n;
|
||
END IF;
|
||
|
||
-- delivery_location_history(若有 person_code)
|
||
IF to_regclass(format('%I.delivery_location_history', sch)) IS NOT NULL
|
||
AND to_regclass('public.delivery_location_history') IS NOT NULL
|
||
AND to_regclass(format('%I.delivery_persons', sch)) IS NOT NULL
|
||
AND EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = 'public' AND table_name = 'delivery_location_history'
|
||
AND column_name = 'person_code'
|
||
) THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.delivery_location_history
|
||
SELECT t.*
|
||
FROM public.delivery_location_history t
|
||
WHERE EXISTS (
|
||
SELECT 1 FROM %I.delivery_persons d WHERE d.person_code = t.person_code
|
||
)
|
||
ON CONFLICT DO NOTHING',
|
||
sch, sch
|
||
);
|
||
GET DIAGNOSTICS n = ROW_COUNT;
|
||
RAISE NOTICE 'migrated delivery_location_history -> % : %', sch, n;
|
||
END IF;
|
||
|
||
-- picking_assign_rules(通常有 branch_code)
|
||
IF to_regclass(format('%I.picking_assign_rules', sch)) IS NOT NULL
|
||
AND to_regclass('public.picking_assign_rules') IS NOT NULL THEN
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = 'public' AND table_name = 'picking_assign_rules'
|
||
AND column_name = 'branch_code'
|
||
) THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.picking_assign_rules
|
||
SELECT * FROM public.picking_assign_rules
|
||
WHERE branch_code IS NOT DISTINCT FROM %L
|
||
ON CONFLICT DO NOTHING',
|
||
sch, r.branch_code
|
||
);
|
||
ELSE
|
||
EXECUTE format(
|
||
'INSERT INTO %I.picking_assign_rules
|
||
SELECT * FROM public.picking_assign_rules
|
||
ON CONFLICT DO NOTHING',
|
||
sch
|
||
);
|
||
END IF;
|
||
GET DIAGNOSTICS n = ROW_COUNT;
|
||
RAISE NOTICE 'migrated picking_assign_rules -> % : %', sch, n;
|
||
END IF;
|
||
|
||
-- inspection_review_logs(有 branch_code)
|
||
IF to_regclass(format('%I.inspection_review_logs', sch)) IS NOT NULL
|
||
AND to_regclass('public.inspection_review_logs') IS NOT NULL THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.inspection_review_logs
|
||
SELECT * FROM public.inspection_review_logs
|
||
WHERE branch_code IS NOT DISTINCT FROM %L
|
||
ON CONFLICT DO NOTHING',
|
||
sch, r.branch_code
|
||
);
|
||
GET DIAGNOSTICS n = ROW_COUNT;
|
||
RAISE NOTICE 'migrated inspection_review_logs -> % : %', sch, n;
|
||
END IF;
|
||
|
||
-- warehouses / warehouse_locations
|
||
IF to_regclass(format('%I.warehouses', sch)) IS NOT NULL
|
||
AND to_regclass('public.warehouses') IS NOT NULL THEN
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = 'public' AND table_name = 'warehouses' AND column_name = 'branch_code'
|
||
) THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.warehouses SELECT * FROM public.warehouses
|
||
WHERE branch_code IS NOT DISTINCT FROM %L ON CONFLICT DO NOTHING',
|
||
sch, r.branch_code
|
||
);
|
||
ELSE
|
||
EXECUTE format(
|
||
'INSERT INTO %I.warehouses SELECT * FROM public.warehouses ON CONFLICT DO NOTHING',
|
||
sch
|
||
);
|
||
END IF;
|
||
END IF;
|
||
|
||
IF to_regclass(format('%I.warehouse_locations', sch)) IS NOT NULL
|
||
AND to_regclass('public.warehouse_locations') IS NOT NULL
|
||
AND to_regclass(format('%I.warehouses', sch)) IS NOT NULL THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.warehouse_locations
|
||
SELECT wl.* FROM public.warehouse_locations wl
|
||
WHERE EXISTS (
|
||
SELECT 1 FROM %I.warehouses w WHERE w.warehouse_code = wl.warehouse_code
|
||
)
|
||
ON CONFLICT DO NOTHING',
|
||
sch, sch
|
||
);
|
||
END IF;
|
||
END LOOP;
|
||
END;
|
||
$$;
|
||
|
||
-- 4) 归档 public 遗漏表到 legacy_public
|
||
DO $$
|
||
DECLARE
|
||
t TEXT;
|
||
missing TEXT[] := ARRAY[
|
||
'order_item_fulfillments',
|
||
'statement_payments',
|
||
'delivery_person_locations',
|
||
'delivery_location_history',
|
||
'picking_assign_rules',
|
||
'inspection_review_logs',
|
||
'warehouses',
|
||
'warehouse_locations'
|
||
];
|
||
BEGIN
|
||
FOREACH t IN ARRAY missing
|
||
LOOP
|
||
IF to_regclass(format('public.%I', t)) IS NULL THEN
|
||
CONTINUE;
|
||
END IF;
|
||
IF to_regclass(format('legacy_public.%I', t)) IS NOT NULL THEN
|
||
EXECUTE format('DROP TABLE legacy_public.%I CASCADE', t);
|
||
END IF;
|
||
EXECUTE format('ALTER TABLE public.%I SET SCHEMA legacy_public', t);
|
||
RAISE NOTICE 'archived public.%s to legacy_public', t;
|
||
END LOOP;
|
||
END;
|
||
$$;
|
||
|
||
-- 5) 再 ensure 一次(template 若此前无这些表,从 legacy 补)
|
||
SELECT public.ensure_tenant_schemas_match_template();
|