diff --git a/server/scripts/backfill-order-item-received-qty.ts b/server/scripts/backfill-order-item-received-qty.ts index a38a9d8..f05d029 100644 --- a/server/scripts/backfill-order-item-received-qty.ts +++ b/server/scripts/backfill-order-item-received-qty.ts @@ -11,6 +11,7 @@ * npx tsx --env-file=.env server/scripts/backfill-order-item-received-qty.ts */ import { pool } from '../src/db'; +import { runWithTenantDbContext } from '../src/services/tenantDbContext'; function quoteIdent(name: string): string { return `"${name.replace(/"/g, '""')}"`; @@ -33,6 +34,12 @@ async function main() { ); let total = 0; for (const { nspname } of schemas.rows) { + const branchMatch = /^tenant_(br\d+)$/i.exec(nspname); + const branchCode = branchMatch ? branchMatch[1].toUpperCase() : null; + const run = async (fn: () => Promise) => { + if (branchCode) await runWithTenantDbContext(branchCode, fn); + else await fn(); + }; if (!(await hasColumn(nspname, 'order_items', 'received_qty'))) { console.log(`skip ${nspname}: 缺少 received_qty(先迁移)`); continue; @@ -43,29 +50,33 @@ async function main() { const actualQty = hasActual ? 'actual_received_qty' : 'NULL'; const actualUnit = hasActual ? 'actual_received_unit' : 'NULL'; if (dryRun) { + await run(async () => { + const r = await pool.query( + `SELECT COUNT(*)::int AS n FROM ${table} + WHERE demand_qty IS NULL OR demand_unit IS NULL OR received_qty IS NULL + OR received_unit IS NULL OR received_stage IS NULL` + ); + const n = Number(r.rows[0]?.n) || 0; + total += n; + console.log(`${nspname}: 待回填 ${n} 行(dry-run)`); + }); + continue; + } + await run(async () => { const r = await pool.query( - `SELECT COUNT(*)::int AS n FROM ${table} + `UPDATE ${table} + SET demand_qty = COALESCE(demand_qty, ordered_quantity, quantity), + demand_unit = COALESCE(demand_unit, ordered_unit, unit), + received_qty = COALESCE(received_qty, CASE WHEN ${actualExpr} THEN ${actualQty} ELSE quantity END), + received_unit = COALESCE(received_unit, CASE WHEN ${actualExpr} THEN COALESCE(${actualUnit}, unit) ELSE unit END), + received_stage = COALESCE(received_stage, CASE WHEN ${actualExpr} THEN 30 ELSE 0 END) WHERE demand_qty IS NULL OR demand_unit IS NULL OR received_qty IS NULL OR received_unit IS NULL OR received_stage IS NULL` ); - const n = Number(r.rows[0]?.n) || 0; + const n = Number(r.rowCount || 0); total += n; - console.log(`${nspname}: 待回填 ${n} 行(dry-run)`); - continue; - } - const r = await pool.query( - `UPDATE ${table} - SET demand_qty = COALESCE(demand_qty, ordered_quantity, quantity), - demand_unit = COALESCE(demand_unit, ordered_unit, unit), - received_qty = COALESCE(received_qty, CASE WHEN ${actualExpr} THEN ${actualQty} ELSE quantity END), - received_unit = COALESCE(received_unit, CASE WHEN ${actualExpr} THEN COALESCE(${actualUnit}, unit) ELSE unit END), - received_stage = COALESCE(received_stage, CASE WHEN ${actualExpr} THEN 30 ELSE 0 END) - WHERE demand_qty IS NULL OR demand_unit IS NULL OR received_qty IS NULL - OR received_unit IS NULL OR received_stage IS NULL` - ); - const n = Number(r.rowCount || 0); - total += n; - console.log(`${nspname}: 回填 ${n} 行`); + console.log(`${nspname}: 回填 ${n} 行`); + }); } console.log(`[backfill-order-item-received-qty] done rows=${total}`); await pool.end();