mirror of
https://github.com/devkitshq/notifkit.git
synced 2026-10-19 18:24:52 +02:00
* 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>
66 lines
2.7 KiB
TypeScript
66 lines
2.7 KiB
TypeScript
import fs from "fs";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import { describe, expect, it } from "vitest";
|
|
import { notifkitTableNames, qualifyMigrationSql } from "@/db/index.js";
|
|
|
|
const drizzleFolder = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../drizzle");
|
|
const journal = JSON.parse(
|
|
fs.readFileSync(path.join(drizzleFolder, "meta", "_journal.json"), "utf8"),
|
|
) as { entries: { tag: string }[] };
|
|
const migrations = journal.entries.map(({ tag }) => ({
|
|
tag,
|
|
source: fs.readFileSync(path.join(drizzleFolder, `${tag}.sql`), "utf8"),
|
|
}));
|
|
|
|
describe("qualifyMigrationSql", () => {
|
|
it.each(migrations)("retargets $tag without leaving public behind", ({ source }) => {
|
|
const qualified = qualifyMigrationSql(source, "notifications");
|
|
|
|
expect(qualified).not.toMatch(/\bpublic\b/i);
|
|
expect(qualified.split('"notifications".').length).toBe(source.split('"public".').length);
|
|
expect(
|
|
qualified.startsWith('SET LOCAL search_path TO "notifications";\n--> statement-breakpoint\n'),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("rewrites every qualifier drizzle-kit emitted", () => {
|
|
const total = migrations.reduce((n, { source }) => n + source.split('"public".').length - 1, 0);
|
|
expect(total).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("refuses a migration that names public in a form it does not rewrite", () => {
|
|
const source =
|
|
'CREATE TABLE "a" ("id" int);\n--> statement-breakpoint\nGRANT SELECT ON public.a TO x;';
|
|
expect(() => qualifyMigrationSql(source, "notifications")).toThrow(/public schema/);
|
|
});
|
|
|
|
it("leaves a column whose name merely starts with public alone", () => {
|
|
const source = 'ALTER TABLE "projects" ADD COLUMN "public_url" text;';
|
|
expect(() => qualifyMigrationSql(source, "notifications")).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe("notifkitTableNames", () => {
|
|
it("lists exactly the tables the bundled migrations create", () => {
|
|
const created = migrations
|
|
.flatMap(({ source }) => [...source.matchAll(/CREATE TABLE (?:IF NOT EXISTS )?"(\w+)"/g)])
|
|
.map((match) => match[1]);
|
|
expect(notifkitTableNames()).toEqual([...new Set(created)].sort());
|
|
});
|
|
});
|
|
|
|
describe("notifkit-migrate packaging", () => {
|
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8")) as {
|
|
bin: Record<string, string>;
|
|
files: string[];
|
|
};
|
|
|
|
it("is published as a bin next to notifkit-create-admin", () => {
|
|
expect(pkg.bin["notifkit-migrate"]).toBe("./scripts/migrate.mjs");
|
|
expect(pkg.files).toContain("scripts/migrate.mjs");
|
|
expect(fs.existsSync(path.join(root, "scripts", "migrate.mjs"))).toBe(true);
|
|
});
|
|
});
|