Files
2537e64970 feat(db): install into a configurable PostgreSQL schema (#5)
* feat(db): install into a configurable PostgreSQL schema

Add DB_SCHEMA (and dbOptions.schema / createDatabase({ schema }) /
runMigrations(db, { schema })) so notifkit can live in a schema other than
public. Defaults to public, where behaviour is unchanged.

For a custom schema, runMigrations creates the schema, retargets the
bundled migrations at it (rewrites the "public". qualifiers drizzle-kit
emits and sets search_path for the unqualified objects with SET LOCAL,
inside the migration transaction), refuses any migration that still
mentions public afterwards, and keeps the drizzle journal in the same
schema. Afterwards it checks via information_schema that every table of
the package's Drizzle schema exists in the target schema and throws with
the names of any that are missing. createDatabase sets search_path on
every connection.

Add a notifkit-migrate bin (next to notifkit-create-admin) that reads
DATABASE_URL and DB_SCHEMA, applies the migrations, closes the connection
and exits non-zero on failure. The README now uses it for production
migrations instead of drizzle-kit, which ignores DB_SCHEMA, and warns
that switching an existing public install to DB_SCHEMA without moving
its tables first makes it look empty.

Closes #4

* test(db): fix typecheck and lint, move schema tests under tests/

---------

Co-authored-by: Alexander Savchenko <savchenko_av@nlmk.com>
Co-authored-by: coo1estguy <70365929+coolestcodeguy@users.noreply.github.com>
2026-10-07 21:27:45 +05:30

44 lines
1.5 KiB
JavaScript

#!/usr/bin/env node
/**
* Apply notifkit's database migrations.
*
* Reads DATABASE_URL and DB_SCHEMA (from the environment or .env) and runs the
* same migrations NotifkitServer runs on start-up with autoMigrate, into the
* same schema. Safe to run repeatedly: already-applied migrations are skipped.
* Exits non-zero if anything fails.
*
* Usage:
* DATABASE_URL=postgres://user:pass@host:5432/db npx notifkit-migrate
* DATABASE_URL=... DB_SCHEMA=notifications npx notifkit-migrate
* node scripts/migrate.mjs
*/
import { config } from "dotenv";
import { resolve } from "node:path";
config({ path: resolve(process.cwd(), ".env") });
if (!process.env.DATABASE_URL) {
console.error("Usage: DATABASE_URL=postgres://... [DB_SCHEMA=schema] npx notifkit-migrate");
process.exit(1);
}
try {
// Imported after .env is loaded: the package reads DB_SCHEMA from the environment.
const { migrateDatabase } = await import("../dist/index.mjs");
const schema = await migrateDatabase({ url: process.env.DATABASE_URL });
console.log(`Migrations applied to schema "${schema}".`);
} catch (err) {
console.error(`Failed to run migrations: ${describe(err)}`);
process.exitCode = 1;
}
/** drizzle wraps driver errors as "Failed query: ..."; the cause says what actually went wrong. */
function describe(err) {
const parts = [err.message];
if (err.fields) parts.push(JSON.stringify(err.fields));
const cause = err.cause;
if (cause) parts.push(`cause: ${cause.message || cause.code || String(cause)}`);
return parts.join("\n ");
}