#!/usr/bin/env node // Re-apply a backup file produced by backup-view.cjs (or the rollback file) inside one transaction. // // node reference/sql/restore-view.cjs reference/sql/backups/v_orders_summary..sql // // CREATE OR REPLACE VIEW is instant and needs no iDempiere restart. const fs = require('fs') const path = require('path') const { SCHEMA, VIEW, connect } = require('./_db.cjs') ;(async () => { const file = process.argv[2] if (!file) { console.error('usage: restore-view.cjs '); process.exit(2) } const sql = fs.readFileSync(path.resolve(file), 'utf8') if (!sql.includes(`CREATE OR REPLACE VIEW ${SCHEMA}.${VIEW} AS`)) throw new Error('file does not contain the expected CREATE OR REPLACE VIEW') const { client } = await connect() try { await client.query('BEGIN') await client.query(sql) const probe = await client.query(`SELECT count(*) AS n FROM ${SCHEMA}.${VIEW} WHERE issotrx = 'Y' AND created >= now() - interval '7 days'`) await client.query('COMMIT') console.log(`restored ${SCHEMA}.${VIEW} from ${file} (probe: ${probe.rows[0].n} SO rows in the last 7 days)`) } catch (e) { await client.query('ROLLBACK').catch(() => {}) throw e } finally { await client.end() } })().catch(e => { console.error('restore failed:', e.message); process.exit(1) })