fix(订单实收): 回填脚本按租户上下文执行
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s

避免 RLS 下跨 schema 回填被静默阻止。
This commit is contained in:
Ubuntu
2026-11-03 13:03:52 +08:00
parent 42c5824646
commit d487e5525c
@@ -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<void>) => {
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();