feat: db connection and migration

This commit is contained in:
2026-06-10 07:51:00 +00:00
parent 935ff5b757
commit 63714f5960
3 changed files with 58 additions and 0 deletions

23
server/db/migrate.ts Normal file
View 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();
}