57 lines
2.6 KiB
JavaScript
57 lines
2.6 KiB
JavaScript
// migrate-transfer-fg-stage.js
|
|
// One-time data fix for removing "Transfer to Finished Goods" from the
|
|
// Production Order workflow (services/productionOrderStore.js's STEPS/
|
|
// STEP_KEYS shortened from 5 steps to 4 — Release, Issuance, Receipt,
|
|
// Close). Stages 0-3 (nothing done .. Receipt done) keep the exact same
|
|
// numeric meaning either way, since Transfer to FG used to sit AFTER all of
|
|
// them. Only stage >= 4 (Transfer to FG done, and/or Closed) needs
|
|
// shifting down by 1 to land on the new, shorter numbering:
|
|
// old stage 4 (Transfer done, Close pending) -> new stage 3 (ready to Close)
|
|
// old stage 5 (Closed) -> new stage 4 (Closed)
|
|
//
|
|
// Usage:
|
|
// node migrate-transfer-fg-stage.js <companyDB> (dry run — shows what WOULD change)
|
|
// node migrate-transfer-fg-stage.js <companyDB> --apply (actually runs the UPDATE)
|
|
'use strict';
|
|
require('dotenv').config();
|
|
const { getPool } = require('./services/sqlPool');
|
|
|
|
async function main() {
|
|
const companyDB = process.argv[2];
|
|
const apply = process.argv.includes('--apply');
|
|
if (!companyDB) {
|
|
console.error('Usage: node migrate-transfer-fg-stage.js <companyDB> [--apply]');
|
|
process.exit(1);
|
|
}
|
|
|
|
const pool = await getPool(companyDB);
|
|
const chk = await pool.request().query(`SELECT COUNT(*) c FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='ZPRODUCTION_ORDERS'`);
|
|
if (!chk.recordset[0].c) {
|
|
console.log(`[${companyDB}] No ZPRODUCTION_ORDERS table — nothing to migrate.`);
|
|
process.exit(0);
|
|
}
|
|
|
|
const before = await pool.request().query(`SELECT STAGE, STATUS, COUNT(*) c FROM ZPRODUCTION_ORDERS WHERE STAGE>=4 GROUP BY STAGE, STATUS ORDER BY STAGE`);
|
|
console.log(`[${companyDB}] Rows with STAGE>=4 (will shift down by 1):`);
|
|
console.log(JSON.stringify(before.recordset, null, 1));
|
|
if (!before.recordset.length) {
|
|
console.log(`[${companyDB}] Nothing to migrate — no rows with STAGE>=4.`);
|
|
process.exit(0);
|
|
}
|
|
|
|
if (!apply) {
|
|
console.log(`[${companyDB}] DRY RUN — no changes made. Re-run with --apply to actually update.`);
|
|
process.exit(0);
|
|
}
|
|
|
|
const result = await pool.request().query(`UPDATE ZPRODUCTION_ORDERS SET STAGE = STAGE - 1 WHERE STAGE >= 4`);
|
|
console.log(`[${companyDB}] ✅ Updated ${result.rowsAffected[0]} row(s).`);
|
|
|
|
const after = await pool.request().query(`SELECT STAGE, STATUS, COUNT(*) c FROM ZPRODUCTION_ORDERS GROUP BY STAGE, STATUS ORDER BY STAGE`);
|
|
console.log(`[${companyDB}] Full stage distribution after migration:`);
|
|
console.log(JSON.stringify(after.recordset, null, 1));
|
|
process.exit(0);
|
|
}
|
|
|
|
main().catch(e => { console.error('ERR', e.message); process.exit(1); });
|