import { afterEach, describe, expect, it, vi } from "vitest"; import type { Knex } from "knex"; import { createMultiStorageServer, startPglite, type MultiStorageServer, type StartedPglite } from "./createMultiStorageServer.js"; import { createStorageProbe } from "./storageProbe.js"; import { admin, INSTALL, LOGIN } from "./installAndLogIn.js"; /** * A Postgres default storage and a second Postgres storage in one process, as a project with * `` (or ``) and an extra Postgres storage runs: two databases * with the same table names, one Postgres CMS module serving both. */ const PG2 = "pg-2"; /** The tables, columns and indexes of the database, to compare before and after a setup. */ const schemaOf = async (knex: Knex) => { const columns = await knex.raw( `SELECT table_name, column_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_schema = 'public' ORDER BY table_name, column_name` ); const indexes = await knex.raw( "SELECT indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' ORDER BY indexname" ); return { columns: columns.rows, indexes: indexes.rows }; }; const tableNames = async (knex: Knex): Promise => { const result = await knex.raw( "SELECT tablename FROM pg_tables WHERE schemaname = 'public' ORDER BY tablename" ); return result.rows.map((row: { tablename: string }) => row.tablename); }; const installAndLogIn = async (server: MultiStorageServer): Promise => { const install = await server.graphql(INSTALL, { installationInput: [{ app: "SelfHostedAuth", data: admin }] }); expect(install.body.data.system.installSystem).toEqual({ data: true, error: null }); const login = await server.graphql(LOGIN, { email: admin.email, password: admin.password }); return login.body.data.selfHostedAuthLogin.data.token; }; describe("api-composition-standalone-sql — two Postgres storages in one process", () => { const toClose: (() => Promise)[] = []; afterEach(async () => { while (toClose.length) { await toClose.pop()!(); } vi.restoreAllMocks(); }); it("keeps the Postgres CMS caches of the two databases apart", async () => { const probe = createStorageProbe(); const server = await createMultiStorageServer({ defaultStorage: "pglite", extraStorages: [PG2], extensions: [probe.extension] }); toClose.push(server.close); const token = await installAndLogIn(server); const entriesOn = async (storage: string, titles: string[], before?: string) => { const result = await server.graphql( `mutation Entries($storage: String!, $titles: [String!]!, $before: String) { entriesOnStorage(storage: $storage, modelId: "note", titles: $titles, before: $before) }`, { storage, titles, before }, { token } ); expect(result.body.errors).toBeUndefined(); return result.body.data.entriesOnStorage; }; /* * Root's scope on each database in turn: the same tenant and the same model. On the default * storage another model is written first, so the note model's CMS scope has a different id * in each database. A cache shared by both would hand one database the other's scope. */ expect(await entriesOn("default", ["On default"], "other")).toEqual(["On default"]); expect(await entriesOn(PG2, ["On pg-2"])).toEqual(["On pg-2"]); expect(await entriesOn("default", [])).toEqual(["On default"]); expect(await entriesOn(PG2, [])).toEqual(["On pg-2"]); // Stored where they were written. const titlesIn = async (knex: Knex) => { const result = await knex.raw( "SELECT row_to_json(e)::text AS row FROM webiny_cms_entries e" ); return result.rows .map((row: { row: string }) => row.row) .filter((row: string) => row.includes("On ")) .map((row: string) => (row.includes("On default") ? "default" : "pg-2")); }; expect(await titlesIn(server.connections.default)).toEqual(["default"]); expect(await titlesIn(server.connections[PG2])).toEqual(["pg-2"]); }); it("sets up the schema on every declared storage at start, and a second setup changes nothing", async () => { const databases: Record = { default: await startPglite(), [PG2]: await startPglite() }; toClose.push(async () => { for (const database of Object.values(databases)) { await database.stop(); } }); const first = await createMultiStorageServer({ defaultStorage: "pglite", extraStorages: [PG2], databases }); // Every part's tables, on both databases, before any request. for (const knex of [databases.default.knex, databases[PG2].knex]) { expect(await tableNames(knex)).toEqual( expect.arrayContaining([ "webiny_core_tenants", "webiny_core_roles", "webiny_core_key_value", "webiny_audit_logs", "WebsocketsConnections", "webiny_self_hosted_credentials", "webiny_cms_entries", "webiny_aco_folders" ]) ); } const token = await installAndLogIn(first); // Schema-managed from the start: no request ran DDL on either database. expect(first.ddlAfterStart("default")).toEqual([]); expect(first.ddlAfterStart(PG2)).toEqual([]); const before = { default: await schemaOf(databases.default.knex), [PG2]: await schemaOf(databases[PG2].knex) }; await first.close(); // A restart sets every storage up again: nothing changes, and the data is still there. const second = await createMultiStorageServer({ defaultStorage: "pglite", extraStorages: [PG2], databases }); toClose.push(second.close); expect(await schemaOf(databases.default.knex)).toEqual(before.default); expect(await schemaOf(databases[PG2].knex)).toEqual(before[PG2]); const login = await second.graphql(LOGIN, { email: admin.email, password: admin.password }); expect(login.body.data.selfHostedAuthLogin.error).toBeNull(); expect(token).toBeTruthy(); }); });