← back to Dw Boardroom V2

src/db/migrate.ts

38 lines

// ═══════════════════════════════════════════════
// DW Boardroom V2 — SQLite Migration Runner
// ═══════════════════════════════════════════════

import fs from 'fs';
import path from 'path';
import { exec, getDb } from './sqlite';

export function runMigration(): void {
  const schemaPath = path.join(__dirname, '..', '..', 'src', 'db', 'schema.sql');

  // When running from dist/, adjust path
  const resolvedPath = fs.existsSync(schemaPath)
    ? schemaPath
    : path.join(__dirname, '..', '..', '..', 'src', 'db', 'schema.sql');

  if (!fs.existsSync(resolvedPath)) {
    throw new Error(`Schema file not found at ${schemaPath} or ${resolvedPath}`);
  }

  const schema = fs.readFileSync(resolvedPath, 'utf-8');
  exec(schema);

  // Verify tables exist
  const tables = getDb()
    .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'br2_%'")
    .all() as { name: string }[];

  console.log(`[migrate] Created/verified ${tables.length} tables:`, tables.map(t => t.name).join(', '));
}

// Run directly if called as script
if (require.main === module) {
  runMigration();
  console.log('[migrate] Done.');
  process.exit(0);
}