import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { migrate } from "drizzle-orm/node-postgres/migrator"; import { closeDb, getDb } from "./client.js"; export async function runMigrations(connectionString = process.env.DATABASE_URL): Promise { const db = getDb(connectionString); const here = dirname(fileURLToPath(import.meta.url)); // works from src (tsx) and from dist (compiled) const migrationsFolder = join(here, "..", "drizzle"); await migrate(db, { migrationsFolder }); } if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { runMigrations() .then(async () => { console.log("[db] migrations applied"); await closeDb(); }) .catch(async (e) => { console.error("[db] migration failed:", e); await closeDb(); process.exit(1); }); }