From d487e5525cfa16e314545bbb7f828fd6a265abca Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 3 Nov 2026 13:03:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=AE=A2=E5=8D=95=E5=AE=9E=E6=94=B6):=20?= =?UTF-8?q?=E5=9B=9E=E5=A1=AB=E8=84=9A=E6=9C=AC=E6=8C=89=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免 RLS 下跨 schema 回填被静默阻止。 --- .../backfill-order-item-received-qty.ts | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) 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();