feat: db connection and migration
This commit is contained in:
23
server/db/migrate.ts
Normal file
23
server/db/migrate.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
// 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();
|
||||
}
|
||||
Reference in New Issue
Block a user