testing/cross_backend/postgres_database.ts

Postgres database helpers for cross-backend global setups — creating the database a backend's URL names, emptying one a setup owns, and printing such a URL safely.

The harness never creates a database by itself: a setup opts in (create_cross_backend_global_setup's ensure_databases, or a call to ensure_database_for_url), so a test role without CREATEDB is never required by default.

view source

Declarations
#

11 declarations

empty_owned_database
#

testing/cross_backend/postgres_database.ts view source

(database_url: string, options: EmptyOwnedDatabaseOptions): Promise<void> import {empty_owned_database} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

Drop and recreate the public schema of a database a setup owns, so a backend that does not wipe its own database migrates and bootstraps from nothing on every run — bootstrap refuses a database that already holds an account.

The wipe takes everything, so it is tied to owned_name, the one database the setup owns, rather than to whatever URL reaches it. Create the database first (ensure_database_for_url); a missing one, a server that cannot be reached and a role that cannot connect all fail with what to check.

database_url

a postgres:// URL naming the owned database

type string

options

the owned name, the setup's label, and an optional hint

returns

Promise<void>

throws

  • Error - when the URL names another database, or when the database cannot be emptied (with what to check)

EmptyOwnedDatabaseOptions
#

testing/cross_backend/postgres_database.ts view source

EmptyOwnedDatabaseOptions import type {EmptyOwnedDatabaseOptions} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

owned_name

The one database name the call may empty — the setup's own. A URL naming any other database is refused before anything connects.

type string

readonly

label

Prefixes every error message, naming the setup (e.g. 'schema parity setup').

type string

readonly

hint?

Appended to the fix a connect failure suggests — another way to reach a usable server, such as the env var that points the setup elsewhere.

type string

readonly

ensure_database
#

testing/cross_backend/postgres_database.ts view source

(database: string, server_url?: string | undefined, deps?: EnsureDatabaseDeps): boolean import {ensure_database} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

Create a Postgres database when it is missing, with createdb.

Which server it lands on depends on server_url. Given one, the database is created on the server that URL names: createdb connects through the URL with its database swapped for the postgres maintenance database, so host, port, role and query parameters all come from the URL, and a password goes to createdb in its environment, never its arguments. Without one, createdb reads the libpq environment (PGHOST / PGPORT / PGUSER).

Idempotent and never throws: a database that already exists is the quiet success case (is_createdb_already_exists). Any other failure — no createdb on PATH, no server, a role that may not create databases — is reported through deps.warn with createdb's own stderr, and whatever connects next reports a database that is really missing.

database

the database name

type string

server_url?

a postgres:// URL on the server to create it on; its database name is ignored

type string
optional

deps?

where a failure is reported, console.warn by default

optional default default_ensure_database_deps

returns

boolean

whether the database exists now (created, or already there)

ensure_database_for_url
#

testing/cross_backend/postgres_database.ts view source

(database_url: string, deps?: EnsureDatabaseDeps | undefined): boolean import {ensure_database_for_url} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

Create the database a Postgres URL names, on the server it names, when it is missing (ensure_database). A URL that is not postgres: — memory://, file:// — or that names no database is left alone.

database_url

the URL a backend connects to

type string

deps?

where a failure is reported, console.warn by default

optional

returns

boolean

whether the URL named a Postgres database to ensure

EnsureDatabaseDeps
#

is_createdb_already_exists
#

testing/cross_backend/postgres_database.ts view source

(stderr: string, database: string): boolean import {is_createdb_already_exists} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

Whether createdb's stderr is its refusal of a database that already exists — Postgres's database "<name>" already exists, which createdb prints after database creation failed: ERROR:.

stderr

what createdb wrote to stderr

type string

database

the database it was asked to create

type string

returns

boolean

is_postgres_url
#

testing/cross_backend/postgres_database.ts view source

(url: string): boolean import {is_postgres_url} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

Whether url parses as a postgres: / postgresql: URL. URL.canParse alone admits a scheme-less localhost:5432/db, which it reads as protocol localhost:.

url

the candidate connection string

type string

returns

boolean

POSTGRES_URL_PROTOCOLS
#

redact_database_url
#

testing/cross_backend/postgres_database.ts view source

(database_url: string): string import {redact_database_url} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

A database URL fit to print: the password, when there is one, replaced — in the userinfo and in the password query parameter pg and libpq also take. The whole URL rather than its host, which is empty for a socket URL that names its server in the query string.

database_url

a postgres:// URL

type string

returns

string

the URL with any password replaced by REDACTED

to_maintenance_database_url
#

testing/cross_backend/postgres_database.ts view source

(server_url: string): string import {to_maintenance_database_url} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

The URL createdb connects through to create a database on the server server_url names — the same URL with its database replaced by postgres.

server_url

a postgres:// URL on the target server

type string

returns

string

the URL of that server's postgres maintenance database

with_database_name
#

testing/cross_backend/postgres_database.ts view source

(database_url: string, database: string): string import {with_database_name} from '@fuzdev/fuz_app/testing/cross_backend/postgres_database.js';

database_url with its database replaced by database — how a setup that spawns a second backend lands on the same server under a database nothing else uses.

database_url

a postgres:// URL on the target server

type string

database

the database name to put in it

type string

returns

string

the URL naming database on the same server

Depends on
#

Imported by
#