Files
TREK/server/tests/unit/nest/postgres-database-backup.test.ts
T
Isaias Tavares fc9e634d86 feat(db): back up, restore and copy a Postgres database before migrating it
On Postgres the backup port dumps with pg_dump and restores by turning the
dump into a script with pg_restore and having psql clear the current schema
and run it in one transaction. A dump only recreates what it holds, so a
plain pg_restore --clean would leave a later migration's tables behind and
break the migrations the reopen runs; a restore that fails leaves the
database as it was. A restore refuses a pg_restore or psql older than
17.6/16.10/15.14/14.19, whose script would lack the \restrict line that
keeps a psql meta-command smuggled into an uploaded dump from running
(CVE-2025-8714). A restore
refuses a SQLite archive, a dump without TREK's migration history or one a
newer TREK wrote before anything is replaced, and keeps a dump of the
database it replaces.

RESTORE_FROM_BACKUP loads the archive's dump the same way into a database
that holds no table yet, and is ignored with a warning once it holds one.

Before pending migrations the boot now takes a pg_dump copy to
data/pre-migrate-<last migration>-<time>.pgdump under the same
TREK_DB_PRE_MIGRATE_SNAPSHOT(_KEEP) policy as the SQLite file, instead of
refusing; a copy that cannot be written refuses the boot.

The tools run by absolute path (TREK_DB_PG_BIN_DIR, or the usual
directories), never through PATH, with the password in PGPASSWORD; an
sslmode that asks for SSL verifies the certificate as the app pool does.
Dumps are written beside their target and renamed into place, readable by
their owner only, and a tool's stderr reaches errors without the lines that
quote SQL or rows. The image installs postgresql-client-17 from Debian 13.
2026-11-04 22:45:31 -03:00

143 lines
6.9 KiB
TypeScript

