feat: dockerfile (node runtime), startup migration, README, runtime env config
This commit is contained in:
17
server/plugins/migrate.ts
Normal file
17
server/plugins/migrate.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
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);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user