diff --git a/src/store/index.ts b/src/store/index.ts index d4d2ab5..b281ec4 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -14,9 +14,10 @@ export async function createStore(config: HeimdallConfig): Promise { - await this.pool.query("SELECT 1"); + /** + * Proves, for a start that does not apply the schema, that the URL connects + * and that the database holds every relation and column the schema makes. + * A missing one fails with code SCHEMA_MISSING. + */ + async checkSchema(): Promise { + const result = await this.pool.query<{ missing: string }>( + ` + SELECT name AS missing FROM unnest($1::text[]) AS name WHERE to_regclass(name) IS NULL + UNION ALL + SELECT required.table_name || '.' || required.column_name + FROM unnest($2::text[], $3::text[]) AS required(table_name, column_name) + WHERE NOT EXISTS ( + SELECT 1 FROM information_schema.columns c + WHERE c.table_schema = current_schema() + AND c.table_name = required.table_name + AND c.column_name = required.column_name + ) + `, + [ + REQUIRED_RELATIONS, + REQUIRED_COLUMNS.map((required) => required.table), + REQUIRED_COLUMNS.map((required) => required.column), + ] + ); + if (result.rows.length > 0) { + throw Object.assign(new Error("The Heimdall schema is not applied."), { code: "SCHEMA_MISSING" }); + } } async close(): Promise { diff --git a/src/store/schema.ts b/src/store/schema.ts index 17ddc49..8e9c921 100644 --- a/src/store/schema.ts +++ b/src/store/schema.ts @@ -162,3 +162,16 @@ CREATE TABLE IF NOT EXISTS audit_events ( CREATE INDEX IF NOT EXISTS audit_events_lookup_idx ON audit_events(account_id, session_id, app_slug, created_at); `; + +/** + * What a start that does not apply the schema must find, read from the SQL + * above so the two cannot drift: every table and index it creates, and every + * column it adds to an existing table. + */ +export const REQUIRED_RELATIONS = [ + ...CREATE_SCHEMA_SQL.matchAll(/^CREATE (?:UNIQUE )?(?:TABLE|INDEX) IF NOT EXISTS (\w+)/gm), +].map((match) => match[1]!); + +export const REQUIRED_COLUMNS = [ + ...CREATE_SCHEMA_SQL.matchAll(/^ALTER TABLE (\w+) ADD COLUMN IF NOT EXISTS (\w+)/gm), +].map((match) => ({ table: match[1]!, column: match[2]! })); diff --git a/tests/store.test.ts b/tests/store.test.ts index 30d6861..c28e353 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from "vitest"; import { newDb } from "pg-mem"; import { createPostgresStore, PostgresStore } from "../src/store/postgres.js"; +import { REQUIRED_COLUMNS, REQUIRED_RELATIONS } from "../src/store/schema.js"; describe("PostgresStore", () => { it("round-trips core auth records through postgres", async () => { @@ -209,3 +210,35 @@ describe("createPostgresStore", () => { ]); }); }); + +describe("PostgresStore.checkSchema", () => { + it("requires every table and index the schema creates and every column it adds", () => { + expect(REQUIRED_RELATIONS).toEqual( + expect.arrayContaining(["accounts", "audit_events", "auth_completions_attempt_unconsumed_unique_idx", "audit_events_lookup_idx"]) + ); + expect(REQUIRED_RELATIONS).toHaveLength(17); + expect(REQUIRED_COLUMNS).toEqual([{ table: "auth_completions", column: "attempt_id" }]); + }); + + function storeAnswering(missing: string[]) { + const queries: unknown[][] = []; + const store = new PostgresStore({ + query: (async (_text: string, values: unknown[]) => { + queries.push(values); + return { rows: missing.map((name) => ({ missing: name })) }; + }) as never, + end: async () => undefined, + }); + return { store, queries }; + } + + it("refuses with SCHEMA_MISSING when anything is missing", async () => { + const { store, queries } = storeAnswering(["auth_completions.attempt_id"]); + await expect(store.checkSchema()).rejects.toMatchObject({ code: "SCHEMA_MISSING" }); + expect(queries).toEqual([[REQUIRED_RELATIONS, ["auth_completions"], ["attempt_id"]]]); + }); + + it("passes when nothing is missing", async () => { + await expect(storeAnswering([]).store.checkSchema()).resolves.toBeUndefined(); + }); +});