24 lines
680 B
TypeScript
24 lines
680 B
TypeScript
// Idempotent migration runner. Run via `bun run migrate` / on container start.
|
|
import { readFileSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import { dirname, join } from "node:path";
|
|
import postgres from "postgres";
|
|
|
|
const url = process.env.DATABASE_URL;
|
|
if (!url) {
|
|
console.error("DATABASE_URL not set");
|
|
process.exit(1);
|
|
}
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const sql = postgres(url, { max: 1 });
|
|
|
|
try {
|
|
const ddl = readFileSync(join(here, "migrations", "0001_init.sql"), "utf8");
|
|
// Trusted local DDL file, not user input.
|
|
await sql.unsafe(ddl);
|
|
console.log("migration 0001_init applied");
|
|
} finally {
|
|
await sql.end();
|
|
}
|