#!/usr/bin/env node // Regenerate setup_sql/schema.generated.sql — a readable snapshot of the current // pf schema, for reference only. Migrations are the source of truth; this file is // generated so it cannot drift from the database the way a hand-maintained // schema file does. Never edit it, and never apply it to create a database. require('dotenv').config(); const { spawnSync } = require('child_process'); const fs = require('fs'); const path = require('path'); const out = path.join(__dirname, '..', 'setup_sql', 'schema.generated.sql'); const result = spawnSync('pg_dump', [ '--schema-only', '--no-owner', '--no-privileges', '--schema=pf', // per-version forecast tables are created at runtime — including them would // make this file churn every time a version is created or dropped '--exclude-table=pf.fc_*', '-f', out ], { stdio: 'inherit', env: { ...process.env, PGHOST: process.env.DB_HOST, PGPORT: process.env.DB_PORT || '5432', PGDATABASE: process.env.DB_NAME, PGUSER: process.env.DB_USER, PGPASSWORD: process.env.DB_PASSWORD } }); if (result.error) { console.error(`pg_dump failed: ${result.error.message}`); process.exit(1); } if (result.status !== 0) process.exit(result.status); // Strip the lines pg_dump varies between runs — a random \restrict token and the // server/client version banner — so regenerating an unchanged schema produces an // identical file instead of a spurious diff. const cleaned = fs.readFileSync(out, 'utf8') .split('\n') .filter(l => !/^\\(un)?restrict /.test(l)) .filter(l => !/^-- Dumped (from|by) /.test(l)) .join('\n') .replace(/\n{3,}/g, '\n\n'); fs.writeFileSync(out, cleaned); console.log(`wrote ${path.relative(process.cwd(), out)} (${cleaned.split('\n').length} lines)`);