← back to Dw Boardroom Governance

src/db/migrate.ts

38 lines

import fs from 'fs';
import path from 'path';
import { pool } from './db';

async function migrate() {
  console.log('[Governance] Running database migration...');

  const schemaPath = path.join(__dirname, 'schema.sql');

  // When running via ts-node, __dirname points to src/db; when compiled, dist/db
  // Check both locations
  let sql: string;
  if (fs.existsSync(schemaPath)) {
    sql = fs.readFileSync(schemaPath, 'utf-8');
  } else {
    const altPath = path.join(__dirname, '..', '..', 'src', 'db', 'schema.sql');
    sql = fs.readFileSync(altPath, 'utf-8');
  }

  try {
    await pool.query(sql);
    console.log('[Governance] Migration complete — all gov_* tables ready');

    // Verify tables exist
    const result = await pool.query(
      "SELECT tablename FROM pg_tables WHERE schemaname='public' AND tablename LIKE 'gov_%' ORDER BY tablename"
    );
    console.log('[Governance] Tables:', result.rows.map(r => r.tablename).join(', '));
  } catch (err: any) {
    console.error('[Governance] Migration failed:', err.message);
    process.exit(1);
  } finally {
    await pool.end();
  }
}

migrate();