Files
TREK/server/tests/unit/nest/postgres-database-backup.test.ts
T
Isaias Tavares a77705984e feat(db): create and migrate the schema on a Postgres database
With TREK_DB_URL set, TREK now boots on Postgres; without it nothing changes.

- The database engine contract gains open, close, reopen and the schema
  bootstrap; DatabaseLifecycle delegates to the engine resolveEngine() picks.
- An empty Postgres database gets server/assets/postgres/baseline.sql
  (generated from the entities by gen:pg-baseline, with the reference rows
  the migrations up to its cut insert: tour types, provider catalogues,
  addons, app settings) and every migration up to its cut recorded as
  executed, in one transaction. A database holding tables
  without TREK's history is refused before anything is written; a migrated one
  runs the newer-database refusal and its pending migrations, which refuse
  without a copy unless TREK_DB_PRE_MIGRATE_SNAPSHOT=false.
- INTEGER columns are bigint on Postgres (SQLite's INTEGER is 64-bit: an
  epoch-millisecond column overflowed int4), and the engine's driver reads
  bigint and numeric back as numbers, refusing values past 2^53.
- The two SQLite triggers are ported to PL/pgSQL; check:pg-baseline fails
  while a SQLite trigger has no port, and while the baseline drifts from the
  entities before a later migration freezes it.
- Backups and restores on Postgres refuse until they can dump and load one.
- Migration helpers read the catalog per engine (dialect/catalog.ts), and
  the email case index no longer needs group_concat.
- CI runs check:pg-baseline and boots the image on postgres:17 twice.
2026-11-04 22:45:02 -03:00

41 lines
1.7 KiB
TypeScript

/**
* PostgresDatabaseBackup: the backup port on a Postgres database before TREK
* can dump one. Every path fails closed, before anything is written or swapped.
*/
import { resolveEngine } from '../../../src/db/engine';
import { POSTGRES_BACKUP_UNAVAILABLE, PostgresDatabaseBackup } from '../../../src/nest/backup/postgres-database-backup';
import { DomainError } from '../../../src/nest/common/domain-error';
import { describe, expect, it } from 'vitest';
describe('PostgresDatabaseBackup', () => {
const backup = new PostgresDatabaseBackup();
it('PGBACKUP-001: a backup attempt reports that it wrote nothing, rather than an archive without the database', async () => {
expect(backup.canSnapshot()).toBe(true);
await expect(backup.checkpoint()).resolves.toBeUndefined();
await expect(backup.snapshot()).rejects.toThrow(POSTGRES_BACKUP_UNAVAILABLE);
});
it('PGBACKUP-002: a restore is refused at the verify step with a 400, and nothing is swapped', async () => {
const refusal = (() => {
try {
return backup.verify();
} catch (error) {
return error;
}
})();
expect(refusal).toBeInstanceOf(DomainError);
expect((refusal as DomainError).getStatus()).toBe(400);
expect((refusal as DomainError).publicMessage).toBe(POSTGRES_BACKUP_UNAVAILABLE);
await expect(backup.keepCopyBeforeRestore()).resolves.toBeNull();
await expect(backup.replace()).rejects.toThrow(POSTGRES_BACKUP_UNAVAILABLE);
});
it('PGBACKUP-003: names the database the way the engine describes it (no password) and points at pg_dump', () => {
expect(backup.location()).toBe(resolveEngine().describe());
expect(backup.archiveEntry).toBe('database.dump');
expect(POSTGRES_BACKUP_UNAVAILABLE).toContain('pg_dump');
});
});