开发者裁问:「为什么这里面会存在这个华东分公司?」并裁定:「首先是删除;然后看看为什么会存在 ——
不能下次我到新机器跑时就直接存在这个分公司,要治根;如果是历史存在这个迁移指令,就把历史的那条删除、整理好。」
根因(证据):它是**建库基础迁移里写死的演示数据**,与任何主系统供应商无关 ——
· `migrations/20260607_create_branch_tenant.sql`:`INSERT INTO branches VALUES ('BR001','默认分公司',…),
('BR002','华东分公司','华东负责人','4000000002','上海市浦东新区示例路100号')`;
· `migrations/20260608_orders_branch_isolation.sql`:又插一次 `('BR001','默认分公司'),('BR002','华东分公司')`。
页面里那行的 4000000002 / 华东负责人 / 上海市浦东新区示例路100号 与迁移字面值**逐字一致**。
「华东分公司」是**主系统自己的经营单元名**;本系统「分公司 = 主系统的供应商」(编码 SUPxxxxx,由开通流程创建)
⇒ 这行既无供应商对应、也无业务数据,纯属建库残留。
治根(仓库):
· 两处 BR002 INSERT **删除**,原处留注释说明为什么不再插入(新机器建库不会再出现);
· `seed-demo-data.ts` 演示客户 CUST003 由 BR002 改挂 BR001(否则演示种子撞 branches 外键);
· **BR001「默认分公司」保留**:`PLATFORM_BRANCH_CODE` 默认值 + 演示账号(admin/operator/finance/warehouse/delivery)挂它;
· `20261009_br002_*` 两条**业务数据**迁移不动(有 to_regclass 守卫,无 tenant_br002 时空转,不是"建分公司")。
清理(本地库,已执行):
· 删 BR002:`public.branches` 1 行 + 全部引用行(`legacy_public.categories` 15 行)+ 租户注册行 + `tenant_br002` schema;
`purchaser1` 演示账号改挂 BR001;
· 顺带删掉随"主系统结构"拷进来、**未注册**的孤儿租户 schema `tenant_br003..007`(空)。
现在:`branches` 只剩 BR001;租户 schema 只剩 `tenant_br001 + tenant_template`;7 个演示账号可登录(密码 123456)。
测试/门禁:`migrationReplayHygiene.test.ts` 4 绿;服务端全量失败集与基线**逐文件一致(24,零新增)**;
设计门禁 8 通过 / 1 未通过(check-ai-cost 基线红)/ 1 未执行(无 psql),`check-migrations.py` ✅。
文档:`docs/design/20261010-分公司开通与主系统供应商绑定.md` 新增 §4.1(现象/根因/处置/验证)。
123 lines
5.8 KiB
TypeScript
123 lines
5.8 KiB
TypeScript
import type { Pool } from 'pg';
|
|
|
|
export async function seedDemoData(pool: Pool) {
|
|
const skuCount = await pool.query('SELECT COUNT(*)::int AS count FROM skus');
|
|
if ((skuCount.rows[0]?.count ?? 0) === 0) {
|
|
console.log('Seeding demo SKUs...');
|
|
await pool.query(`
|
|
INSERT INTO skus (sku_code, sku_name, category_code, unit, status) VALUES
|
|
('SKU001', '红富士苹果', 'FRUIT001', '斤', 1),
|
|
('SKU002', '土鸡蛋', 'MEAT001', '个', 1),
|
|
('SKU003', '土豆', 'VEG001', '斤', 1),
|
|
('SKU004', '赣南橙', 'FRUIT001', '箱', 1),
|
|
('SKU005', '大白菜', 'VEG001', '斤', 1)
|
|
ON CONFLICT (sku_code) DO NOTHING
|
|
`);
|
|
}
|
|
|
|
const customerCount = await pool.query('SELECT COUNT(*)::int AS count FROM customers');
|
|
if ((customerCount.rows[0]?.count ?? 0) === 0) {
|
|
console.log('Seeding demo customers...');
|
|
await pool.query(`
|
|
INSERT INTO customers (customer_code, company_name, contact_name, contact_phone, address, status, branch_code) VALUES
|
|
('CUST001', '阳光食堂', '张经理', '13800000001', '北京市朝阳区示范路1号', 10, 'BR001'),
|
|
('CUST002', '绿野学校食堂', '李老师', '13800000002', '北京市海淀区学院路88号', 10, 'BR001'),
|
|
('CUST003', '示范食堂三店', '周经理', '13800000003', '上海市浦东新区示例路66号', 10, 'BR001')
|
|
ON CONFLICT (customer_code) DO NOTHING
|
|
`);
|
|
}
|
|
|
|
const customerSkuCount = await pool.query('SELECT COUNT(*)::int AS count FROM customer_skus');
|
|
if ((customerSkuCount.rows[0]?.count ?? 0) === 0) {
|
|
console.log('Seeding demo customer SKUs...');
|
|
await pool.query(`
|
|
INSERT INTO customer_skus (customer_code, sku_code, customer_sku_name, customer_unit, usage_count, is_favorite) VALUES
|
|
('CUST001', 'SKU001', '苹果', '斤', 12, true),
|
|
('CUST001', 'SKU002', '鸡蛋', '个', 8, false),
|
|
('CUST001', 'SKU003', '土豆', '斤', 5, false),
|
|
('CUST002', 'SKU001', '红富士', '斤', 6, true),
|
|
('CUST002', 'SKU004', '橙子', '箱', 3, false)
|
|
ON CONFLICT DO NOTHING
|
|
`).catch(async () => {
|
|
await pool.query(`
|
|
INSERT INTO customer_skus (customer_code, sku_code, customer_sku_name, customer_unit, usage_count, is_favorite)
|
|
SELECT v.customer_code, v.sku_code, v.customer_sku_name, v.customer_unit, v.usage_count, v.is_favorite
|
|
FROM (VALUES
|
|
('CUST001', 'SKU001', '苹果', '斤', 12, true),
|
|
('CUST001', 'SKU002', '鸡蛋', '个', 8, false),
|
|
('CUST001', 'SKU003', '土豆', '斤', 5, false),
|
|
('CUST002', 'SKU001', '红富士', '斤', 6, true),
|
|
('CUST002', 'SKU004', '橙子', '箱', 3, false)
|
|
) AS v(customer_code, sku_code, customer_sku_name, customer_unit, usage_count, is_favorite)
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM customer_skus cs
|
|
WHERE cs.customer_code = v.customer_code AND cs.sku_code = v.sku_code
|
|
)
|
|
`);
|
|
});
|
|
}
|
|
|
|
const supplierCount = await pool.query('SELECT COUNT(*)::int AS count FROM suppliers');
|
|
if ((supplierCount.rows[0]?.count ?? 0) === 0) {
|
|
console.log('Seeding demo suppliers...');
|
|
await pool.query(`
|
|
INSERT INTO suppliers (supplier_code, company_name, contact_name, contact_phone, rating, quota_percent, settlement_days, status) VALUES
|
|
('SUP001', '鲜丰果蔬供应链', '王总', '13900000001', 'A', 100, 30, 10),
|
|
('SUP002', '绿源禽蛋批发中心', '刘经理', '13900000002', 'A', 100, 15, 10),
|
|
('SUP003', '华北土豆种植基地', '赵场长', '13900000003', 'B', 80, 30, 10),
|
|
('SUP004', '赣南果业直销', '陈老板', '13900000004', 'B', 90, 20, 10)
|
|
ON CONFLICT (supplier_code) DO NOTHING
|
|
`);
|
|
}
|
|
|
|
const supplierCategoryCount = await pool.query('SELECT COUNT(*)::int AS count FROM supplier_categories');
|
|
if ((supplierCategoryCount.rows[0]?.count ?? 0) === 0) {
|
|
console.log('Seeding demo supplier categories...');
|
|
await pool.query(`
|
|
INSERT INTO supplier_categories (supplier_code, category_code, is_primary) VALUES
|
|
('SUP001', 'FRUIT001', true),
|
|
('SUP001', 'VEG001', false),
|
|
('SUP002', 'MEAT001', true),
|
|
('SUP003', 'VEG001', true),
|
|
('SUP004', 'FRUIT001', true)
|
|
ON CONFLICT DO NOTHING
|
|
`).catch(() => undefined);
|
|
}
|
|
|
|
const bidPriceCount = await pool.query('SELECT COUNT(*)::int AS count FROM bid_prices');
|
|
if ((bidPriceCount.rows[0]?.count ?? 0) === 0) {
|
|
console.log('Seeding demo bid prices...');
|
|
const now = new Date();
|
|
const cycleCode = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}`;
|
|
await pool.query(
|
|
`
|
|
INSERT INTO bid_prices (bid_code, cycle_code, sku_code, price, status) VALUES
|
|
('BID_DEMO', $1, 'SKU001', 5.50, 1),
|
|
('BID_DEMO', $1, 'SKU002', 1.20, 1),
|
|
('BID_DEMO', $1, 'SKU003', 2.00, 1),
|
|
('BID_DEMO', $1, 'SKU004', 38.00, 1),
|
|
('BID_DEMO', $1, 'SKU005', 1.80, 1)
|
|
ON CONFLICT (bid_code, cycle_code, sku_code) DO NOTHING
|
|
`,
|
|
[cycleCode]
|
|
);
|
|
}
|
|
|
|
const skuSupplierCount = await pool.query('SELECT COUNT(*)::int AS count FROM sku_suppliers');
|
|
if ((skuSupplierCount.rows[0]?.count ?? 0) === 0) {
|
|
console.log('Seeding demo SKU-supplier relations...');
|
|
await pool.query(`
|
|
INSERT INTO sku_suppliers (sku_code, supplier_code, supply_price, quality_rating, lead_time, min_order_qty, is_preferred, status) VALUES
|
|
('SKU001', 'SUP001', 4.50, 4.8, 1, 10, true, 1),
|
|
('SKU001', 'SUP004', 4.80, 4.5, 2, 5, false, 1),
|
|
('SKU002', 'SUP002', 1.20, 4.9, 1, 30, true, 1),
|
|
('SKU003', 'SUP003', 2.00, 4.6, 1, 20, true, 1),
|
|
('SKU003', 'SUP001', 2.20, 4.2, 2, 10, false, 1),
|
|
('SKU004', 'SUP004', 35.00, 4.7, 2, 2, true, 1),
|
|
('SKU005', 'SUP001', 1.50, 4.4, 1, 15, true, 1),
|
|
('SKU005', 'SUP003', 1.30, 4.3, 1, 10, false, 1)
|
|
ON CONFLICT (sku_code, supplier_code) DO NOTHING
|
|
`);
|
|
}
|
|
}
|