import { Pool } from 'pg'; /** * The database of the import tests (ยง 11). It is a scratch database that holds * the core schema and nothing of anyone's store, and the tests never leave * anything in it: * * - The tests connect to `evershop_import_scratch_test` and to no other * database. The name is not read from `DB_NAME` (a developer's `.env` points * that at the store they work on), and a name that does not end in * `_scratch_test` is refused. * - Everything the tests create goes into a schema of their own, put first on * the `search_path`, and the schema is dropped at the end. The tables of the * store (`public`) are only read, never written, so a store that runs on the * same database (its event process, its cron jobs) sees nothing of it: the * copies of the category tables have no triggers, so no event is written. * - The host, port, user and password come from `DB_HOST`, `DB_PORT`, `DB_USER` * and `DB_PASSWORD`, and when `DB_USER` is not set, from the `.env` file in * the directory the tests run from (only those four keys). * * - The tests that write through core's real services write to the shared * tables of `public` in the second database (`category`, `event`, ...) and * clean up after themselves, so two of them must not run at the same time: * `openScratchDb({ core: true })` holds a PostgreSQL advisory lock until * `close()`, and a second suite (in another Jest worker, or in another run of * the tests) waits for it. The lock belongs to the connection, so it goes away * with a process that dies. * * Without a reachable database the suites skip, as the other database suites of * the repository do. With `IMPORT_DB_REQUIRED` set they fail instead, so that a * missing database is never mistaken for a pass: the workflow sets it where it * provides a database. (It is not tied to `CI`: a workflow that has no database * yet would fail on every pull request.) */ export declare const SCRATCH_DB = "evershop_import_scratch_test"; /** * The second scratch database, a schema-only copy of the first with no store * running on it. The tests that write through core's real services (and so fire * the triggers of the real tables) use this one, so that no event process reacts * to them. `IMPORT_TEST_CORE_DB` can name another; it too must end in * `_scratch_test`. */ export declare const CORE_SCRATCH_DB = "evershop_import_core_scratch_test"; export interface ScratchDb { /** A pool whose `search_path` starts with the test schema. */ pool: Pool; schema: string; database: string; /** How the database was reached, to point core's own pool at it (`pointCoreAt`). */ connection: { host: string; port: number; user?: string; password?: string; }; close(): Promise; } export interface OpenOptions { /** * Open the database for the real services: the tables of the store are not * copied into the test schema, because the services write to `public`. */ core?: boolean; /** * The name of the lock a `core` suite takes, for the test of the lock itself: * it takes turns with its own copies and not with the suites that write to the * tables, so its timing does not depend on how long those take. */ lock?: string; } export declare function openScratchDb(options?: OpenOptions): Promise; /** * Points core's own pool (`lib/postgres/connection`) at the scratch database. * That pool is built from the environment when it is first imported, so this * must run **before** the first import of anything that reaches it: a test * imports the real services afterwards, with `await import(...)`. The name is * checked again here, because this is what lets core's services write. */ export declare function pointCoreAt(db: ScratchDb): void;