/**
* PostgresDatabaseBackup: the backup port on a Postgres database, over
* stand-in pg_dump, pg_restore and psql scripts (TREK_DB_PG_BIN_DIR) and a
* stand-in DatabaseLifecycle. The tools' own arguments are pinned in
* tests/unit/db/engine/postgres/pg-tools.test.ts.
*/
import { knownMigrationNames } from '../../../src/db/known-migrations';
import type { DataPathsService } from '../../../src/nest/app-config/data-paths.service';
import { PostgresDatabaseBackup } from '../../../src/nest/backup/postgres-database-backup';
import { DomainError } from '../../../src/nest/common/domain-error';
import { DatabaseConnectionLostError } from '../../../src/nest/database/database-backup.interface';
import type { DatabaseLifecycle } from '../../../src/nest/database/database-lifecycle.service';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const KNOWN = [...knownMigrationNames()].sort();
let root: string;
let bin: string;
let extract: string;
let reopen: ReturnType<typeof vi.fn>;
let backup: PostgresDatabaseBackup;
/** A stand-in tool: `body` is the shell it runs. */
function tool(name: 'pg_dump' | 'pg_restore' | 'psql', body: string): void {
const version = `[ "$1" = --version ] && { echo '${name} (PostgreSQL) 17.11'; exit 0; }`;
writeFileSync(path.join(bin, name), `#!/bin/sh\n${version}\n${body}\n`);
chmodSync(path.join(bin, name), 0o755);
}
const WRITES_ITS_FILE = 'for a in "$@"; do case "$a" in --file=*) printf dump > "${a#--file=}";; esac; done';
const FAILS = 'echo "connection refused" >&2; exit 1';
/** A pg_restore whose --data-only listing shows `names` as the dump's migrations. */
const LISTS = (names: string[]) =>
[
'case "$*" in *--data-only*)',
" printf 'COPY public.mikro_orm_migrations (id, name, executed_at) FROM stdin;\\n'",
...names.map((name, i) => ` printf '${i + 1}\\t${name}\\tx\\n'`),
" printf '\\\\.\\n';;",
'esac',
].join('\n');
beforeEach(() => {
root = mkdtempSync(path.join(tmpdir(), 'trek-pg-backup-'));
bin = path.join(root, 'bin');
extract = path.join(root, 'extract');
mkdirSync(bin);
mkdirSync(extract);
vi.stubEnv('TREK_DB_URL', 'postgresql://trek:pw@db/trek');
vi.stubEnv('TREK_DB_PG_BIN_DIR', bin);
reopen = vi.fn(async () => undefined);
backup = new PostgresDatabaseBackup(
{ reopen } as unknown as DatabaseLifecycle,
{ dataDir: root } as unknown as DataPathsService,
);
});
afterEach(() => {
vi.unstubAllEnvs();
vi.restoreAllMocks();
rmSync(root, { recursive: true, force: true });
});
async function refusal(promise: Promise<unknown>): Promise<{ status: number; message: string }> {
const error = await promise.then(
() => null,
(err: unknown) => err,
);
expect(error).toBeInstanceOf(DomainError);
return { status: (error as DomainError).getStatus(), message: (error as DomainError).publicMessage };
}
describe('PostgresDatabaseBackup', () => {
it('PGBACKUP-001: a snapshot is a pg_dump; a failed one leaves nothing that could pass for a copy', async () => {
expect(backup.archiveEntry).toBe('travel.pgdump');
expect(backup.canSnapshot()).toBe(true);
await expect(backup.checkpoint()).resolves.toBeUndefined();
tool('pg_dump', WRITES_ITS_FILE);
await backup.snapshot(path.join(root, 'a.pgdump'));
expect(existsSync(path.join(root, 'a.pgdump'))).toBe(true);
tool('pg_dump', `printf partial > "$(echo "$4" | sed 's/--file=//')"; ${FAILS}`);
await expect(backup.snapshot(path.join(root, 'b.pgdump'))).rejects.toThrow('pg_dump failed: connection refused');
expect(existsSync(path.join(root, 'b.pgdump'))).toBe(false);
expect(existsSync(path.join(root, 'b.pgdump.partial'))).toBe(false);
});
it('PGBACKUP-002: verify takes a TREK dump this build knows, and refuses a missing, SQLite, foreign or newer one', async () => {
expect(await refusal(backup.verify(extract))).toEqual({
status: 400,
message: 'Invalid backup: travel.pgdump not found',
});
writeFileSync(path.join(extract, 'travel.db'), 'sqlite');
expect((await refusal(backup.verify(extract))).message).toMatch(/holds a SQLite database/);
writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
tool('pg_restore', LISTS(KNOWN.slice(0, 3)));
await expect(backup.verify(extract)).resolves.toBeNull();
tool('pg_restore', LISTS([...KNOWN, 'Migration29990101000000_from_the_future']));
expect((await refusal(backup.verify(extract))).message).toMatch(/made by a newer TREK version/);
tool('pg_restore', LISTS([]));
expect((await refusal(backup.verify(extract))).message).toMatch(/holds no TREK migration history/);
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
tool('pg_restore', FAILS);
expect((await refusal(backup.verify(extract))).message).toBe('Uploaded file is not a valid Postgres dump');
});
it('PGBACKUP-003: keeps a dump of the database a restore replaces, or says it could not', async () => {
vi.spyOn(console, 'log').mockImplementation(() => undefined);
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
tool('pg_dump', WRITES_ITS_FILE);
const kept = await backup.keepCopyBeforeRestore();
expect(kept).toMatch(/pre-restore-\d+\.pgdump$/);
expect(existsSync(kept as string)).toBe(true);
tool('pg_dump', FAILS);
await expect(backup.keepCopyBeforeRestore()).resolves.toBeNull();
});
it('PGBACKUP-004: replace restores, then reopens; a reopen failure is reported, not thrown', async () => {
tool('pg_restore', "printf '%s\\n' '\\restrict k1' > \"${3#--file=}\"");
tool('psql', 'exit 0');
await expect(backup.replace(path.join(extract, 'travel.pgdump'))).resolves.toEqual({ reopenError: null });
expect(reopen).toHaveBeenCalledTimes(1);
const lost = new Error('reconnect failed');
reopen.mockRejectedValueOnce(lost);
await expect(backup.replace(path.join(extract, 'travel.pgdump'))).resolves.toEqual({ reopenError: lost });
});
it('PGBACKUP-005: a failed restore still reopens, and both failures surface when the reopen fails too', async () => {
tool('pg_restore', FAILS);
tool('psql', 'exit 0');
const source = path.join(extract, 'travel.pgdump');
await expect(backup.replace(source)).rejects.toThrow('pg_restore failed: connection refused');
expect(reopen).toHaveBeenCalledTimes(1);
reopen.mockRejectedValueOnce(new Error('reconnect failed'));
await expect(backup.replace(source)).rejects.toBeInstanceOf(DatabaseConnectionLostError);
});
it('PGBACKUP-006: names the database without its password, and refuses to run without TREK_DB_URL', async () => {
expect(backup.location()).toBe('Postgres postgresql://trek:***@db/trek');
vi.stubEnv('TREK_DB_URL', '');
await expect(backup.snapshot(path.join(root, 'c.pgdump'))).rejects.toThrow(/without TREK_DB_URL/);
});
});