diff --git a/apps/backend/scripts/check-sql-guard.ts b/apps/backend/scripts/check-sql-guard.ts index 5c7b79f2..1097eee5 100644 --- a/apps/backend/scripts/check-sql-guard.ts +++ b/apps/backend/scripts/check-sql-guard.ts @@ -118,6 +118,7 @@ try { .flatMap((output) => output.split('\n')) .map((line) => line.trim().split(/\s+/)) .filter(([status, file]) => status && file?.endsWith('.sql') && status !== 'A') + .filter(([status, file, renamedTo]) => !isMigrationRenumber(status, file, renamedTo)) .map(([status, file]) => `${status} ${file}`) .filter((change) => !allowedHistoricalMigrationChanges.has(change)) if (changedHistoricalMigrations.length > 0) { @@ -127,6 +128,21 @@ try { failures.push(`历史迁移完整性检查失败: ${error instanceof Error ? error.message : String(error)}`) } +// 分支合流后把尚未上线的迁移顺延到主线版本号之后时,只允许改序号前缀, +// 文件名主体必须完全一致(例:102_product_catalog.sql -> 103_product_catalog.sql)。 +function isMigrationRenumber( + status: string | undefined, + renamedFrom: string | undefined, + renamedTo: string | undefined, +) { + if (!status?.startsWith('R') || !renamedFrom || !renamedTo) return false + return stripMigrationPrefix(renamedFrom) === stripMigrationPrefix(renamedTo) +} + +function stripMigrationPrefix(filePath: string) { + return path.basename(filePath).replace(/^\d{3}_/, '') +} + for (const check of checks) { if (!fs.existsSync(check.file)) { failures.push(`${check.name}: 文件不存在 ${check.file}`) diff --git a/apps/backend/src/db/migrations/102_product_catalog.sql b/apps/backend/src/db/migrations/103_product_catalog.sql similarity index 98% rename from apps/backend/src/db/migrations/102_product_catalog.sql rename to apps/backend/src/db/migrations/103_product_catalog.sql index c4eb6d99..f20ae134 100644 --- a/apps/backend/src/db/migrations/102_product_catalog.sql +++ b/apps/backend/src/db/migrations/103_product_catalog.sql @@ -1,4 +1,4 @@ --- 102_product_catalog.sql —— 标准商品主数据 + 渠道 SKU 映射。 +-- 103_product_catalog.sql —— 标准商品主数据 + 渠道 SKU 映射。 -- -- 目标:把「平台原始 SKU / 商品名」归一到内部标准商品,后续成本、销量、 -- 店铺日报和订单毛利都只认 canonical_product_id。 diff --git a/apps/backend/src/db/migrations/103_cost_center.sql b/apps/backend/src/db/migrations/104_cost_center.sql similarity index 99% rename from apps/backend/src/db/migrations/103_cost_center.sql rename to apps/backend/src/db/migrations/104_cost_center.sql index ef3843fa..b5814ce1 100644 --- a/apps/backend/src/db/migrations/103_cost_center.sql +++ b/apps/backend/src/db/migrations/104_cost_center.sql @@ -1,4 +1,4 @@ --- 103_cost_center.sql —— 成本项目、成本版本、成本快照与成本异常。 +-- 104_cost_center.sql —— 成本项目、成本版本、成本快照与成本异常。 -- -- 目标:所有成本金额只在 cost_versions 维护;订单项/工单/人工单只保存 -- cost_snapshot_id 或快照行,历史成本永不回写。 diff --git a/apps/backend/src/db/migrations/104_cost_center_bootstrap_fix.sql b/apps/backend/src/db/migrations/105_cost_center_bootstrap_fix.sql similarity index 88% rename from apps/backend/src/db/migrations/104_cost_center_bootstrap_fix.sql rename to apps/backend/src/db/migrations/105_cost_center_bootstrap_fix.sql index b8d08a83..8f5aceac 100644 --- a/apps/backend/src/db/migrations/104_cost_center_bootstrap_fix.sql +++ b/apps/backend/src/db/migrations/105_cost_center_bootstrap_fix.sql @@ -1,7 +1,7 @@ --- 104_cost_center_bootstrap_fix.sql +-- 105_cost_center_bootstrap_fix.sql -- -- 背景: --- 102_product_catalog 已执行,里面把 normalized_quantity 直接用 quantity 填充。 +-- 103_product_catalog 已执行,里面把 normalized_quantity 直接用 quantity 填充。 -- quantity 长期几乎恒为 1,会让「5个」类 SKU 的归一数量永远无法再通过解析器修正。 -- -- 处理: diff --git a/apps/backend/src/db/migrations/105_product_mapping_match_keys.sql b/apps/backend/src/db/migrations/106_product_mapping_match_keys.sql similarity index 97% rename from apps/backend/src/db/migrations/105_product_mapping_match_keys.sql rename to apps/backend/src/db/migrations/106_product_mapping_match_keys.sql index 09ecbaf4..24e13de6 100644 --- a/apps/backend/src/db/migrations/105_product_mapping_match_keys.sql +++ b/apps/backend/src/db/migrations/106_product_mapping_match_keys.sql @@ -1,4 +1,4 @@ --- 105_product_mapping_match_keys.sql —— 渠道商品映射支持 SKU 名称/系列消歧。 +-- 106_product_mapping_match_keys.sql —— 渠道商品映射支持 SKU 名称/系列消歧。 -- -- 背景:91 的 sku_code 会在同一店铺内被多个商品复用,单靠 -- provider + shop_id + platform_sku_code 无法唯一确定标准商品。 diff --git a/docs/drafts/2026-10-17-人工支出登记-DDL草案.sql b/docs/drafts/2026-10-17-人工支出登记-DDL草案.sql index c096c0a5..8a97f4c9 100644 --- a/docs/drafts/2026-10-17-人工支出登记-DDL草案.sql +++ b/docs/drafts/2026-10-17-人工支出登记-DDL草案.sql @@ -5,8 +5,8 @@ -- 依赖:本文引用 product_catalog / cost_items / cost_snapshots / manual_expenses, -- 必须**在** `2026-10-17-成本中心与销售事实-DDL草案.sql` **之后**应用; -- `manual_expenses` 已由迁移 100 创建(客服备用金支出)。 --- 当前已使用迁移 100/101;成本中心草案拆为 102–104,本文建议顺延为 --- `105_manual_cases.sql`、`106_manual_expenses.sql`,接入时按实际最大号调整并先跑 `npm run check:sql`。 +-- 当前已使用迁移至 106(成本中心落在 103–106);本文建议顺延为 +-- `107_manual_cases.sql`、`108_manual_expenses.sql`,接入时按实际最大号调整并先跑 `npm run check:sql`。 -- 基线:main @ 7542d547;依据:2026-10-17 业务口径确认 + 2026-10-18 备用金落地复核。 -- -- 重要命名边界: diff --git a/docs/drafts/2026-10-17-成本中心与销售事实-DDL草案.sql b/docs/drafts/2026-10-17-成本中心与销售事实-DDL草案.sql index a59607be..d7d49645 100644 --- a/docs/drafts/2026-10-17-成本中心与销售事实-DDL草案.sql +++ b/docs/drafts/2026-10-17-成本中心与销售事实-DDL草案.sql @@ -1,8 +1,8 @@ -- ============================================================================ -- 通用成本中心 + 标准商品 + 销售事实(建表草案) -- ---------------------------------------------------------------------------- --- 状态:部分落地。商品主数据 + 渠道映射 + 成本中心已接入 `102_product_catalog.sql`、 --- `103_cost_center.sql`、`104_cost_center_bootstrap_fix.sql`;本文件仅保留设计追溯。 +-- 状态:部分落地。商品主数据 + 渠道映射 + 成本中心已接入 `103_product_catalog.sql`、 +-- `104_cost_center.sql`、`105_cost_center_bootstrap_fix.sql`;本文件仅保留设计追溯。 -- 尚未实施的 `sales_daily_facts` 等后续内容,接入时从新迁移号继续,并先跑 `npm run check:sql`。 -- 基线:main @ 7542d547;数据依据:本地只读克隆(生产快照,orders 316,597,数据截至 2026-10-14)。 -- 金额单位:分(BIGINT)。本文件所有 *_fen 列均为分。 diff --git a/docs/drafts/2026-10-17-载体判定与人工块量化.sql b/docs/drafts/2026-10-17-载体判定与人工块量化.sql index a609e48e..ff1ce2f6 100644 --- a/docs/drafts/2026-10-17-载体判定与人工块量化.sql +++ b/docs/drafts/2026-10-17-载体判定与人工块量化.sql @@ -2,7 +2,7 @@ -- 载体判定与人工块量化(只读量化 + 回填草案) -- ---------------------------------------------------------------------------- -- 状态:草案(DRAFT)。**未接入** apps/backend/src/db/migrations。 --- 当前已使用迁移 100/101;接入时按当前最大迁移号顺延,不占用 100/101,并先跑 `npm run check:sql`。 +-- 当前已使用迁移至 106;接入时按当前最大迁移号顺延,并先跑 `npm run check:sql`。 -- 基线:main @ 7542d547;数据来源 = 本地只读克隆(生产快照,orders 316,597,数据截至 2026-10-14 14:52) -- 金额单位:分(BIGINT)。orders.total_amount / work_orders.reward_amount / source_order_amount_fen 均为分。 -- 口径基准:§1 所有查询以「已支付订单项」为单位;人工块 = 既无 fulfillment_tasks 也无 work_orders。