import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { pool } from "./pool.js"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); async function main() { await pool.query(` CREATE TABLE IF NOT EXISTS schema_migrations ( id SERIAL PRIMARY KEY, filename TEXT NOT NULL UNIQUE, applied_at TIMESTAMPTZ NOT NULL DEFAULT now() ) `); const files = (await readdir(path.join(__dirname, "migrations"))) .filter((file) => file.endsWith(".sql")) .sort(); for (const filename of files) { const existing = await pool.query( "SELECT 1 FROM schema_migrations WHERE filename = $1", [filename] ); if (existing.rowCount) { continue; } const sql = await readFile(path.join(__dirname, "migrations", filename), "utf8"); await pool.query("BEGIN"); try { await pool.query(sql); await pool.query("INSERT INTO schema_migrations (filename) VALUES ($1)", [filename]); await pool.query("COMMIT"); console.log(`applied migration ${filename}`); } catch (error) { await pool.query("ROLLBACK"); throw error; } } } main() .catch((error) => { console.error(error); process.exitCode = 1; }) .finally(async () => { await pool.end(); });