#!/usr/bin/env node /** 回填 inventory.production_date(仅空值,取入库日中国时区) */ import path from 'path'; import { fileURLToPath, pathToFileURL } from 'url'; import { createRequire } from 'module'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const serverRoot = path.resolve(__dirname, '..'); const require = createRequire(path.join(serverRoot, 'package.json')); require('dotenv').config({ path: path.join(serverRoot, '.env') }); const execute = process.argv.includes('--execute'); const { listActiveTenantBranches, runInBranchSchema } = await import( pathToFileURL(path.join(serverRoot, 'dist/services/tenantSchemaFallback.js')).href ); const { pool } = await import(pathToFileURL(path.join(serverRoot, 'dist/db.js')).href); for (const t of await listActiveTenantBranches()) { await runInBranchSchema(t.branch_code, async () => { const before = await pool.query( `SELECT COUNT(*)::int AS c FROM inventory WHERE production_date IS NULL` ); console.log(t.branch_code, 'null production_date=', before.rows[0].c); if (!execute) return; const r = await pool.query(` UPDATE inventory SET production_date = (COALESCE(created_at, NOW()) AT TIME ZONE 'Asia/Shanghai')::date WHERE production_date IS NULL `); console.log(t.branch_code, 'updated=', r.rowCount); }); } if (!execute) console.log('[dry-run] 加 --execute 写库'); await pool.end();