18 lines
599 B
TypeScript
18 lines
599 B
TypeScript
import { INIT_SQL } from "../utils/schema";
|
|
|
|
// Apply the schema idempotently on server startup. An advisory lock prevents concurrent
|
|
// instances from racing on CREATE TABLE. Idempotent CREATE ... IF NOT EXISTS means this is
|
|
// safe to run on every boot.
|
|
export default defineNitroPlugin(async () => {
|
|
try {
|
|
const sql = getSql();
|
|
await sql.begin(async (tx) => {
|
|
await tx`select pg_advisory_xact_lock(871042)`;
|
|
await tx.unsafe(INIT_SQL);
|
|
});
|
|
console.log("[migrate] schema ensured");
|
|
} catch (e) {
|
|
console.error("[migrate] failed:", (e as Error).message);
|
|
}
|
|
});
|