← back to Lawyer Directory Builder
src/scripts/migrate.ts
45 lines
import 'dotenv/config';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { pool } from '../db/pool.ts';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const migrationsDir = path.resolve(__dirname, '../../migrations');
async function main() {
// Ensure schema_migrations tracking table.
await pool.query(`
CREATE TABLE IF NOT EXISTS schema_migrations (
filename TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
`);
const files = fs.readdirSync(migrationsDir).filter(f => f.endsWith('.sql')).sort();
for (const file of files) {
const exists = await pool.query('SELECT 1 FROM schema_migrations WHERE filename = $1', [file]);
if (exists.rowCount && exists.rowCount > 0) {
console.log(`✓ already applied ${file}`);
continue;
}
const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf8');
console.log(`→ applying ${file}`);
const client = await pool.connect();
try {
await client.query(sql);
await client.query('INSERT INTO schema_migrations(filename) VALUES ($1)', [file]);
console.log(`✓ applied ${file}`);
} finally {
client.release();
}
}
await pool.end();
}
main().catch(err => {
console.error('migrate failed:', err);
process.exit(1);
});