import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { migrate } from "drizzle-orm/node-postgres/migrator"; import { createDb } from "./client.js"; const HERE = dirname(fileURLToPath(import.meta.url)); export const MIGRATIONS_DIR = join(HERE, "..", "migrations"); /** Apply all pending Drizzle migrations to the target database. */ export async function runMigrations(connectionString: string, migrationsFolder = MIGRATIONS_DIR): Promise { const { db, pool } = createDb(connectionString); try { await migrate(db, { migrationsFolder }); } finally { await pool.end(); } } async function main(): Promise { const url = process.env.DATABASE_URL; if (!url) { console.error("DATABASE_URL is required"); process.exit(1); } await runMigrations(url); console.log(JSON.stringify({ event: "migrations_applied" })); } if (import.meta.url === `file://${process.argv[1]}`) { main().catch((e) => { console.error(e); process.exit(1); }); }