mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
feat(db): run TREK on PostgreSQL
TREK runs on PostgreSQL when TREK_DB_URL is set, and on SQLite as before otherwise: an engine port under src/db/engine, the dialect helpers under src/db/dialect, a Postgres baseline for the migrations up to the cut, pg_dump/pg_restore backups, a probe of every repository method against a real server and a Postgres test lane in CI.
This commit is contained in:
@@ -18,14 +18,15 @@ async function main(): Promise<void> {
|
||||
fs.rmSync(schemaSnapshotPath(), { force: true });
|
||||
|
||||
const { getRawConnection } = await import('../src/db/database');
|
||||
const { createStandaloneOrm, runSchemaBootstrap } = await import('../src/db/orm');
|
||||
const { runSchemaBootstrap } = await import('../src/db/orm');
|
||||
const { createStandaloneOrm } = await import('../src/db/standalone-orm');
|
||||
|
||||
// The shared driver binds to the connection database.ts opens on the ORM's
|
||||
// first connect, so this migrates that in-memory database and it can be
|
||||
// serialised straight after.
|
||||
const orm = await createStandaloneOrm();
|
||||
try {
|
||||
await runSchemaBootstrap(orm);
|
||||
await runSchemaBootstrap(orm, { snapshot: true });
|
||||
writeSchemaSnapshot(getRawConnection().serialize());
|
||||
console.log(`[test-db] schema snapshot written to ${schemaSnapshotPath()}`);
|
||||
} finally {
|
||||
|
||||
@@ -27,6 +27,7 @@ import { DayAssignments } from '../../src/db/entities/DayAssignments.entity';
|
||||
import { Places } from '../../src/db/entities/Places.entity';
|
||||
import { Reservations } from '../../src/db/entities/Reservations.entity';
|
||||
import { Trips } from '../../src/db/entities/Trips.entity';
|
||||
import { AssistantToolCallsRepository } from '../../src/db/repositories/AssistantToolCalls.repository';
|
||||
import { dbNow } from '../../src/db/types';
|
||||
import { TURNS_PER_MINUTE } from '../../src/nest/assistant/assistant-tools.constants';
|
||||
import { AssistantToolsService } from '../../src/nest/assistant/assistant-tools.service';
|
||||
@@ -661,6 +662,32 @@ describe('Trip assistant e2e', () => {
|
||||
expect(again.status).toBe(409);
|
||||
});
|
||||
|
||||
it('ASTE2E-026: the change is recorded as failed before the stream ends, however slow the write', async () => {
|
||||
const t = await travelerWithTrip();
|
||||
const { toolCallId, threadId } = await proposePlace(t, 'Fushimi Inari');
|
||||
const open = vi
|
||||
.spyOn(nestApp.get(AssistantToolsService), 'open')
|
||||
.mockRejectedValueOnce(new Error('tool session down'));
|
||||
const record = AssistantToolCallsRepository.prototype.recordOutcome;
|
||||
const slow = vi.spyOn(AssistantToolCallsRepository.prototype, 'recordOutcome').mockImplementation(async function (
|
||||
this: AssistantToolCallsRepository,
|
||||
...args
|
||||
) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
return record.apply(this, args);
|
||||
});
|
||||
try {
|
||||
await post(`${t.base}/threads/${threadId}/decisions`, t.userId, {
|
||||
...CTX,
|
||||
decisions: [{ toolCallId, approved: true }],
|
||||
});
|
||||
expect(await findRow(orm, AssistantToolCalls, { id: toolCallId })).toMatchObject({ state: 'failed' });
|
||||
} finally {
|
||||
slow.mockRestore();
|
||||
open.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('ASTE2E-015: an attached file is refused as JSON before the stream when nothing reads it', async () => {
|
||||
const t = await travelerWithTrip();
|
||||
const bookingImport = nestApp.get(BookingImportService);
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Stand-in PostgreSQL client tools (pg_dump, pg_restore and psql) for the unit
|
||||
* tests of the Postgres backup and restore. Each is an executable shell script
|
||||
* in a bin directory, which the test hands TREK as its client-tool directory.
|
||||
*
|
||||
* A stand-in writes its argv and its PG* environment to `<bin>/<name>.log`
|
||||
* before anything else it does, so readToolCall() says what the tool was given.
|
||||
* The log holds the last call. What a call does is a list of shell snippets,
|
||||
* run in order: the builders below name what a test sets up (answersSchema,
|
||||
* writesFile, failsWith, ...), so a test reads as the scenario it describes.
|
||||
* Output files are found by their `--file=` argument, not by position.
|
||||
*/
|
||||
import { chmodSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
export type PgToolName = 'pg_dump' | 'pg_restore' | 'psql';
|
||||
|
||||
export interface FakeToolOptions {
|
||||
/** What `--version` answers after "PostgreSQL". Defaults to 17.11. */
|
||||
version?: string;
|
||||
/** Shell fragments run after the call is logged, in order. */
|
||||
snippets?: readonly string[];
|
||||
}
|
||||
|
||||
/** What a stand-in was called with: its arguments, and its PG* environment. */
|
||||
export interface ToolCall {
|
||||
args: string[];
|
||||
env: Record<string, string>;
|
||||
}
|
||||
|
||||
/** A value as one single-quoted shell word. */
|
||||
function shellWord(value: string): string {
|
||||
return `'${value.replaceAll("'", "'\\''")}'`;
|
||||
}
|
||||
|
||||
/** Answers a call whose arguments contain `pattern` with `text`, and ends the call. */
|
||||
export function printsWhen(pattern: string, text: string): string {
|
||||
return `case "$*" in *${pattern}*) printf '%s' ${shellWord(text)}; exit 0;; esac`;
|
||||
}
|
||||
|
||||
/** psql's current-schema answer: the schema's name, and its quote_ident spelling, as pgCurrentSchema reads them. */
|
||||
export function answersSchema(name = 'public', quoted = name): string {
|
||||
return printsWhen('quote_ident', `${name}\t${quoted}\n`);
|
||||
}
|
||||
|
||||
/** psql's table listing, one name per line, as pgTableNames reads it. */
|
||||
export function answersTables(names: readonly string[]): string {
|
||||
return printsWhen('table_name', names.map((name) => `${name}\n`).join(''));
|
||||
}
|
||||
|
||||
/** Writes `content` to the output file the call names with `--file=`. */
|
||||
export function writesFile(content: string): string {
|
||||
return `printf '%s' ${shellWord(content)} > "$(fake_file_target "$@")"`;
|
||||
}
|
||||
|
||||
/** Writes the start of a restore script that restricts psql (CVE-2025-8714) and creates TREK's migration table. */
|
||||
export function writesRestrictedScript(table = 'public.mikro_orm_migrations'): string {
|
||||
return writesFile(`\\restrict k1\nCREATE TABLE ${table} (\n);\n`);
|
||||
}
|
||||
|
||||
/** Answers a `--data-only` call with the COPY block of `mikro_orm_migrations`, one row per name. */
|
||||
export function listsMigrations(names: readonly string[]): string {
|
||||
const rows = names.map((name, i) => `${i + 1}\t${name}\tx\n`).join('');
|
||||
const copy = `COPY public.mikro_orm_migrations (id, name, executed_at) FROM stdin;\n${rows}\\.\n`;
|
||||
return `case "$*" in *--data-only*) printf '%s' ${shellWord(copy)}; exit 0;; esac`;
|
||||
}
|
||||
|
||||
/** Fails the way a tool that cannot connect does: the message on stderr, then a non-zero exit. */
|
||||
export function failsWith(message: string, exitCode = 1): string {
|
||||
return `printf '%s\\n' ${shellWord(message)} >&2; exit ${exitCode}`;
|
||||
}
|
||||
|
||||
/** Creates `file`: a marker that the tool ran. */
|
||||
export function touches(file: string): string {
|
||||
return `touch ${shellWord(file)}`;
|
||||
}
|
||||
|
||||
/** Copies the `--file=` output whose path ends with `suffix` to `destination`, so a test can read a work file the tool has since removed. */
|
||||
export function copiesFileEndingWith(suffix: string, destination: string): string {
|
||||
return `for a in "$@"; do case "$a" in --file=*${suffix}) cp "\${a#--file=}" ${shellWord(destination)};; esac; done`;
|
||||
}
|
||||
|
||||
/** The function every stand-in uses to find its `--file=` argument, wherever it sits. */
|
||||
const FILE_TARGET_FUNCTION =
|
||||
'fake_file_target() { for a in "$@"; do case "$a" in --file=*) printf \'%s\' "${a#--file=}";; esac; done; }';
|
||||
|
||||
/**
|
||||
* Installs the stand-in `name` in `bin` and returns the path of its call log.
|
||||
* The script is executable and runs by its absolute path, as TREK runs it.
|
||||
*/
|
||||
export function installFakeTool(bin: string, name: PgToolName, options: FakeToolOptions = {}): string {
|
||||
const { version = '17.11', snippets = [] } = options;
|
||||
const log = path.join(bin, `${name}.log`);
|
||||
const script = [
|
||||
'#!/bin/sh',
|
||||
FILE_TARGET_FUNCTION,
|
||||
`[ "$1" = --version ] && { echo '${name} (PostgreSQL) ${version} (Debian)'; exit 0; }`,
|
||||
`printf '%s\\n' "$@" > ${shellWord(log)}`,
|
||||
`env | grep '^PG' | sort >> ${shellWord(log)}`,
|
||||
...snippets,
|
||||
'',
|
||||
].join('\n');
|
||||
writeFileSync(path.join(bin, name), script);
|
||||
chmodSync(path.join(bin, name), 0o755);
|
||||
return log;
|
||||
}
|
||||
|
||||
/** What the stand-in logged last: its arguments, and the PG* variables it was run with. */
|
||||
export function readToolCall(log: string): ToolCall {
|
||||
const lines = readFileSync(log, 'utf8')
|
||||
.split('\n')
|
||||
.filter((line) => line !== '');
|
||||
const env: Record<string, string> = {};
|
||||
for (const line of lines.filter((line) => line.startsWith('PG'))) {
|
||||
const eq = line.indexOf('=');
|
||||
env[line.slice(0, eq)] = line.slice(eq + 1);
|
||||
}
|
||||
return { args: lines.filter((line) => !line.startsWith('PG')), env };
|
||||
}
|
||||
|
||||
/** Every argument that starts with `flag`, with the flag removed (`--file=` gives the file paths). */
|
||||
export function flagValues(args: readonly string[], flag: string): string[] {
|
||||
return args.filter((arg) => arg.startsWith(flag)).map((arg) => arg.slice(flag.length));
|
||||
}
|
||||
@@ -152,7 +152,7 @@ export function describeLegacyUpgrade(
|
||||
it(`${c.id}-005: a second bootstrap (a restart, or a restore of this file) baselines nothing and applies nothing`, async () => {
|
||||
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
try {
|
||||
await runSchemaBootstrap(app.get(MikroORM));
|
||||
await runSchemaBootstrap(app.get(MikroORM), { snapshot: true });
|
||||
const lines = logSpy.mock.calls.map((args) => args.map(String).join(' '));
|
||||
expect(
|
||||
lines.filter((line) => line.startsWith('[DB] Legacy install') || line.startsWith('[DB] Applying')),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Addons } from '../../src/db/entities/Addons.entity';
|
||||
import { AppSettings } from '../../src/db/entities/AppSettings.entity';
|
||||
import { PhotoProviderFields } from '../../src/db/entities/PhotoProviderFields.entity';
|
||||
@@ -42,6 +43,6 @@ export async function createTestAddonsService(db: Database.Database): Promise<Ad
|
||||
t.repo(PhotoProviderFields),
|
||||
t.repo(AppSettings),
|
||||
t.repo(Users),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AppSettings } from '../../src/db/entities/AppSettings.entity';
|
||||
import { AssignmentParticipants } from '../../src/db/entities/AssignmentParticipants.entity';
|
||||
import { Categories } from '../../src/db/entities/Categories.entity';
|
||||
@@ -143,7 +144,7 @@ export function createTestUnitOfWork(db: Database.Database): Promise<UnitOfWork>
|
||||
// which is exactly what no-misused-promises/checksConditionals rejects.
|
||||
const existing = perHandle.get(db);
|
||||
if (existing !== undefined) return existing;
|
||||
const pending = sharedTestOrm(db).then((t) => new UnitOfWork(t.em));
|
||||
const pending = sharedTestOrm(db).then((t) => new UnitOfWork(t.em, new SqliteEngine()));
|
||||
perHandle.set(db, pending);
|
||||
return pending;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
import { getMcpSafeUrl } from '../../src/app-config';
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { db as testDb } from '../../src/db/database';
|
||||
import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AuditLog } from '../../src/db/entities/AuditLog.entity';
|
||||
import { McpTokens } from '../../src/db/entities/McpTokens.entity';
|
||||
import { OauthClients } from '../../src/db/entities/OauthClients.entity';
|
||||
@@ -85,7 +86,7 @@ beforeAll(async () => {
|
||||
t.repo(OauthConsents),
|
||||
await createTestAddonsService(testDb),
|
||||
new AuditService(t.repo(AuditLog), t.repo(Users)),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
nestApp.get(CimdClientResolver),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { db as testDb } from '../../src/db/database';
|
||||
import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Addons } from '../../src/db/entities/Addons.entity';
|
||||
import { AuditLog } from '../../src/db/entities/AuditLog.entity';
|
||||
import { OauthClients } from '../../src/db/entities/OauthClients.entity';
|
||||
@@ -96,7 +97,7 @@ beforeAll(async () => {
|
||||
t.repo(OauthConsents),
|
||||
await createTestAddonsService(testDb),
|
||||
new AuditService(t.repo(AuditLog), t.repo(Users)),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
nestApp.get(CimdClientResolver),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { db as testDb } from '../../src/db/database';
|
||||
import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Addons } from '../../src/db/entities/Addons.entity';
|
||||
import { AuditLog } from '../../src/db/entities/AuditLog.entity';
|
||||
import { OauthClients } from '../../src/db/entities/OauthClients.entity';
|
||||
@@ -97,7 +98,7 @@ beforeAll(async () => {
|
||||
t.repo(OauthConsents),
|
||||
await createTestAddonsService(testDb),
|
||||
new AuditService(t.repo(AuditLog), t.repo(Users)),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
nestApp.get(CimdClientResolver),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -468,6 +468,23 @@ describe('Search places', () => {
|
||||
expect(res.body.places).toHaveLength(1);
|
||||
expect(res.body.places[0].name).toBe('Scenic Place');
|
||||
});
|
||||
|
||||
it('PLACE-017c — GET ?category= or ?tag= with an id that is not a number matches nothing', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
createPlace(testDb, trip.id, { name: 'Somewhere' });
|
||||
|
||||
const queries = ['category=abc', 'tag=abc', 'category=1.5', 'tag=-1', 'category=0', 'tag=0'];
|
||||
const responses = await Promise.all(
|
||||
queries.map((query) =>
|
||||
request(app).get(`/api/trips/${trip.id}/places?${query}`).set('Cookie', authCookie(user.id)),
|
||||
),
|
||||
);
|
||||
for (const [i, res] of responses.entries()) {
|
||||
expect(res.status, queries[i]).toBe(200);
|
||||
expect(res.body.places, queries[i]).toEqual([]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
* asserts the plugin still comes up clean.
|
||||
*/
|
||||
import { db as testDb } from '../../../src/db/database';
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Addons } from '../../../src/db/entities/Addons.entity';
|
||||
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
@@ -173,7 +174,7 @@ describe('plugin boot vs registry scan ordering', () => {
|
||||
(t as TestOrm).repo(NotificationChannelPreferences),
|
||||
// Plan 4 Task 4: `uow` is no longer `@Optional()` — ordered ahead
|
||||
// of `registry?`/`hostFactory?`, matching the real constructor.
|
||||
new UnitOfWork((t as TestOrm).em),
|
||||
new UnitOfWork((t as TestOrm).em, new SqliteEngine()),
|
||||
undefined,
|
||||
hostFactory,
|
||||
t?.orm,
|
||||
@@ -264,7 +265,7 @@ describe('plugin boot vs registry scan ordering', () => {
|
||||
t2.repo(PhotoProviderFields),
|
||||
t2.repo(AppSettings),
|
||||
t2.repo(Users),
|
||||
new UnitOfWork(t2.em),
|
||||
new UnitOfWork(t2.em, new SqliteEngine()),
|
||||
);
|
||||
const auditLogRepo2 = t2.repo(AuditLog);
|
||||
const usersRepo2 = t2.repo(Users);
|
||||
@@ -293,7 +294,7 @@ describe('plugin boot vs registry scan ordering', () => {
|
||||
t2.repo(PluginCapabilityAudit),
|
||||
t2.repo(Settings),
|
||||
t2.repo(NotificationChannelPreferences),
|
||||
new UnitOfWork(t2.em),
|
||||
new UnitOfWork(t2.em, new SqliteEngine()),
|
||||
undefined,
|
||||
hostFactory2,
|
||||
t2.orm,
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* so every one of the 14 repositories this task injects reads/writes real columns,
|
||||
* not a hand-slimmed fixture table.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
@@ -76,7 +77,7 @@ async function buildRuntime(registrar?: CronRegistrarService): Promise<PluginRun
|
||||
const audit = new AuditService(t.repo(AuditLog), t.repo(Users));
|
||||
const addons = await createTestAddonsService(testDb);
|
||||
const userSettings = new PluginUserSettingsService(t.repo(PluginSettingsFields), t.repo(PluginUserConfig));
|
||||
const uow = new UnitOfWork(t.em);
|
||||
const uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
return new PluginRuntimeService(
|
||||
audit,
|
||||
addons,
|
||||
@@ -218,7 +219,7 @@ describe('Plan 3j Task 2 — R-install-gates named accept+refuse pairs (PR17/PR2
|
||||
t.repo(PluginCapabilityAudit),
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
fakeRegistry(['db:own']),
|
||||
);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -254,7 +255,7 @@ describe('Plan 3j Task 2 — R-install-gates named accept+refuse pairs (PR17/PR2
|
||||
t.repo(PluginCapabilityAudit),
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
fakeRegistry(['db:own', 'db:read:trips']),
|
||||
);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -303,7 +304,7 @@ describe('Plan 3j Task 2 — R-install-gates named accept+refuse pairs (PR17/PR2
|
||||
t.repo(PluginCapabilityAudit),
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
registry,
|
||||
);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -342,7 +343,7 @@ describe('Plan 3j Task 2 — R-install-gates named accept+refuse pairs (PR17/PR2
|
||||
t.repo(PluginCapabilityAudit),
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
registry,
|
||||
);
|
||||
await expect(rtR.retrust('pr28-refuse', '2.0.0', 'NEWKEY', { userId: 1 })).rejects.toThrow(/nothing to re-trust/);
|
||||
|
||||
@@ -59,6 +59,7 @@
|
||||
* plugin-runtime.service.ts's own comment at the wrap site) already ran inside a
|
||||
* request context by the time this file's shared `runtime` finished booting.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
@@ -158,7 +159,7 @@ async function buildRuntime(registrar?: CronRegistrarService, orm?: TestOrm['orm
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
// Plan 4 Task 4: `uow` is no longer `@Optional()`.
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
undefined,
|
||||
undefined,
|
||||
orm,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
* install a pinned version through the full verify -> extract -> validate ->
|
||||
* move -> register pipeline (with the network download mocked).
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
|
||||
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
|
||||
@@ -148,7 +149,7 @@ beforeEach(() => {
|
||||
t.repo(PluginActions),
|
||||
t.repo(PluginSettingsFields),
|
||||
t.repo(PluginErrorLog),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
);
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { buildOrmConfig, resolveEngine } from '../../src/db/engine';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
|
||||
/**
|
||||
* Brings the template database TREK_DB_URL names to the current schema, the
|
||||
* way a first boot on an empty Postgres database does: the engine's own
|
||||
* bootstrap (baseline, the migrations after its cut, seeders), with no
|
||||
* pre-migrate copy, since the database is new.
|
||||
*/
|
||||
async function main(): Promise<void> {
|
||||
const engine = resolveEngine();
|
||||
if (engine.id !== 'postgres') throw new Error('build-template.ts runs on the Postgres engine only (TREK_DB_URL).');
|
||||
const config = buildOrmConfig(engine);
|
||||
const orm = await MikroORM.init({
|
||||
...config,
|
||||
// As src/db/standalone-orm.ts: the bootstrap runs SQL and seeders, which
|
||||
// need no entity metadata.
|
||||
entities: [],
|
||||
entitiesTs: [],
|
||||
discovery: { warnWhenNoEntities: false },
|
||||
migrations: { ...config.migrations, silent: true },
|
||||
});
|
||||
try {
|
||||
await engine.schemaBootstrap(orm, { snapshot: false });
|
||||
process.stdout.write(`[test-db] Postgres template ready: ${engine.describe()}\n`);
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((err: unknown) => {
|
||||
process.stderr.write(
|
||||
`[test-db] failed to build the Postgres template: ${err instanceof Error ? err.stack : String(err)}\n`,
|
||||
);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import { MikroORM } from '@mikro-orm/postgresql';
|
||||
|
||||
/**
|
||||
* The databases the Postgres lane creates on the server TREK_TEST_DB_URL names:
|
||||
* one migrated template per run, and one copy of it per test file.
|
||||
*/
|
||||
export const TEMPLATE_DATABASE = 'trek_test_template';
|
||||
export const WORKER_DATABASE_PREFIX = 'trek_test_w';
|
||||
|
||||
/** `adminUrl` pointed at `database`, everything else (credentials, host, sslmode) kept. */
|
||||
export function databaseUrl(adminUrl: string, database: string): string {
|
||||
const url = new URL(adminUrl);
|
||||
url.pathname = `/${database}`;
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
type Run = <T = unknown>(sql: string, params?: unknown[]) => Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Runs `fn` on a connection to the admin database, outside any transaction
|
||||
* (CREATE and DROP DATABASE refuse to run in one), through the same Postgres
|
||||
* driver the app uses.
|
||||
*/
|
||||
export async function withAdmin<T>(adminUrl: string, fn: (run: Run) => Promise<T>): Promise<T> {
|
||||
const orm = await MikroORM.init({
|
||||
clientUrl: adminUrl,
|
||||
entities: [],
|
||||
discovery: { warnWhenNoEntities: false },
|
||||
pool: { min: 0, max: 1 },
|
||||
});
|
||||
try {
|
||||
const connection = orm.em.getConnection();
|
||||
return await fn((sql, params = []) => connection.execute(sql, params, 'all'));
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { databaseUrl, TEMPLATE_DATABASE, WORKER_DATABASE_PREFIX, withAdmin } from './databases';
|
||||
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
|
||||
/**
|
||||
* Runs once per `npm run test:pg`, before any worker starts: the Postgres
|
||||
* counterpart of tests/global-setup.ts's schema snapshot. One template
|
||||
* database is created and brought to the current schema the way a first boot
|
||||
* does (baseline, migrations after its cut, seeders); every test file then
|
||||
* gets a copy of it (tests/postgres/setup.ts), which Postgres makes at the
|
||||
* file level, far faster than migrating per file.
|
||||
*
|
||||
* TREK_TEST_DB_URL is required: a lane that skipped without it would pass
|
||||
* having tested nothing.
|
||||
*/
|
||||
export async function setup(): Promise<void> {
|
||||
const adminUrl = process.env.TREK_TEST_DB_URL;
|
||||
if (!adminUrl) {
|
||||
throw new Error(
|
||||
'The Postgres lane needs TREK_TEST_DB_URL: a database it may create and drop databases from, ' +
|
||||
'e.g. postgres://postgres:trek@127.0.0.1:55432/postgres.',
|
||||
);
|
||||
}
|
||||
|
||||
// Left behind by an earlier run that was killed; nothing else uses these names.
|
||||
await dropLaneDatabases(adminUrl);
|
||||
await withAdmin(adminUrl, (run) => run(`CREATE DATABASE "${TEMPLATE_DATABASE}"`));
|
||||
|
||||
// A child process under tsx, as tests/global-setup.ts does, for the same
|
||||
// reason: globalSetup runs outside vitest's transform pipeline.
|
||||
execFileSync(process.execPath, ['--import', 'tsx', path.join(__dirname, 'build-template.ts')], {
|
||||
cwd: path.join(__dirname, '..', '..'),
|
||||
stdio: ['ignore', 'inherit', 'inherit'],
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: 'test',
|
||||
ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
|
||||
LOG_LEVEL: 'error',
|
||||
TREK_DB_URL: databaseUrl(adminUrl, TEMPLATE_DATABASE),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** The template and every worker's copy: names the lane alone uses (a prefix compared as text, not a LIKE pattern). */
|
||||
async function dropLaneDatabases(adminUrl: string): Promise<void> {
|
||||
await withAdmin(adminUrl, async (run) => {
|
||||
const lane = await run<{ datname: string }>(
|
||||
'select datname from pg_database where datname = ? or left(datname, length(?)) = ?',
|
||||
[TEMPLATE_DATABASE, WORKER_DATABASE_PREFIX, WORKER_DATABASE_PREFIX],
|
||||
);
|
||||
await Promise.all(lane.map((row) => run(`DROP DATABASE IF EXISTS "${row.datname}" WITH (FORCE)`)));
|
||||
});
|
||||
}
|
||||
|
||||
/** After the run: the lane leaves no database behind. */
|
||||
export async function teardown(): Promise<void> {
|
||||
const adminUrl = process.env.TREK_TEST_DB_URL;
|
||||
if (adminUrl) await dropLaneDatabases(adminUrl);
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* PARITY-PG — the schema a Postgres database ends with after its bootstrap
|
||||
* (the baseline, then every migration after its cut) is the schema the
|
||||
* entities describe, as scripts/generate-pg-baseline.ts writes it for
|
||||
* Postgres (src/db/engine/postgres/schema-adjust.ts). MikroORM's comparator
|
||||
* reads the live database and must find nothing to change: a migration after
|
||||
* the cut that left Postgres behind SQLite, or a baseline that drifted from
|
||||
* the entities, fails here. It runs on the lane's database, never skipped.
|
||||
*
|
||||
* The comparator also reports statements that change nothing, and each kind
|
||||
* is proven so rather than recognised by its text:
|
||||
* - `drop not null` on a primary key column: SQLite lets a key column be null
|
||||
* and the entities say so; Postgres keeps every primary key NOT NULL (and
|
||||
* refuses the statement), checked against the live keys.
|
||||
* - a column default or a check constraint Postgres stores in its own spelling
|
||||
* (casts added, EXTRACT rewritten, BETWEEN as two comparisons): such
|
||||
* statements are applied in a transaction that is rolled back, and the
|
||||
* catalogue's own rendering of every default and check must read the same
|
||||
* after as before. A default or a bound that really differs changes it.
|
||||
* - dropping a trigger the entities cannot carry: only the triggers ported by
|
||||
* hand (POSTGRES_TRIGGERS), by exact name, and each of them must exist.
|
||||
*/
|
||||
import { POSTGRES_TRIGGERS } from '../../src/db/engine/postgres/postgres-triggers';
|
||||
import { applyPostgresColumnAdjustments } from '../../src/db/engine/postgres/schema-adjust';
|
||||
import { ALL_ENTITIES } from '../../src/db/entities';
|
||||
import type { Transaction } from '@mikro-orm/core';
|
||||
import { MikroORM } from '@mikro-orm/postgresql';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
const PORTED = Object.keys(POSTGRES_TRIGGERS);
|
||||
const DROP_NOT_NULL = /^alter table "(\w+)" alter column "(\w+)" drop not null$/;
|
||||
const SPELLING =
|
||||
/^alter table "\w+" (?:alter column "\w+" set default .+|drop constraint "\w+"|add constraint "\w+" check \(.+\))$/s;
|
||||
/** MikroORM drops a trigger by its name, and the function it would have made for it as `<table>_<trigger>_fn`. */
|
||||
const DROP_TRIGGER = /^drop trigger if exists "(\w+)" on "(\w+)"$/;
|
||||
const DROP_FUNCTION = /^drop function if exists "(\w+)"\(\)$/;
|
||||
|
||||
/** Every column default and check constraint of the schema, as the catalogue renders them. */
|
||||
const FINGERPRINT_SQL = `select string_agg(item, E'\\n' order by item) as fingerprint from (
|
||||
select 'default ' || c.relname || '.' || a.attname || ' = ' || pg_get_expr(d.adbin, d.adrelid) as item
|
||||
from pg_attrdef d join pg_class c on c.oid = d.adrelid join pg_attribute a on a.attrelid = d.adrelid and a.attnum = d.adnum
|
||||
where c.relnamespace = current_schema()::regnamespace
|
||||
union all
|
||||
select 'check ' || conrelid::regclass || '.' || conname || ' = ' || pg_get_constraintdef(oid)
|
||||
from pg_constraint where contype = 'c' and connamespace = current_schema()::regnamespace
|
||||
) items`;
|
||||
|
||||
const ROLLBACK = new Error('roll back the spelling check');
|
||||
|
||||
async function liveSets(orm: MikroORM): Promise<{ primaryKeys: Set<string>; triggers: Set<string> }> {
|
||||
const connection = orm.em.getConnection();
|
||||
const names = (sql: string) =>
|
||||
connection.execute<Array<{ name: string }>>(sql).then((rows) => new Set(rows.map((row) => row.name)));
|
||||
const [primaryKeys, triggers] = await Promise.all([
|
||||
names(
|
||||
`select c.relname || '.' || a.attname as name from pg_index i
|
||||
join pg_class c on c.oid = i.indrelid join pg_attribute a on a.attrelid = i.indrelid and a.attnum = any(i.indkey)
|
||||
where i.indisprimary and c.relnamespace = current_schema()::regnamespace`,
|
||||
),
|
||||
names(
|
||||
`select c.relname || '.' || t.tgname as name from pg_trigger t join pg_class c on c.oid = t.tgrelid
|
||||
where not t.tgisinternal and c.relnamespace = current_schema()::regnamespace`,
|
||||
),
|
||||
]);
|
||||
return { primaryKeys, triggers };
|
||||
}
|
||||
|
||||
/** Whether applying `statements` leaves every default and check constraint reading as it did; nothing is kept. */
|
||||
async function changesNothing(
|
||||
orm: MikroORM,
|
||||
statements: readonly string[],
|
||||
): Promise<{ before: string; after: string }> {
|
||||
const connection = orm.em.getConnection();
|
||||
const fingerprint = async (trx?: Transaction) =>
|
||||
(await connection.execute<Array<{ fingerprint: string }>>(FINGERPRINT_SQL, [], 'all', trx))[0].fingerprint;
|
||||
const before = await fingerprint();
|
||||
let after = '';
|
||||
await connection
|
||||
.transactional(async (trx) => {
|
||||
// One after the other: a drop and the add that follows name the same constraint.
|
||||
await statements.reduce<Promise<unknown>>(
|
||||
(previous, statement) => previous.then(() => connection.execute(statement, [], 'run', trx)),
|
||||
Promise.resolve(),
|
||||
);
|
||||
after = await fingerprint(trx);
|
||||
throw ROLLBACK;
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (err !== ROLLBACK) throw err;
|
||||
});
|
||||
return { before, after };
|
||||
}
|
||||
|
||||
describe('PARITY-PG', () => {
|
||||
it('PARITY-PG-001: the bootstrapped database holds exactly the schema the entities generate for Postgres', async () => {
|
||||
const orm = await MikroORM.init({
|
||||
clientUrl: process.env.TREK_DB_URL,
|
||||
entities: [...ALL_ENTITIES],
|
||||
discovery: { warnWhenNoEntities: false },
|
||||
});
|
||||
try {
|
||||
for (const meta of orm.getMetadata().getAll().values()) {
|
||||
applyPostgresColumnAdjustments(meta.props);
|
||||
}
|
||||
const live = await liveSets(orm);
|
||||
const statements = (await orm.schema.getUpdateSchemaSQL({ wrap: false }))
|
||||
.split(';\n')
|
||||
.map((statement) => statement.trim().replace(/;$/, ''))
|
||||
.filter((statement) => statement !== '');
|
||||
|
||||
const isKeyNotNull = (s: string) => {
|
||||
const m = DROP_NOT_NULL.exec(s);
|
||||
return m !== null && live.primaryKeys.has(`${m[1]}.${m[2]}`);
|
||||
};
|
||||
const isPortedTrigger = (s: string) => {
|
||||
const trigger = DROP_TRIGGER.exec(s);
|
||||
if (trigger) return PORTED.includes(trigger[1]) && live.triggers.has(`${trigger[2]}.${trigger[1]}`);
|
||||
const fn = DROP_FUNCTION.exec(s);
|
||||
return fn !== null && PORTED.some((name) => fn[1].endsWith(`_${name}_fn`));
|
||||
};
|
||||
const spelling = statements.filter((s) => !isKeyNotNull(s) && !isPortedTrigger(s) && SPELLING.test(s));
|
||||
|
||||
expect(statements.filter((s) => !isKeyNotNull(s) && !isPortedTrigger(s) && !SPELLING.test(s))).toEqual([]);
|
||||
const { before, after } = await changesNothing(orm, spelling);
|
||||
expect(after).toBe(before);
|
||||
// Every hand-ported trigger is in the database, on its table.
|
||||
expect(PORTED.filter((name) => ![...live.triggers].some((t) => t.endsWith(`.${name}`)))).toEqual([]);
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 60_000);
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* PGSEM-001..002 — what SQLite gives for free and Postgres only through the
|
||||
* engine, against the real app on the lane's Postgres database:
|
||||
* writes in a UnitOfWork run one at a time, as on SQLite's single writer, so
|
||||
* a read-then-insert never races; counts come back as numbers; and a search
|
||||
* ignores case as SQLite's LIKE does.
|
||||
*/
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { ilike } from '../../src/db/dialect/sql-functions';
|
||||
import { withRequestContext } from '../../src/nest/database/request-context';
|
||||
import { UnitOfWork } from '../../src/nest/database/unit-of-work';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
let app: INestApplication;
|
||||
let orm: MikroORM;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await buildApp();
|
||||
orm = app.get(MikroORM);
|
||||
}, 60_000);
|
||||
|
||||
afterAll(async () => {
|
||||
await app?.close();
|
||||
});
|
||||
|
||||
/** Rows of `sql` read through the current (transactional) EntityManager. */
|
||||
function query<T>(sql: string, params: unknown[] = []): Promise<T[]> {
|
||||
return orm.em.getConnection().execute<T[]>(sql, params, 'all', orm.em.getTransactionContext());
|
||||
}
|
||||
|
||||
describe('Postgres keeps SQLite semantics', () => {
|
||||
it('PGSEM-001: concurrent read-then-insert units of work never see the same count, and counts are numbers', async () => {
|
||||
const uow = app.get(UnitOfWork, { strict: false });
|
||||
const claim = () =>
|
||||
withRequestContext(orm, () =>
|
||||
uow.transactional(async () => {
|
||||
const [{ n }] = await query<{ n: number }>(
|
||||
"select count(*) as n from categories where name like 'pg-lock-%'",
|
||||
);
|
||||
// Long enough for every other unit of work to have started and read too, were nothing holding them.
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
await query('insert into categories (name, color, icon) values (?, ?, ?)', [`pg-lock-${n}`, '#000000', 'x']);
|
||||
return n;
|
||||
}),
|
||||
);
|
||||
|
||||
const claimed = await Promise.all([claim(), claim(), claim(), claim()]);
|
||||
|
||||
expect(claimed.every((n) => typeof n === 'number')).toBe(true);
|
||||
expect([...claimed].sort((a, b) => a - b)).toEqual([0, 1, 2, 3]);
|
||||
});
|
||||
|
||||
it('PGSEM-002: a search ignores case, as LIKE does on SQLite', async () => {
|
||||
const rows = await withRequestContext(orm, () =>
|
||||
query<{ name: string }>(`select name from categories where ${ilike(orm.em.getPlatform(), ['name'])}`, [
|
||||
'%HOTEL%',
|
||||
]),
|
||||
);
|
||||
const names = rows.map((row) => row.name);
|
||||
expect(names).toContain('Hotel');
|
||||
expect(names.every((name) => /hotel/i.test(name))).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
// The Postgres lane's per-file setup (vitest.pg.config.ts), after tests/setup.ts:
|
||||
// every test file runs on a fresh copy of the migrated template, under a name of
|
||||
// its worker's own, so files never see each other's rows and workers never share
|
||||
// a database. TREK_DB_URL is set here, before the test file imports anything
|
||||
// that reads it (src/mikro-orm.config.ts reads it on import); the copy itself is
|
||||
// made in a beforeAll, which runs before the test file's own (where the app
|
||||
// connects).
|
||||
import { databaseUrl, TEMPLATE_DATABASE, WORKER_DATABASE_PREFIX, withAdmin } from './databases';
|
||||
|
||||
import { beforeAll } from 'vitest';
|
||||
|
||||
/** Copies of the template are made one at a time: Postgres refuses to copy a template another copy is reading. */
|
||||
const COPY_LOCK = 0x54524b54;
|
||||
const COPY_ATTEMPTS = 3;
|
||||
const COPY_RETRY_MS = 500;
|
||||
const COPY_TIMEOUT_MS = 60_000;
|
||||
|
||||
const adminUrl = process.env.TREK_TEST_DB_URL as string;
|
||||
const poolId = process.env.VITEST_POOL_ID ?? '0';
|
||||
if (!/^\d+$/.test(poolId)) throw new Error(`unexpected VITEST_POOL_ID ${poolId}`);
|
||||
const database = `${WORKER_DATABASE_PREFIX}${poolId}`;
|
||||
|
||||
process.env.TREK_DB_URL = databaseUrl(adminUrl, database);
|
||||
|
||||
async function copyTemplate(attempt = 1): Promise<void> {
|
||||
try {
|
||||
await withAdmin(adminUrl, async (run) => {
|
||||
// One connection (withAdmin's pool holds one), so the session lock covers both statements.
|
||||
await run('select pg_advisory_lock(?)', [COPY_LOCK]);
|
||||
try {
|
||||
await run(`DROP DATABASE IF EXISTS "${database}" WITH (FORCE)`);
|
||||
await run(`CREATE DATABASE "${database}" TEMPLATE "${TEMPLATE_DATABASE}"`);
|
||||
} finally {
|
||||
await run('select pg_advisory_unlock(?)', [COPY_LOCK]);
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
if (attempt >= COPY_ATTEMPTS) throw err;
|
||||
await new Promise((resolve) => setTimeout(resolve, COPY_RETRY_MS * attempt));
|
||||
await copyTemplate(attempt + 1);
|
||||
}
|
||||
}
|
||||
|
||||
beforeAll(() => copyTemplate(), COPY_TIMEOUT_MS);
|
||||
@@ -396,6 +396,34 @@ describe('deriveDb: the pre-migration snapshot', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('deriveDb: the engine', () => {
|
||||
it('DB-ENGINE-001: SQLite unless TREK_DB_URL names a database', () => {
|
||||
expect(deriveDb({})).toMatchObject({ engine: 'sqlite' });
|
||||
expect(deriveDb({ TREK_DB_URL: ' ' })).toMatchObject({ engine: 'sqlite' });
|
||||
expect(deriveDb({ TREK_DB_URL: ' postgres://u:p@db:5432/trek ' })).toMatchObject({
|
||||
engine: 'postgres',
|
||||
url: 'postgres://u:p@db:5432/trek',
|
||||
});
|
||||
});
|
||||
|
||||
it('DB-ENGINE-002: a pool of ten connections unless told otherwise, from 1 to 100', () => {
|
||||
const poolMaxOf = (raw: Record<string, string>) => {
|
||||
const db = deriveDb({ TREK_DB_URL: 'postgres://u:p@db:5432/trek', ...raw });
|
||||
return db.engine === 'postgres' ? db.poolMax : null;
|
||||
};
|
||||
expect(deriveDb({ TREK_DB_URL: 'postgres://u:p@db:5432/trek' })).toMatchObject({ engine: 'postgres' });
|
||||
expect(poolMaxOf({})).toBe(10);
|
||||
expect(poolMaxOf({ TREK_DB_POOL_MAX: '25' })).toBe(25);
|
||||
expect(poolMaxOf({ TREK_DB_POOL_MAX: '0' })).toBe(10);
|
||||
expect(poolMaxOf({ TREK_DB_POOL_MAX: '500' })).toBe(100);
|
||||
});
|
||||
|
||||
it('DB-ENGINE-003: no pool size on SQLite, which has no pool', () => {
|
||||
expect(deriveDb({ TREK_DB_POOL_MAX: '25' })).toMatchObject({ engine: 'sqlite' });
|
||||
expect(deriveDb({})).not.toHaveProperty('poolMax');
|
||||
});
|
||||
});
|
||||
|
||||
describe('deriveNet', () => {
|
||||
it('ALLOW_INTERNAL_NETWORK coerces the boolean-like family (ssrfGuard)', () => {
|
||||
expect(deriveNet({ ALLOW_INTERNAL_NETWORK: 'True' }).allowInternalNetwork).toBe(true);
|
||||
|
||||
@@ -341,3 +341,40 @@ describe('validateEnvAtBoot: the pre-migration snapshot switches', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateEnvAtBoot: the database engine', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('DB-BOOT-001: refuses a TREK_DB_URL that is not a postgres:// or postgresql:// URL, without printing it', () => {
|
||||
for (const value of ['mysql://u:secret@db/trek', 'not a url', 'sqlite:///data/travel.db']) {
|
||||
const { printed, thrown } = bootReport({ TREK_DB_URL: value });
|
||||
expect(thrown, value).toMatch(/Invalid environment configuration/);
|
||||
expect(printed, value).toContain('TREK_DB_URL=***');
|
||||
expect(printed, value).not.toContain('secret');
|
||||
}
|
||||
});
|
||||
|
||||
it('DB-BOOT-002: masks TREK_DB_URL like every other secret', () => {
|
||||
expect(SECRET_ENV_KEYS.has('TREK_DB_URL')).toBe(true);
|
||||
});
|
||||
|
||||
it('DB-BOOT-003: refuses TREK_DB_URL together with a SQLite setting, naming both', () => {
|
||||
for (const key of ['TREK_DB_FILE', 'TREK_DB_JOURNAL_MODE', 'TREK_DB_SYNCHRONOUS']) {
|
||||
const { thrown, printed } = bootReport({ TREK_DB_URL: 'postgres://u:p@db/trek', [key]: 'x' });
|
||||
expect(thrown, key).toMatch(/Invalid environment configuration/);
|
||||
expect(printed, key).toContain('ambiguous database configuration');
|
||||
expect(printed, key).toContain(key);
|
||||
}
|
||||
});
|
||||
|
||||
it('DB-BOOT-004: accepts a Postgres database on its own', () => {
|
||||
expect(() => validateEnvAtBoot({ TREK_DB_URL: 'postgresql://u:p@db:5432/trek?sslmode=require' })).not.toThrow();
|
||||
});
|
||||
|
||||
it('DB-BOOT-005: refuses a pool size outside 1 to 100', () => {
|
||||
expect(bootReport({ TREK_DB_POOL_MAX: '0' }).thrown).toMatch(/Invalid environment configuration/);
|
||||
expect(() => validateEnvAtBoot({ TREK_DB_POOL_MAX: '20' })).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* on another install. The run now goes through backupService.createBackup(),
|
||||
* which snapshots the DB with VACUUM INTO and bundles the key.
|
||||
*/
|
||||
import type { MaintenanceRepository } from '../../src/db/repositories/MaintenanceRepository';
|
||||
import type { SqliteMaintenance } from '../../src/db/engine/sqlite/sqlite-maintenance';
|
||||
import { AutoBackupJob } from '../../src/nest/backup/auto-backup.job';
|
||||
import { createBackup } from '../../src/nest/backup/backup.impl';
|
||||
import type { BackupService } from '../../src/nest/backup/backup.service';
|
||||
@@ -78,7 +78,7 @@ const liveDb = path.join(__dirname, '../../data', 'travel.db');
|
||||
/** The backup port BackupService injects: the real SQLite one over stubs. */
|
||||
const database = new SqliteDatabaseBackup(
|
||||
{ file: liveDb, close: vi.fn(), reopen: vi.fn() } as unknown as DatabaseLifecycle,
|
||||
maintenanceStub as unknown as MaintenanceRepository,
|
||||
maintenanceStub as unknown as SqliteMaintenance,
|
||||
);
|
||||
|
||||
// createBackup receives StorageService and the database port as parameters
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { openDatabase } from '../../../src/db/connection';
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Places } from '../../../src/db/entities/Places.entity';
|
||||
import { Trips } from '../../../src/db/entities/Trips.entity';
|
||||
import { UnitOfWork } from '../../../src/nest/database/unit-of-work';
|
||||
@@ -147,7 +148,19 @@ async function deleteTripAndWatchTempFiles(db: Database.Database): Promise<{
|
||||
|
||||
const none = undefined as never;
|
||||
try {
|
||||
const trips = new TripsService(none, none, none, none, none, none, none, none, new UnitOfWork(t.em), t.em, none);
|
||||
const trips = new TripsService(
|
||||
none,
|
||||
none,
|
||||
none,
|
||||
none,
|
||||
none,
|
||||
none,
|
||||
none,
|
||||
none,
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
t.em,
|
||||
none,
|
||||
);
|
||||
await trips.remove(trip.id, user.id, 'user');
|
||||
return {
|
||||
tempFiles: [...seen],
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* The schema catalog per engine (src/db/dialect/catalog.ts) and the migration
|
||||
* helpers built on it (src/db/migration-utils.ts), so a migration after the
|
||||
* Postgres baseline's cut can ask what exists on either engine.
|
||||
*/
|
||||
import { columnNamesSql, tableExistsSql } from '../../../../src/db/dialect/catalog';
|
||||
import {
|
||||
addColumnIfMissing,
|
||||
columnNames,
|
||||
execUnlessTableMissing,
|
||||
tableExists,
|
||||
} from '../../../../src/db/migration-utils';
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
import { Platform, TableNotFoundException } from '@mikro-orm/core';
|
||||
import type { Migration } from '@mikro-orm/migrations';
|
||||
import { BasePostgreSqlPlatform, SqlitePlatform } from '@mikro-orm/sql';
|
||||
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const sqlite = new SqlitePlatform();
|
||||
const postgres = new BasePostgreSqlPlatform();
|
||||
|
||||
describe('catalog statements', () => {
|
||||
it('CATALOG-001: SQLite reads its own catalog, Postgres the information schema of the current schema', () => {
|
||||
expect(columnNamesSql(sqlite)).toBe('select name from pragma_table_info(?)');
|
||||
expect(tableExistsSql(sqlite)).toBe("select name from sqlite_master where type = 'table' and name = ?");
|
||||
expect(columnNamesSql(postgres)).toBe(
|
||||
'select column_name as name from information_schema.columns where table_schema = current_schema() and table_name = ?',
|
||||
);
|
||||
expect(tableExistsSql(postgres)).toBe(
|
||||
'select table_name as name from information_schema.tables where table_schema = current_schema() and table_name = ?',
|
||||
);
|
||||
});
|
||||
|
||||
it('CATALOG-002: any other engine is refused', () => {
|
||||
class OtherPlatform extends Platform {}
|
||||
expect(() => columnNamesSql(new OtherPlatform())).toThrow();
|
||||
expect(() => tableExistsSql(new OtherPlatform())).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('migration helpers', () => {
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let migration: Migration;
|
||||
const execute = vi.fn();
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
execute.mockImplementation((sql: string, params?: unknown[]) => t.em.getConnection().execute(sql, params));
|
||||
migration = { execute, getEntityManager: () => t.em } as unknown as Migration;
|
||||
});
|
||||
afterAll(async () => {
|
||||
await t.close();
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
it('CATALOG-010: tableExists and columnNames answer from the live SQLite schema', async () => {
|
||||
expect(await tableExists(migration, 'users')).toBe(true);
|
||||
expect(await tableExists(migration, 'no_such_table')).toBe(false);
|
||||
expect((await columnNames(migration, 'users')).has('email')).toBe(true);
|
||||
expect((await columnNames(migration, 'no_such_table')).size).toBe(0);
|
||||
});
|
||||
|
||||
it('CATALOG-011: addColumnIfMissing adds once, quoting the table the way the engine does', async () => {
|
||||
execute.mockClear();
|
||||
await addColumnIfMissing(migration, 'users', 'catalog_probe', 'catalog_probe TEXT');
|
||||
await addColumnIfMissing(migration, 'users', 'catalog_probe', 'catalog_probe TEXT');
|
||||
const alters = execute.mock.calls.map(([sql]) => String(sql)).filter((sql) => sql.startsWith('alter table'));
|
||||
expect(alters).toEqual(['alter table `users` add column catalog_probe TEXT']);
|
||||
expect((await columnNames(migration, 'users')).has('catalog_probe')).toBe(true);
|
||||
expect(postgres.quoteIdentifier('users')).toBe('"users"');
|
||||
});
|
||||
|
||||
it('CATALOG-012: execUnlessTableMissing tolerates a missing table on either engine, and nothing else', async () => {
|
||||
const failing = (error: Error) => ({ execute: vi.fn(async () => Promise.reject(error)) }) as unknown as Migration;
|
||||
await expect(execUnlessTableMissing(failing(new Error('no such table: x')), 'x')).resolves.toBeUndefined();
|
||||
const pgMissing = new TableNotFoundException(
|
||||
Object.assign(new Error('relation "x" does not exist'), { code: '42P01' }),
|
||||
);
|
||||
await expect(execUnlessTableMissing(failing(pgMissing), 'x')).resolves.toBeUndefined();
|
||||
await expect(execUnlessTableMissing(failing(new Error('syntax error')), 'x')).rejects.toThrow('syntax error');
|
||||
});
|
||||
});
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
dateOf,
|
||||
dayDistance,
|
||||
foundAgainState,
|
||||
ilike,
|
||||
lower,
|
||||
lowerParam,
|
||||
lowerTrim,
|
||||
@@ -175,6 +176,9 @@ describe('sql-functions (postgres)', () => {
|
||||
'CAST(trunc(CAST(vr.accommodation_id AS numeric)) AS integer)',
|
||||
);
|
||||
expect(collateNoCase(pg, 'name').sql).toBe('name COLLATE "nocase"');
|
||||
expect(ilike(pg, ['p.name', 'p.address'])).toBe("(p.name ILIKE ? ESCAPE '\\' OR p.address ILIKE ? ESCAPE '\\')");
|
||||
expect(ilike(new SqlitePlatform(), ['p.name'])).toBe("(p.name LIKE ? ESCAPE '\\')");
|
||||
expect(() => ilike(pg, ['p.name; drop'])).toThrow(/not a column reference/);
|
||||
const whileSame = coalesceOverrideWhileSame(pg, null, 'u.tls', 'u.url', 'https://a');
|
||||
expect(whileSame.sql).toBe(
|
||||
'CASE WHEN u.url IS NOT DISTINCT FROM ? THEN COALESCE(?, u.tls) ELSE COALESCE(?, 0) END',
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
import { deriveDb } from '../../../../src/app-config/derive';
|
||||
import { buildOrmConfig, resolveEngine } from '../../../../src/db/engine';
|
||||
import { TrekPostgreSqlDriver } from '../../../../src/db/engine/postgres/postgres-driver';
|
||||
import { sslOptionFor } from '../../../../src/db/engine/postgres/ssl';
|
||||
import { redactUrl } from '../../../../src/db/engine/redact-url';
|
||||
import type { AnyOrm } from '../../../../src/db/orm';
|
||||
import { SharedSqliteDriver } from '../../../../src/db/orm-driver';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
|
||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const PG_URL = 'postgresql://trek:s3cret@db.example:5432/trek';
|
||||
|
||||
describe('resolveEngine', () => {
|
||||
it('ENGINE-001: SQLite when no TREK_DB_URL is set, which is the test environment', () => {
|
||||
const engine = resolveEngine();
|
||||
expect(engine.id).toBe('sqlite');
|
||||
expect(engine.ormOptions()).toEqual({ driver: SharedSqliteDriver, dbName: ':memory:' });
|
||||
expect(engine.describe()).toBe('SQLite file :memory:');
|
||||
});
|
||||
|
||||
it('ENGINE-002: Postgres when TREK_DB_URL names a database, with the pool size and no SSL by default', () => {
|
||||
const engine = resolveEngine(deriveDb({ TREK_DB_URL: PG_URL, TREK_DB_POOL_MAX: '7' }));
|
||||
expect(engine.id).toBe('postgres');
|
||||
expect(engine.ormOptions()).toEqual({
|
||||
driver: TrekPostgreSqlDriver,
|
||||
clientUrl: PG_URL,
|
||||
pool: { min: 0, max: 7 },
|
||||
driverOptions: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('ENGINE-003: describes a Postgres database without its password', () => {
|
||||
const engine = resolveEngine(deriveDb({ TREK_DB_URL: `${PG_URL}?sslmode=require&password=other` }));
|
||||
expect(engine.describe()).toBe('Postgres postgresql://trek:***@db.example:5432/trek');
|
||||
expect(engine.describe()).not.toContain('s3cret');
|
||||
expect(engine.describe()).not.toContain('other');
|
||||
});
|
||||
});
|
||||
|
||||
describe('sslOptionFor', () => {
|
||||
it('ENGINE-010: maps sslmode onto the pg pool option (MikroORM drops the URL query), always verifying the certificate', () => {
|
||||
// As the pg driver itself reads them: require and verify-ca are verify-full.
|
||||
expect(sslOptionFor(PG_URL)).toBeUndefined();
|
||||
expect(sslOptionFor(`${PG_URL}?sslmode=disable`)).toBe(false);
|
||||
for (const mode of ['require', 'verify-ca', 'verify-full']) {
|
||||
expect(sslOptionFor(`${PG_URL}?sslmode=${mode}`), mode).toEqual({ rejectUnauthorized: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('ENGINE-011: refuses an sslmode it does not know or that would skip verification', () => {
|
||||
expect(() => sslOptionFor(`${PG_URL}?sslmode=requrie`)).toThrow(/sslmode "requrie"/);
|
||||
expect(() => sslOptionFor(`${PG_URL}?sslmode=no-verify`)).toThrow(/sslmode "no-verify"/);
|
||||
expect(() => sslOptionFor(`${PG_URL}?sslmode=prefer`)).toThrow(/sslmode "prefer"/);
|
||||
});
|
||||
|
||||
it('ENGINE-012: passes the SSL option to the pool when the URL asks for one', () => {
|
||||
const engine = resolveEngine(deriveDb({ TREK_DB_URL: `${PG_URL}?sslmode=require` }));
|
||||
expect(engine.ormOptions().driverOptions).toEqual({ ssl: { rejectUnauthorized: true } });
|
||||
});
|
||||
|
||||
it('ENGINE-013: trusts the CA in sslrootcert (a managed host that signs its own certificates), and refuses a missing file', () => {
|
||||
const dir = mkdtempSync(path.join(tmpdir(), 'trek-ca-'));
|
||||
try {
|
||||
const ca = path.join(dir, 'root.crt');
|
||||
writeFileSync(ca, '-----BEGIN CERTIFICATE-----\nprobe\n-----END CERTIFICATE-----\n');
|
||||
expect(sslOptionFor(`${PG_URL}?sslmode=verify-full&sslrootcert=${encodeURIComponent(ca)}`)).toEqual({
|
||||
rejectUnauthorized: true,
|
||||
ca: readFileSync(ca, 'utf8'),
|
||||
});
|
||||
expect(() =>
|
||||
sslOptionFor(`${PG_URL}?sslmode=require&sslrootcert=${encodeURIComponent(path.join(dir, 'no.crt'))}`),
|
||||
).toThrow(/sslrootcert/);
|
||||
expect(() => sslOptionFor(`${PG_URL}?sslrootcert=${encodeURIComponent(ca)}`)).toThrow(
|
||||
/sslrootcert without sslmode/,
|
||||
);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('redactUrl', () => {
|
||||
it('ENGINE-020: hides the password and drops the query, which can carry one too', () => {
|
||||
expect(redactUrl('postgres://u:p@h/d?password=x&sslmode=require')).toBe('postgres://u:***@h/d');
|
||||
expect(redactUrl('postgres://u@h:5433/d')).toBe('postgres://u@h:5433/d');
|
||||
expect(redactUrl('not a url')).toBe('<unparseable URL>');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildOrmConfig', () => {
|
||||
it('ENGINE-030: the SQLite config carries the entities, migrations and seeders with the engine options', () => {
|
||||
const config = buildOrmConfig(resolveEngine());
|
||||
expect(config.driver).toBe(SharedSqliteDriver);
|
||||
expect(config.dbName).toBe(':memory:');
|
||||
expect(config.migrations).toMatchObject({ pathTs: 'src/db/migrations', snapshot: false });
|
||||
expect(config.seeder).toMatchObject({ defaultSeeder: 'DatabaseSeeder' });
|
||||
expect(Array.isArray(config.entities) && config.entities.length).toBeGreaterThan(100);
|
||||
});
|
||||
|
||||
it('ENGINE-031: the Postgres config carries the same entities and migrations, on the pg driver', () => {
|
||||
const config = buildOrmConfig(resolveEngine(deriveDb({ TREK_DB_URL: PG_URL })));
|
||||
expect(config.driver).toBe(TrekPostgreSqlDriver);
|
||||
expect(config.clientUrl).toBe(PG_URL);
|
||||
expect(config.migrations).toMatchObject({ pathTs: 'src/db/migrations', snapshot: false });
|
||||
expect(config.entities).toBe(buildOrmConfig(resolveEngine()).entities);
|
||||
});
|
||||
|
||||
it('ENGINE-032: a Postgres engine opens and closes nothing of its own; a reopen (after pg_restore) rebinds and migrates', async () => {
|
||||
const engine = resolveEngine(deriveDb({ TREK_DB_URL: PG_URL }));
|
||||
await expect(engine.reopen()).rejects.toThrow(/reopened before it was opened/);
|
||||
const onReopened = vi.fn(async () => undefined);
|
||||
const orm = {} as unknown as AnyOrm;
|
||||
expect(() => engine.open(orm, onReopened)).not.toThrow();
|
||||
expect(() => engine.close()).not.toThrow();
|
||||
await engine.reopen();
|
||||
expect(onReopened).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('ENGINE-033: Postgres serializes write transactions with one advisory lock taken in the transaction, SQLite needs none', async () => {
|
||||
const execute = vi.fn(async () => []);
|
||||
const trx = { trx: true };
|
||||
const em = { getConnection: () => ({ execute }), getTransactionContext: () => trx } as unknown as EntityManager;
|
||||
await resolveEngine(deriveDb({ TREK_DB_URL: PG_URL })).serializeWrites(em);
|
||||
expect(execute).toHaveBeenCalledWith('select pg_advisory_xact_lock(?)', [0x5452454b], 'run', trx);
|
||||
execute.mockClear();
|
||||
await resolveEngine().serializeWrites(em);
|
||||
expect(execute).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* The committed Postgres baseline (server/assets/postgres/) and what keeps it
|
||||
* honest: reading and writing it (src/db/engine/postgres/baseline.ts) and the
|
||||
* generator's --check (scripts/generate-pg-baseline.ts).
|
||||
*/
|
||||
import { checkBaseline, rowStatements, triggerPortProblems } from '../../../../../scripts/generate-pg-baseline';
|
||||
import {
|
||||
baselineMatches,
|
||||
readPostgresBaseline,
|
||||
renderBaseline,
|
||||
writePostgresBaseline,
|
||||
type PostgresBaseline,
|
||||
} from '../../../../../src/db/engine/postgres/baseline';
|
||||
import { POSTGRES_TRIGGERS } from '../../../../../src/db/engine/postgres/postgres-triggers';
|
||||
import { knownMigrationNames } from '../../../../../src/db/known-migrations';
|
||||
|
||||
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
|
||||
const A = 'Migration20200101000000_a';
|
||||
const B = 'Migration20200101000100_b';
|
||||
const C = 'Migration20200101000200_c';
|
||||
const sample: PostgresBaseline = {
|
||||
through: B,
|
||||
migrations: [A, B],
|
||||
statements: ['create table "t" ("note" text default \'x;y\')', 'create index "i" on "t" ("note")'],
|
||||
};
|
||||
|
||||
let dir: string | undefined;
|
||||
afterEach(() => {
|
||||
if (dir) rmSync(dir, { recursive: true, force: true });
|
||||
dir = undefined;
|
||||
});
|
||||
const scratch = () => (dir = mkdtempSync(path.join(tmpdir(), 'trek-pg-baseline-')));
|
||||
|
||||
describe('Postgres baseline files', () => {
|
||||
it('PGBASE-001: what is written reads back the same, comment line and all', () => {
|
||||
const target = scratch();
|
||||
writePostgresBaseline(sample, target);
|
||||
expect(readPostgresBaseline(target)).toEqual(sample);
|
||||
expect(baselineMatches(sample, target)).toBe(true);
|
||||
expect(baselineMatches({ ...sample, statements: [sample.statements[0]] }, target)).toBe(false);
|
||||
expect(renderBaseline(sample).sql.startsWith('-- ')).toBe(true);
|
||||
});
|
||||
|
||||
it('PGBASE-002: a manifest that does not list its own cut, or an empty SQL file, is refused', () => {
|
||||
const target = scratch();
|
||||
writePostgresBaseline(sample, target);
|
||||
writeFileSync(path.join(target, 'baseline.json'), JSON.stringify({ through: C, migrations: [A, B] }));
|
||||
expect(() => readPostgresBaseline(target)).toThrow(/must name "through"/);
|
||||
writePostgresBaseline(sample, target);
|
||||
writeFileSync(path.join(target, 'baseline.sql'), '-- nothing\n');
|
||||
expect(() => readPostgresBaseline(target)).toThrow(/holds no statements/);
|
||||
writeFileSync(path.join(target, 'baseline.sql'), 'create table "q" ("a" text default \'?\');\n');
|
||||
expect(() => readPostgresBaseline(target)).toThrow(/holds a "\?"/);
|
||||
expect(baselineMatches(sample, path.join(target, 'missing'))).toBe(false);
|
||||
});
|
||||
|
||||
it('PGBASE-003: the committed baseline names every migration on disk up to its cut, and only those', () => {
|
||||
const committed = readPostgresBaseline();
|
||||
const known = [...knownMigrationNames()].sort();
|
||||
expect(committed.migrations).toEqual(known.filter((name) => name <= committed.through));
|
||||
expect(committed.statements.some((statement) => /^create table "users"/.test(statement))).toBe(true);
|
||||
expect(committed.statements.join('\n')).not.toMatch(/\binteger\b/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkBaseline', () => {
|
||||
const PORTED = Object.keys(POSTGRES_TRIGGERS);
|
||||
const generated = (through: string, migrations: string[]): PostgresBaseline => ({
|
||||
through,
|
||||
migrations,
|
||||
statements: [],
|
||||
});
|
||||
|
||||
it('PGBASE-010: at the newest migration the baseline must be exactly what the entities generate', () => {
|
||||
expect(checkBaseline(sample, generated(B, [A, B]), true, PORTED)).toEqual([]);
|
||||
expect(checkBaseline(sample, generated(B, [A, B]), false, PORTED)).toEqual([
|
||||
'the entities generate a different schema; run npm run gen:pg-baseline',
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGBASE-011: once a migration lands after the cut, the baseline is frozen and only its manifest is checked', () => {
|
||||
expect(checkBaseline(sample, generated(C, [A, B, C]), false, PORTED)).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGBASE-013: while not frozen, a SQLite trigger without a port is reported; once frozen the triggers are not checked', () => {
|
||||
const withNewTrigger = [...PORTED, 'trg_new_trigger'];
|
||||
expect(checkBaseline(sample, generated(B, [A, B]), true, withNewTrigger)).toEqual([
|
||||
'SQLite triggers without a Postgres port in POSTGRES_TRIGGERS: trg_new_trigger',
|
||||
]);
|
||||
expect(checkBaseline(sample, generated(C, [A, B, C]), false, withNewTrigger)).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGBASE-012: a manifest naming a migration that is gone, or missing one before its cut, is reported', () => {
|
||||
expect(checkBaseline(sample, generated(C, [B, C]), true, PORTED)).toEqual([
|
||||
`the baseline names migrations that do not exist: ${A}`,
|
||||
]);
|
||||
const skipped = { ...sample, migrations: [B] };
|
||||
expect(checkBaseline(skipped, generated(B, [A, B]), true, PORTED)).toEqual([
|
||||
`migrations up to ${B} are missing from it: ${A}`,
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('readPostgresBaseline manifest', () => {
|
||||
it('PGBASE-004: a manifest whose migrations are not all names, or are empty, is refused', () => {
|
||||
const target = scratch();
|
||||
for (const manifest of [
|
||||
{ through: B, migrations: [A, 3] },
|
||||
{ through: B, migrations: [] },
|
||||
{ through: 7, migrations: [B] },
|
||||
]) {
|
||||
writePostgresBaseline(sample, target);
|
||||
writeFileSync(path.join(target, 'baseline.json'), JSON.stringify(manifest));
|
||||
expect(() => readPostgresBaseline(target), JSON.stringify(manifest)).toThrow(/must name "through"/);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('triggerPortProblems', () => {
|
||||
it('PGBASE-020: every SQLite trigger needs a Postgres port, and a port without its trigger is stale', () => {
|
||||
expect(triggerPortProblems(['a', 'b'], ['b', 'a'])).toEqual([]);
|
||||
expect(triggerPortProblems(['a', 'b'], ['a', 'c'])).toEqual([
|
||||
'SQLite triggers without a Postgres port in POSTGRES_TRIGGERS: b',
|
||||
'POSTGRES_TRIGGERS ports triggers the migrations no longer create: c',
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGBASE-021: the committed baseline carries every trigger port after the tables', () => {
|
||||
const statements = readPostgresBaseline().statements;
|
||||
const firstTrigger = statements.findIndex((statement) => statement.startsWith('CREATE FUNCTION'));
|
||||
expect(firstTrigger).toBeGreaterThan(0);
|
||||
expect(statements.slice(firstTrigger).every((statement) => /^CREATE (FUNCTION|TRIGGER) /.test(statement))).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('rowStatements', () => {
|
||||
it("PGBASE-030: a migration's rows go in as quoted literals the column casts, and a numeric id moves its sequence past them", () => {
|
||||
expect(
|
||||
rowStatements('photo_provider_fields', [
|
||||
{ id: 1, provider_id: 'immich', label: "Server's URL", required: 1, hint: null },
|
||||
{ id: 2, provider_id: 'immich', label: 'API key', required: 0, hint: 'x' },
|
||||
]),
|
||||
).toEqual([
|
||||
'insert into "photo_provider_fields" ("id", "provider_id", "label", "required", "hint") values ' +
|
||||
"('1', 'immich', 'Server''s URL', '1', NULL), ('2', 'immich', 'API key', '0', 'x')",
|
||||
"select setval(seq, greatest(max_id, 1)) from (select pg_get_serial_sequence('\"photo_provider_fields\"', 'id') as seq, " +
|
||||
'max("id") as max_id from "photo_provider_fields") ids where seq is not null',
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGBASE-031: a text key has no sequence to move, an empty table writes nothing, and binary data is refused', () => {
|
||||
expect(rowStatements('addons', [{ id: 'mcp', enabled: 0 }])).toEqual([
|
||||
'insert into "addons" ("id", "enabled") values (\'mcp\', \'0\')',
|
||||
]);
|
||||
expect(rowStatements('addons', [])).toEqual([]);
|
||||
expect(() => rowStatements('blobs', [{ data: Buffer.from('x') }])).toThrow(/binary data/);
|
||||
});
|
||||
|
||||
it('PGBASE-032: the committed baseline carries the reference rows the migrations insert', () => {
|
||||
const sql = readPostgresBaseline().statements.join('\n');
|
||||
expect(sql).toContain('insert into "tour_types"');
|
||||
expect(sql).toContain('insert into "app_settings"');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,260 @@
|
||||
/**
|
||||
* The Postgres client tools (src/db/engine/postgres/pg-tools.ts, with
|
||||
* pg-process.ts and restore-safety.ts), run against stand-in pg_dump,
|
||||
* pg_restore and psql scripts (tests/helpers/fake-pg-tools.ts): the arguments
|
||||
* and environment each call hands the binary, the password kept out of the
|
||||
* arguments, the files a dump and a restore leave behind, and the refusal when
|
||||
* a tool is missing or too old. The real tools against a real database are
|
||||
* exercised by the Postgres lane.
|
||||
*/
|
||||
import { pgConnectionEnv, PgToolMissingError, resolvePgTool } from '../../../../../src/db/engine/postgres/pg-process';
|
||||
import { pgDump, pgDumpMigrations, pgRestore } from '../../../../../src/db/engine/postgres/pg-tools';
|
||||
import { hasRestrictFix } from '../../../../../src/db/engine/postgres/restore-safety';
|
||||
import {
|
||||
answersSchema,
|
||||
copiesFileEndingWith,
|
||||
failsWith,
|
||||
flagValues,
|
||||
installFakeTool,
|
||||
listsMigrations,
|
||||
printsWhen,
|
||||
readToolCall,
|
||||
touches,
|
||||
writesFile,
|
||||
writesRestrictedScript,
|
||||
} from '../../../../helpers/fake-pg-tools';
|
||||
|
||||
import fs, { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
||||
import os, { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const URL_WITH_SECRET = 'postgresql://trek:s3cr%40t@db.example:6543/trek_db?sslmode=require';
|
||||
/** The restore's session settings, which psql gets through PGOPTIONS. */
|
||||
const RESTORE_OPTIONS = '-c lock_timeout=60s -c client_min_messages=warning';
|
||||
|
||||
let bin: string;
|
||||
let out: string;
|
||||
|
||||
beforeEach(() => {
|
||||
bin = mkdtempSync(path.join(tmpdir(), 'trek-pg-bin-'));
|
||||
out = mkdtempSync(path.join(tmpdir(), 'trek-pg-out-'));
|
||||
});
|
||||
afterEach(() => {
|
||||
rmSync(bin, { recursive: true, force: true });
|
||||
rmSync(out, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe('pg-tools', () => {
|
||||
it('PGTOOLS-001: finds a tool by absolute path in the configured directory, and nowhere else', () => {
|
||||
installFakeTool(bin, 'pg_dump');
|
||||
writeFileSync(path.join(bin, 'psql'), 'not executable');
|
||||
expect(resolvePgTool('pg_dump', bin)).toBe(path.join(bin, 'pg_dump'));
|
||||
expect(resolvePgTool('pg_restore', bin)).toBeNull();
|
||||
expect(resolvePgTool('psql', bin)).toBeNull();
|
||||
});
|
||||
|
||||
it('PGTOOLS-002: the connection goes in the libpq environment, the password decoded and never an argument', () => {
|
||||
expect(pgConnectionEnv(URL_WITH_SECRET)).toEqual({
|
||||
PGHOST: 'db.example',
|
||||
PGPORT: '6543',
|
||||
PGDATABASE: 'trek_db',
|
||||
PGUSER: 'trek',
|
||||
PGPASSWORD: 's3cr@t',
|
||||
PGCONNECT_TIMEOUT: '10',
|
||||
PGSSLMODE: 'verify-full',
|
||||
PGSSLROOTCERT: 'system',
|
||||
});
|
||||
expect(pgConnectionEnv('postgres://u@[::1]/d')).toEqual({
|
||||
PGHOST: '::1',
|
||||
PGPORT: '5432',
|
||||
PGDATABASE: 'd',
|
||||
PGUSER: 'u',
|
||||
PGCONNECT_TIMEOUT: '10',
|
||||
});
|
||||
});
|
||||
|
||||
it('PGTOOLS-003: SSL is verified as the app pool verifies it, against the URL root certificate when it names one', () => {
|
||||
expect(pgConnectionEnv('postgres://u@h/d?sslmode=disable')).toMatchObject({ PGSSLMODE: 'disable' });
|
||||
expect(pgConnectionEnv('postgres://u@h/d?sslmode=disable').PGSSLROOTCERT).toBeUndefined();
|
||||
expect(pgConnectionEnv('postgres://u@h/d?sslmode=verify-ca&sslrootcert=%2Fca%2Froot.crt')).toMatchObject({
|
||||
PGSSLMODE: 'verify-full',
|
||||
PGSSLROOTCERT: '/ca/root.crt',
|
||||
});
|
||||
});
|
||||
|
||||
it('PGTOOLS-004: pg_dump writes a private custom-format dump beside the target and renames it into place', async () => {
|
||||
installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
|
||||
const log = installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
|
||||
const target = path.join(out, 'x.pgdump');
|
||||
await pgDump(URL_WITH_SECRET, target, bin);
|
||||
|
||||
const call = readToolCall(log);
|
||||
expect(call.args).toHaveLength(5);
|
||||
expect(call.args).toEqual(
|
||||
expect.arrayContaining([
|
||||
'--format=custom',
|
||||
'--no-owner',
|
||||
'--no-privileges',
|
||||
`--file=${target}.partial`,
|
||||
'--schema="public"',
|
||||
]),
|
||||
);
|
||||
expect(call.env.PGPASSWORD).toBe('s3cr@t');
|
||||
expect(call.args.join(' ')).not.toContain('s3cr');
|
||||
expect(readFileSync(target, 'utf8')).toBe('dump');
|
||||
expect(statSync(target).mode & 0o777).toBe(0o600);
|
||||
expect(readdirSync(out)).toEqual(['x.pgdump']);
|
||||
});
|
||||
|
||||
it('PGTOOLS-005: a restore clears the schema and loads the script in one psql transaction, then removes its work files', async () => {
|
||||
const clearCopy = path.join(bin, 'clear.sql');
|
||||
const restoreLog = installFakeTool(bin, 'pg_restore', { snippets: [writesRestrictedScript()] });
|
||||
const psqlLog = installFakeTool(bin, 'psql', {
|
||||
snippets: [answersSchema(), copiesFileEndingWith('clear-schema.sql', clearCopy)],
|
||||
});
|
||||
const source = path.join(out, 'in.pgdump');
|
||||
await pgRestore(URL_WITH_SECRET, source, bin);
|
||||
|
||||
const restore = readToolCall(restoreLog);
|
||||
expect(restore.args).toEqual(expect.arrayContaining(['--no-owner', '--no-privileges', '--schema=public']));
|
||||
const [restoreScript] = flagValues(restore.args, '--file=');
|
||||
expect(restoreScript).toMatch(/\.pg-restore-[^/]+\/restore\.sql$/);
|
||||
expect(restore.args.slice(-2)).toEqual(['--', source]);
|
||||
expect(restore.env).toEqual({});
|
||||
|
||||
const psql = readToolCall(psqlLog);
|
||||
expect(psql.args).toEqual(
|
||||
expect.arrayContaining([
|
||||
'--no-psqlrc',
|
||||
'--quiet',
|
||||
'--single-transaction',
|
||||
'--set=ON_ERROR_STOP=1',
|
||||
`--output=${os.devNull}`,
|
||||
]),
|
||||
);
|
||||
// Order is the point here: the schema is cleared before the script loads.
|
||||
const [clearFile, loadFile] = flagValues(psql.args, '--file=');
|
||||
expect(clearFile).toMatch(/clear-schema\.sql$/);
|
||||
expect(loadFile).toBe(restoreScript);
|
||||
expect(psql.env.PGOPTIONS).toBe(RESTORE_OPTIONS);
|
||||
expect(readFileSync(clearCopy, 'utf8')).toMatch(/DROP %s IF EXISTS %s CASCADE/);
|
||||
expect(readdirSync(out)).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGTOOLS-005b: the restore script is checked line by line, so its COPY data is never read whole', async () => {
|
||||
const source = path.join(out, 'in.pgdump');
|
||||
installFakeTool(bin, 'pg_restore', {
|
||||
snippets: [writesRestrictedScript(), 'seq 1 200000 >> "$(fake_file_target "$@")"'],
|
||||
});
|
||||
installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
|
||||
const readWhole = vi.spyOn(fs, 'readFileSync');
|
||||
try {
|
||||
await pgRestore(URL_WITH_SECRET, source, bin);
|
||||
expect(readWhole.mock.calls.filter(([file]) => String(file).endsWith('restore.sql'))).toEqual([]);
|
||||
} finally {
|
||||
readWhole.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("PGTOOLS-006: reads the dump's recorded migrations from its COPY block, and none from a dump without them", async () => {
|
||||
const log = installFakeTool(bin, 'pg_restore', {
|
||||
snippets: [listsMigrations(['Migration20200101000000_baseline_schema', 'Migration20200101000100_second'])],
|
||||
});
|
||||
await expect(pgDumpMigrations('/data/in.pgdump', bin)).resolves.toEqual([
|
||||
'Migration20200101000000_baseline_schema',
|
||||
'Migration20200101000100_second',
|
||||
]);
|
||||
const call = readToolCall(log);
|
||||
expect(call.args).toEqual(expect.arrayContaining(['--data-only', '--table=mikro_orm_migrations', '--file=-']));
|
||||
expect(call.args.slice(-2)).toEqual(['--', '/data/in.pgdump']);
|
||||
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [printsWhen('--data-only', 'SET statement_timeout = 0;\n')] });
|
||||
await expect(pgDumpMigrations('/data/in.pgdump', bin)).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
id: 'PGTOOLS-007a',
|
||||
stderr: 'pg_dump: error: query failed\nCommand was: SELECT secret_row\nDETAIL: Key (email)=(a@b.c)\n',
|
||||
message: /^pg_dump failed: pg_dump: error: query failed$/,
|
||||
},
|
||||
{ id: 'PGTOOLS-007b', stderr: 'x'.repeat(5000), message: /^pg_dump failed: x{2000}…$/ },
|
||||
])(
|
||||
'$id: a failing tool reports its own message without the lines that quote SQL or rows, and leaves no file',
|
||||
async ({ stderr, message }) => {
|
||||
installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
|
||||
installFakeTool(bin, 'pg_dump', { snippets: [failsWith(stderr)] });
|
||||
const target = path.join(out, 'x.pgdump');
|
||||
await expect(pgDump(URL_WITH_SECRET, target, bin)).rejects.toThrow(message);
|
||||
expect(readdirSync(out)).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it('PGTOOLS-008: a missing tool says how to install it, and a restore that cannot run leaves no work files', async () => {
|
||||
const source = path.join(out, 'in.pgdump');
|
||||
await expect(pgRestore(URL_WITH_SECRET, source, bin)).rejects.toThrow(PgToolMissingError);
|
||||
await expect(pgDump(URL_WITH_SECRET, path.join(out, 'x.pgdump'), bin)).rejects.toThrow(/TREK_DB_PG_BIN_DIR/);
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [writesRestrictedScript()] });
|
||||
await expect(pgRestore(URL_WITH_SECRET, source, bin)).rejects.toThrow(/psql was not found/);
|
||||
expect(existsSync(out) && readdirSync(out)).toEqual([]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
id: 'PGTOOLS-009a',
|
||||
tool: 'pg_restore' as const,
|
||||
version: '16.9',
|
||||
message:
|
||||
/pg_restore is too old to restore safely \(pg_restore \(PostgreSQL\) 16\.9 \(Debian\)\): it must be at least 17\.6, 16\.10, 15\.14 or 14\.19/,
|
||||
},
|
||||
{ id: 'PGTOOLS-009b', tool: 'psql' as const, version: '17.5', message: /psql is too old/ },
|
||||
])(
|
||||
'$id: client tools without the \\restrict fix (CVE-2025-8714) never turn a dump into a script',
|
||||
async ({ tool, version, message }) => {
|
||||
const ran = path.join(bin, 'ran');
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [touches(ran)] });
|
||||
installFakeTool(bin, 'psql', { snippets: [touches(ran)] });
|
||||
installFakeTool(bin, tool, { version, snippets: [touches(ran)] });
|
||||
await expect(pgRestore(URL_WITH_SECRET, path.join(out, 'in.pgdump'), bin)).rejects.toThrow(message);
|
||||
expect(existsSync(ran)).toBe(false);
|
||||
expect(readdirSync(out)).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it('PGTOOLS-010: knows which client versions carry the \\restrict fix', () => {
|
||||
const fixed = ['14.19', '15.14', '16.10', '17.6', '17.11', '18.0', '19.1'];
|
||||
const unfixed = ['13.30', '14.18', '15.13', '16.9', '17.5'];
|
||||
for (const v of fixed) expect(hasRestrictFix(`pg_restore (PostgreSQL) ${v}`), v).toBe(true);
|
||||
for (const v of unfixed) expect(hasRestrictFix(`pg_restore (PostgreSQL) ${v}`), v).toBe(false);
|
||||
expect(hasRestrictFix('not a version')).toBe(false);
|
||||
});
|
||||
|
||||
it('PGTOOLS-011: a dump holds the current schema only, matched by its exact name, which psql reads without the password in an argument', async () => {
|
||||
const psqlLog = installFakeTool(bin, 'psql', { snippets: [answersSchema('My"Trek', '"My""Trek"')] });
|
||||
const dumpLog = installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
|
||||
await pgDump(URL_WITH_SECRET, path.join(out, 'x.pgdump'), bin);
|
||||
expect(readToolCall(dumpLog).args).toContain('--schema="My""Trek"');
|
||||
const psql = readToolCall(psqlLog);
|
||||
expect(psql.args.join(' ')).toContain('current_schema()');
|
||||
expect(psql.args.join(' ')).not.toContain('s3cr');
|
||||
|
||||
installFakeTool(bin, 'psql', { snippets: [printsWhen('quote_ident', '\n')] });
|
||||
await expect(pgDump(URL_WITH_SECRET, path.join(out, 'y.pgdump'), bin)).rejects.toThrow(/no current schema/);
|
||||
expect(readdirSync(out)).toEqual(['x.pgdump']);
|
||||
});
|
||||
|
||||
it('PGTOOLS-012: a restore keeps to the current schema, and refuses a dump without TREK tables there before clearing anything', async () => {
|
||||
const source = path.join(out, 'in.pgdump');
|
||||
const restoreLog = installFakeTool(bin, 'pg_restore', {
|
||||
snippets: [writesFile('\\restrict k1\nCREATE TABLE auth.users (\n);\n')],
|
||||
});
|
||||
const psqlLog = installFakeTool(bin, 'psql', { snippets: [answersSchema('trek')] });
|
||||
await expect(pgRestore(URL_WITH_SECRET, source, bin)).rejects.toThrow(
|
||||
'The backup holds no TREK tables in the schema "trek"; nothing was replaced.',
|
||||
);
|
||||
expect(readToolCall(restoreLog).args).toContain('--schema=trek');
|
||||
expect(readToolCall(psqlLog).args.join(' ')).not.toContain('--single-transaction');
|
||||
expect(readdirSync(out)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* The Postgres driver reads `bigint` as a number, on its own pool only
|
||||
* (src/db/engine/postgres/postgres-driver.ts). Nothing here connects.
|
||||
*/
|
||||
import { parseInt8, parseNumeric, TrekPostgreSqlDriver } from '../../../../../src/db/engine/postgres/postgres-driver';
|
||||
import { Configuration } from '@mikro-orm/core';
|
||||
import type { PostgreSqlConnection } from '@mikro-orm/postgresql';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
type Parsers = { getTypeParser(oid: number): (text: string) => unknown };
|
||||
|
||||
describe('postgres driver', () => {
|
||||
it('PGDRIVER-001: a bigint is a number up to 2^53 and refused past it, never rounded', () => {
|
||||
expect(parseInt8('0')).toBe(0);
|
||||
expect(parseInt8('1793636850006')).toBe(1793636850006);
|
||||
expect(parseInt8('-42')).toBe(-42);
|
||||
expect(() => parseInt8('9007199254740993')).toThrow(/beyond what TREK reads exactly/);
|
||||
});
|
||||
|
||||
it('PGDRIVER-003: a numeric (sum of a bigint, avg) is a number like SQLite answers, an integral one past 2^53 refused', () => {
|
||||
expect(parseNumeric('4200')).toBe(4200);
|
||||
expect(parseNumeric('2.5000000000000000')).toBe(2.5);
|
||||
expect(parseNumeric('-7')).toBe(-7);
|
||||
expect(() => parseNumeric('9007199254740993')).toThrow(/beyond what TREK reads exactly/);
|
||||
expect(() => parseNumeric('NaN')).toThrow(/not a finite number/);
|
||||
expect(() => parseNumeric('Infinity')).toThrow(/not a finite number/);
|
||||
});
|
||||
|
||||
it("PGDRIVER-002: the pool parses int8 as a number and keeps MikroORM's text dates", () => {
|
||||
const config = new Configuration(
|
||||
{
|
||||
driver: TrekPostgreSqlDriver,
|
||||
clientUrl: 'postgresql://u@localhost/trek',
|
||||
entities: [],
|
||||
discovery: { warnWhenNoEntities: false },
|
||||
},
|
||||
false,
|
||||
);
|
||||
const driver = new TrekPostgreSqlDriver(config);
|
||||
const options = (driver.getConnection() as PostgreSqlConnection).mapOptions({});
|
||||
const types = options.types as unknown as Parsers;
|
||||
expect(types.getTypeParser(20)('12')).toBe(12);
|
||||
expect(types.getTypeParser(1700)('12.5')).toBe(12.5);
|
||||
expect(types.getTypeParser(1114)('2026-01-01 00:00:00')).toBe('2026-01-01 00:00:00');
|
||||
});
|
||||
});
|
||||
+42
-10
@@ -1,21 +1,22 @@
|
||||
/**
|
||||
* How the Postgres probe turns the entities into a Postgres schema
|
||||
* (scripts/pg-probe/schema.ts): SQLite-only defaults, TEXT timestamps, the
|
||||
* How the entities' schema is written in Postgres
|
||||
* (src/db/engine/postgres/schema-adjust.ts): SQLite-only defaults, TEXT timestamps, the
|
||||
* NOCASE collation, and splitting the generated DDL so one refused statement
|
||||
* does not take the rest with it.
|
||||
*/
|
||||
import { POSTGRES_TRIGGERS } from '../../../../../src/db/engine/postgres/postgres-triggers';
|
||||
import {
|
||||
adjustColumns,
|
||||
applyPostgresColumnAdjustments,
|
||||
type ColumnMeta,
|
||||
NOCASE_COLLATION_SQL,
|
||||
PG_NOW_TEXT_DEFAULT,
|
||||
portableDefault,
|
||||
splitStatements,
|
||||
type ColumnMeta,
|
||||
} from '../../../../scripts/pg-probe/schema';
|
||||
} from '../../../../../src/db/engine/postgres/schema-adjust';
|
||||
import { splitStatements } from '../../../../../src/db/engine/postgres/sql-split';
|
||||
import { DbTimestampType } from '../../../../../src/db/types';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
class DbTimestampType {}
|
||||
class TextType {}
|
||||
|
||||
describe('pg-probe schema', () => {
|
||||
@@ -39,19 +40,30 @@ describe('pg-probe schema', () => {
|
||||
{ name: 'title', customType: new TextType(), columnTypes: ['text'], defaultRaw: "'x'" },
|
||||
{ name: 'stamp', columnTypes: ['int'], defaultRaw: "(strftime('%s','now'))" },
|
||||
{ name: 'region', columnTypes: ['text'], collation: 'NOCASE' },
|
||||
{ name: 'id', columnTypes: ['int'] },
|
||||
{ name: 'body', columnTypes: ['text'] },
|
||||
];
|
||||
expect(adjustColumns(columns)).toEqual(['created_at', 'stamp', 'region']);
|
||||
expect(applyPostgresColumnAdjustments(columns)).toEqual(['created_at', 'stamp', 'region']);
|
||||
expect(columns[0]).toMatchObject({ columnTypes: ['text'], defaultRaw: PG_NOW_TEXT_DEFAULT });
|
||||
expect(columns[1]).toMatchObject({ columnTypes: ['text'], defaultRaw: "'x'" });
|
||||
expect(columns[2]).toMatchObject({
|
||||
columnTypes: ['int'],
|
||||
columnTypes: ['bigint'],
|
||||
defaultRaw: 'CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)',
|
||||
});
|
||||
expect(columns[3]).toMatchObject({ collation: 'nocase' });
|
||||
expect(NOCASE_COLLATION_SQL).toContain('CREATE COLLATION IF NOT EXISTS nocase');
|
||||
});
|
||||
|
||||
it('PGPROBE-043: an integer column is bigint, as wide as SQLite INTEGER (epoch milliseconds overflow integer)', () => {
|
||||
const columns: ColumnMeta[] = [
|
||||
{ name: 'id', columnTypes: ['integer'] },
|
||||
{ name: 'next_run_at', columnTypes: ['int'] },
|
||||
{ name: 'enabled', columnTypes: ['boolean'] },
|
||||
{ name: 'note', columnTypes: ['text'] },
|
||||
];
|
||||
expect(applyPostgresColumnAdjustments(columns)).toEqual(['id', 'next_run_at']);
|
||||
expect(columns.map((column) => column.columnTypes)).toEqual([['bigint'], ['bigint'], ['boolean'], ['text']]);
|
||||
});
|
||||
|
||||
it('PGPROBE-042: splits DDL on semicolons outside quotes and drops session settings', () => {
|
||||
const ddl = [
|
||||
"set names 'utf8';",
|
||||
@@ -67,4 +79,24 @@ describe('pg-probe schema', () => {
|
||||
]);
|
||||
expect(splitStatements(' ; ; ')).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGPROBE-045: refuses a tagged dollar quote, which it does not understand, rather than splitting inside it', () => {
|
||||
expect(() => splitStatements('create function f() as $f$ select 1; $f$;')).toThrow(
|
||||
/only the \$\$ dollar quote, not \$f\$/,
|
||||
);
|
||||
expect(splitStatements("select '$f$'; select 2")).toEqual(["select '$f$'", 'select 2']);
|
||||
});
|
||||
|
||||
it('PGPROBE-044: a $$ body stays one statement, semicolons and all, and every trigger port splits into its two', () => {
|
||||
const fn =
|
||||
"CREATE FUNCTION f() RETURNS trigger LANGUAGE plpgsql AS $$\nBEGIN\n UPDATE t SET x = ';';\n RETURN NEW;\nEND\n$$";
|
||||
expect(splitStatements(`${fn};\nCREATE TRIGGER g AFTER UPDATE ON t FOR EACH ROW EXECUTE FUNCTION f();`)).toEqual([
|
||||
fn,
|
||||
'CREATE TRIGGER g AFTER UPDATE ON t FOR EACH ROW EXECUTE FUNCTION f()',
|
||||
]);
|
||||
for (const [name, statements] of Object.entries(POSTGRES_TRIGGERS)) {
|
||||
expect(splitStatements(statements.join(';\n')), name).toEqual([...statements]);
|
||||
expect(statements.at(-1), name).toContain(`CREATE TRIGGER ${name} `);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,267 @@
|
||||
/**
|
||||
* How a Postgres database reaches the schema (src/db/engine/postgres/schema-bootstrap.ts):
|
||||
* the three states the plan names (empty, foreign tables, TREK's own) and the
|
||||
* safety net before pending migrations, over a fake migrator and connection.
|
||||
*/
|
||||
import type { AppEnv } from '../../../../../src/app-config';
|
||||
import {
|
||||
bootstrapPostgresSchema,
|
||||
postgresSafetyNet,
|
||||
type PostgresBootstrapDeps,
|
||||
} from '../../../../../src/db/engine/postgres/schema-bootstrap';
|
||||
import type { MigrationSafetyNet } from '../../../../../src/db/legacy-baseline';
|
||||
import type { AnyOrm } from '../../../../../src/db/orm';
|
||||
import { answersSchema, failsWith, installFakeTool, writesFile } from '../../../../helpers/fake-pg-tools';
|
||||
import { Logger } from '@nestjs/common';
|
||||
|
||||
import { mkdtempSync, readdirSync, rmSync, utimesSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const M1 = 'Migration20200101000000_baseline_schema';
|
||||
const M2 = 'Migration20200101000100_second';
|
||||
const M3 = 'Migration20200101000200_after_the_cut';
|
||||
|
||||
interface FakeDb {
|
||||
executed: string[];
|
||||
pending: string[];
|
||||
tables: string[];
|
||||
/** Whether mikro_orm_migrations exists: from the start when anything is recorded. */
|
||||
history: boolean;
|
||||
ensureTable: ReturnType<typeof vi.fn>;
|
||||
sql: string[];
|
||||
logged: string[];
|
||||
inTransaction: boolean[];
|
||||
up: ReturnType<typeof vi.fn>;
|
||||
}
|
||||
|
||||
function fakeOrm(state: Pick<FakeDb, 'executed' | 'pending' | 'tables'> & { history?: boolean }): {
|
||||
orm: AnyOrm;
|
||||
db: FakeDb;
|
||||
} {
|
||||
const db: FakeDb = {
|
||||
...state,
|
||||
history: state.history ?? state.executed.length > 0,
|
||||
ensureTable: vi.fn(async () => {
|
||||
db.history = true;
|
||||
}),
|
||||
sql: [],
|
||||
logged: [],
|
||||
inTransaction: [],
|
||||
up: vi.fn(async () => undefined),
|
||||
};
|
||||
const trx = { trx: true };
|
||||
const connection = {
|
||||
execute: vi.fn(async (sql: string, _params?: unknown[], _method?: string, ctx?: unknown) => {
|
||||
if (sql.includes('information_schema.tables')) {
|
||||
return [...db.tables, ...(db.history ? ['mikro_orm_migrations'] : [])].map((table_name) => ({ table_name }));
|
||||
}
|
||||
db.sql.push(sql);
|
||||
db.inTransaction.push(ctx === trx);
|
||||
return [];
|
||||
}),
|
||||
transactional: vi.fn(async (work: (ctx: unknown) => Promise<void>) => work(trx)),
|
||||
};
|
||||
const storage = {
|
||||
ensureTable: db.ensureTable,
|
||||
logMigration: vi.fn(async ({ name }: { name: string }, ctx?: unknown) => {
|
||||
db.logged.push(name);
|
||||
db.inTransaction.push(ctx === trx);
|
||||
db.executed = [...db.executed, name];
|
||||
db.pending = db.pending.filter((pending) => pending !== name);
|
||||
}),
|
||||
};
|
||||
const migrator = {
|
||||
getStorage: () => storage,
|
||||
getExecuted: vi.fn(async () => db.executed.map((name) => ({ name }))),
|
||||
getPending: vi.fn(async () => db.pending.map((name) => ({ name }))),
|
||||
up: db.up,
|
||||
};
|
||||
const orm = {
|
||||
config: { getExtension: () => migrator, get: () => ({}) },
|
||||
em: { getConnection: () => connection },
|
||||
} as unknown as AnyOrm;
|
||||
return { orm, db };
|
||||
}
|
||||
|
||||
function deps(
|
||||
safetyNet: MigrationSafetyNet = { beforeMigrate: vi.fn(async () => undefined), restoreHint: async () => null },
|
||||
) {
|
||||
const value: PostgresBootstrapDeps = {
|
||||
baseline: () => ({
|
||||
through: M2,
|
||||
migrations: [M1, M2],
|
||||
statements: ['create table "a" ("id" bigserial)', 'create index "x" on "a" ("id")'],
|
||||
}),
|
||||
known: new Set([M1, M2, M3]),
|
||||
safetyNet,
|
||||
};
|
||||
return value;
|
||||
}
|
||||
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
describe('bootstrapPostgresSchema', () => {
|
||||
beforeEach(() => {
|
||||
vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
|
||||
});
|
||||
|
||||
it('PGBOOT-001: an empty database gets the baseline and its migrations recorded, all in one transaction', async () => {
|
||||
const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2], tables: [] });
|
||||
const net = deps();
|
||||
await bootstrapPostgresSchema(orm, net);
|
||||
expect(db.sql).toEqual(['create table "a" ("id" bigserial);\ncreate index "x" on "a" ("id")']);
|
||||
expect(db.logged).toEqual([M1, M2]);
|
||||
expect(db.inTransaction.every(Boolean)).toBe(true);
|
||||
expect(db.up).not.toHaveBeenCalled();
|
||||
expect(net.safetyNet.beforeMigrate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('PGBOOT-002: a fresh database runs the migrations after the cut without the safety net (nothing to lose)', async () => {
|
||||
const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2, M3], tables: [] });
|
||||
const net = deps();
|
||||
await bootstrapPostgresSchema(orm, net);
|
||||
expect(db.logged).toEqual([M1, M2]);
|
||||
expect(db.up).toHaveBeenCalledTimes(1);
|
||||
expect(net.safetyNet.beforeMigrate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('PGBOOT-003: tables without TREK history are refused, named, and nothing is written', async () => {
|
||||
const { orm, db } = fakeOrm({ executed: [], pending: [M1], tables: ['customers', 'orders'] });
|
||||
await expect(bootstrapPostgresSchema(orm, deps())).rejects.toThrow(
|
||||
/Refusing to boot: the database holds 2 table\(s\) \(customers, orders\) but no TREK migration history/,
|
||||
);
|
||||
expect(db.sql).toEqual([]);
|
||||
expect(db.logged).toEqual([]);
|
||||
expect(db.ensureTable).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('PGBOOT-008: an empty history table beside no other table is a fresh database (a first boot cut short)', async () => {
|
||||
const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2], tables: [], history: true });
|
||||
await bootstrapPostgresSchema(orm, deps());
|
||||
expect(db.logged).toEqual([M1, M2]);
|
||||
});
|
||||
|
||||
it('PGBOOT-004: a database a newer TREK migrated is refused before anything runs', async () => {
|
||||
const { orm, db } = fakeOrm({ executed: [M1, 'Migration20990101000000_from_the_future'], pending: [], tables: [] });
|
||||
await expect(bootstrapPostgresSchema(orm, deps())).rejects.toThrow(/migrated by a newer TREK/);
|
||||
expect(db.up).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('PGBOOT-005: pending migrations on a database in use run after the safety net, labelled by the last one', async () => {
|
||||
const { orm, db } = fakeOrm({ executed: [M2, M1], pending: [M3], tables: ['users'] });
|
||||
const net = deps();
|
||||
await bootstrapPostgresSchema(orm, net);
|
||||
expect(net.safetyNet.beforeMigrate).toHaveBeenCalledWith('20200101000100');
|
||||
expect(db.up).toHaveBeenCalledTimes(1);
|
||||
expect(db.sql).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGBOOT-006: a refusing safety net stops the migration', async () => {
|
||||
const { orm, db } = fakeOrm({ executed: [M1, M2], pending: [M3], tables: ['users'] });
|
||||
const refusing = {
|
||||
beforeMigrate: vi.fn(async () => Promise.reject(new Error('no copy'))),
|
||||
restoreHint: async () => null,
|
||||
};
|
||||
await expect(bootstrapPostgresSchema(orm, deps(refusing))).rejects.toThrow('no copy');
|
||||
expect(db.up).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('PGBOOT-007: a database at head does nothing', async () => {
|
||||
const { orm, db } = fakeOrm({ executed: [M1, M2, M3], pending: [], tables: ['users'] });
|
||||
const net = deps();
|
||||
await bootstrapPostgresSchema(orm, net);
|
||||
expect(db.sql).toEqual([]);
|
||||
expect(db.up).not.toHaveBeenCalled();
|
||||
expect(net.safetyNet.beforeMigrate).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('postgresSafetyNet', () => {
|
||||
const URL = 'postgresql://trek:pw@db/trek';
|
||||
const NOW = new Date('2026-11-02T13:05:09Z');
|
||||
let dir: string;
|
||||
let bin: string;
|
||||
|
||||
beforeEach(() => {
|
||||
dir = mkdtempSync(path.join(tmpdir(), 'trek-pg-net-'));
|
||||
bin = mkdtempSync(path.join(tmpdir(), 'trek-pg-net-bin-'));
|
||||
vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
|
||||
});
|
||||
afterEach(() => {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
rmSync(bin, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
/**
|
||||
* A stand-in pg_dump that writes its --file, or fails like one that cannot
|
||||
* connect, beside the psql that names the schema the dump is limited to.
|
||||
*/
|
||||
const fakePgDump = (ok: boolean) => {
|
||||
installFakeTool(bin, 'pg_dump', { snippets: [ok ? writesFile('dump') : failsWith('could not connect')] });
|
||||
installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
|
||||
};
|
||||
const envWith =
|
||||
(preMigrateSnapshot: boolean | undefined, isTest: boolean, keep = 3) =>
|
||||
() =>
|
||||
({
|
||||
db: { preMigrateSnapshot, preMigrateSnapshotKeep: keep, pgBinDir: bin },
|
||||
app: { isTest },
|
||||
}) as unknown as AppEnv;
|
||||
const net = (env: () => AppEnv) =>
|
||||
postgresSafetyNet(
|
||||
URL,
|
||||
env,
|
||||
() => NOW,
|
||||
() => dir,
|
||||
);
|
||||
|
||||
it.each([undefined, true])(
|
||||
'PGBOOT-010: dumps the database before migrating it (setting %s), keeping the newest copies and no half-written one',
|
||||
async (setting) => {
|
||||
fakePgDump(true);
|
||||
for (const old of ['20200101000001', '20200101000002']) {
|
||||
writeFileSync(path.join(dir, `pre-migrate-${old}-20250101T000000Z.pgdump`), 'old');
|
||||
utimesSync(path.join(dir, `pre-migrate-${old}-20250101T000000Z.pgdump`), 1, 1);
|
||||
}
|
||||
writeFileSync(path.join(dir, 'pre-migrate-20200101000003-20250101T000000Z.pgdump.partial'), 'killed');
|
||||
await net(envWith(setting, false, 2)).beforeMigrate('20200101000100');
|
||||
const kept = readdirSync(dir).sort();
|
||||
expect(kept).toHaveLength(2);
|
||||
expect(kept).toContain('pre-migrate-20200101000100-20261102T130509Z.pgdump');
|
||||
expect(kept.some((file) => file.endsWith('.partial'))).toBe(false);
|
||||
},
|
||||
);
|
||||
|
||||
it('PGBOOT-011: migrates without a copy when the operator turned it off, and says so', async () => {
|
||||
const warn = vi.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);
|
||||
await expect(net(envWith(false, false)).beforeMigrate('1')).resolves.toBeUndefined();
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('without a copy'));
|
||||
expect(readdirSync(dir)).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGBOOT-012: stays out of the way under test unless a suite asks for it', async () => {
|
||||
await expect(net(envWith(undefined, true)).beforeMigrate('1')).resolves.toBeUndefined();
|
||||
expect(readdirSync(dir)).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGBOOT-013: a copy that cannot be written refuses the boot, names the switch, and leaves no partial file', async () => {
|
||||
fakePgDump(false);
|
||||
await expect(net(envWith(true, false)).beforeMigrate('1')).rejects.toThrow(
|
||||
/Refusing to boot: could not write a copy.*pg_dump failed: could not connect.*TREK_DB_PRE_MIGRATE_SNAPSHOT=false/s,
|
||||
);
|
||||
expect(readdirSync(dir)).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGBOOT-014: the newer-database refusal points at the copy taken from this release, or at the others', async () => {
|
||||
const hint = net(envWith(true, false));
|
||||
await expect(hint.restoreHint('20200101000100')).resolves.toBeNull();
|
||||
writeFileSync(path.join(dir, 'pre-migrate-20200101000050-20250101T000000Z.pgdump'), 'old');
|
||||
await expect(hint.restoreHint('20200101000100')).resolves.toMatch(/none of them from this release/);
|
||||
writeFileSync(path.join(dir, 'pre-migrate-20200101000100-20250102T000000Z.pgdump'), 'mine');
|
||||
await expect(hint.restoreHint('20200101000100')).resolves.toMatch(
|
||||
/pre-migrate-20200101000100-20250102T000000Z\.pgdump: stop TREK, empty the database .*pg_restore --no-owner/,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,109 @@
|
||||
import { SqliteMaintenance } from '../../../../../src/db/engine/sqlite/sqlite-maintenance';
|
||||
import { createSnapshotTestDb } from '../../../../helpers/db-mock';
|
||||
import { createTestOrm, type TestOrm } from '../../../../helpers/test-orm';
|
||||
|
||||
import Database from 'better-sqlite3';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let maintenance: SqliteMaintenance;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
maintenance = new SqliteMaintenance(t.em);
|
||||
});
|
||||
afterAll(async () => {
|
||||
await t.close();
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
/**
|
||||
* The first SQL text handed to the connection after the spy is set (the ORM
|
||||
* may read its own metadata on the same connection afterwards).
|
||||
*/
|
||||
function spyOnExecute(): { first: () => string; restore: () => void } {
|
||||
const connection = t.em.getConnection();
|
||||
const spy = vi.spyOn(connection, 'execute');
|
||||
return {
|
||||
first: () => String(spy.mock.calls[0]?.[0]),
|
||||
restore: () => spy.mockRestore(),
|
||||
};
|
||||
}
|
||||
|
||||
/** Whether the SQLite file at `file` holds a table named `table`. */
|
||||
function holdsTable(file: string, table: string): boolean {
|
||||
const snap = new Database(file, { readonly: true });
|
||||
try {
|
||||
return (snap.pragma(`table_info(${table})`) as unknown[]).length > 0;
|
||||
} finally {
|
||||
snap.close();
|
||||
}
|
||||
}
|
||||
|
||||
describe('SqliteMaintenance', () => {
|
||||
it('SQLMAINT-001: walCheckpoint sends PRAGMA wal_checkpoint(TRUNCATE)', async () => {
|
||||
const execute = spyOnExecute();
|
||||
try {
|
||||
await expect(maintenance.walCheckpoint()).resolves.toBeUndefined();
|
||||
expect(execute.first()).toBe('PRAGMA wal_checkpoint(TRUNCATE)');
|
||||
} finally {
|
||||
execute.restore();
|
||||
}
|
||||
});
|
||||
|
||||
describe('vacuumInto', () => {
|
||||
let scratchDir: string;
|
||||
|
||||
afterEach(() => {
|
||||
if (scratchDir) fs.rmSync(scratchDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("SQLMAINT-002: sends `VACUUM INTO '<path>'` and leaves a readable copy at the path", async () => {
|
||||
scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sqlite-maint-'));
|
||||
const target = path.join(scratchDir, 'snap.db');
|
||||
const execute = spyOnExecute();
|
||||
try {
|
||||
await maintenance.vacuumInto(target);
|
||||
expect(execute.first()).toBe(`VACUUM INTO '${target}'`);
|
||||
} finally {
|
||||
execute.restore();
|
||||
}
|
||||
expect(holdsTable(target, 'users')).toBe(true);
|
||||
});
|
||||
|
||||
it('SQLMAINT-003: doubles single quotes in the path', async () => {
|
||||
scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), "sqlite-maint-o'brien-"));
|
||||
const target = path.join(scratchDir, 'snap.db');
|
||||
const execute = spyOnExecute();
|
||||
try {
|
||||
await maintenance.vacuumInto(target);
|
||||
expect(execute.first()).toBe(`VACUUM INTO '${target.replaceAll("'", "''")}'`);
|
||||
} finally {
|
||||
execute.restore();
|
||||
}
|
||||
expect(holdsTable(target, 'users')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('SQLMAINT-004: both methods throw outside a request context (fail closed, as TrekRepository)', async () => {
|
||||
const { MikroORM } = await import('@mikro-orm/core');
|
||||
const fresh = await MikroORM.init({
|
||||
entities: [],
|
||||
driver: (await import('../../../../../src/db/orm-driver')).createBoundSqliteDriver(() => testDb),
|
||||
dbName: ':memory:',
|
||||
allowGlobalContext: false,
|
||||
discovery: { warnWhenNoEntities: false },
|
||||
});
|
||||
try {
|
||||
const unwrapped = new SqliteMaintenance(fresh.em);
|
||||
await expect(unwrapped.walCheckpoint()).rejects.toThrow();
|
||||
await expect(unwrapped.vacuumInto(path.join(os.tmpdir(), 'never.db'))).rejects.toThrow();
|
||||
} finally {
|
||||
await fresh.close(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -44,7 +44,7 @@ const migrator = () => orm.config.getExtension('@mikro-orm/migrator') as Migrato
|
||||
function envWith(db: Partial<AppEnv['db']>, isTest = false): () => AppEnv {
|
||||
return () => {
|
||||
const env = readEnv();
|
||||
return { ...env, app: { ...env.app, isTest }, db: { ...env.db, ...db } };
|
||||
return { ...env, app: { ...env.app, isTest }, db: { ...env.db, ...db } as AppEnv['db'] };
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
* username)` — unlike `AssignmentsService.getParticipants` (AS15) — see the
|
||||
* repository's own docstring.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AssignmentParticipants } from '../../../../src/db/entities/AssignmentParticipants.entity';
|
||||
import { Users } from '../../../../src/db/entities/Users.entity';
|
||||
import type { AssignmentParticipantsRepository } from '../../../../src/db/repositories/AssignmentParticipants.repository';
|
||||
@@ -27,7 +28,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
participants = t.repo(AssignmentParticipants);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
* assignment-with-place projection contract (built here, Task 3 consumes it
|
||||
* unchanged for AS1/AS3).
|
||||
*/
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { DayAccommodations } from '../../../../src/db/entities/DayAccommodations.entity';
|
||||
import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity';
|
||||
import { Places } from '../../../../src/db/entities/Places.entity';
|
||||
@@ -35,7 +36,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
assignments = t.repo(DayAssignments);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { InviteTokens } from '../../../../src/db/entities/InviteTokens.entity';
|
||||
import type { InviteTokensRepository } from '../../../../src/db/repositories/InviteTokens.repository';
|
||||
import { withRequestContext } from '../../../../src/nest/database/request-context';
|
||||
@@ -227,7 +228,7 @@ describe('InviteTokensRepository', () => {
|
||||
it('INVREPO-017: incrementUsedCount inside uow.transactional is not discarded by a stale entity read earlier under a different projection', async () => {
|
||||
const { user: admin } = createUser(testDb, { username: 'invrepo-017-admin' });
|
||||
const invite = createInviteToken(testDb, { token: 'invrepo-017', max_uses: 5, created_by: admin.id });
|
||||
const uow = new UnitOfWork(t.em);
|
||||
const uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
|
||||
await withRequestContext(t.orm, async () => {
|
||||
// Projection A — wide, full row, carries used_count.
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { MaintenanceRepository } from '../../../../src/db/repositories/MaintenanceRepository';
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
import { TableNotFoundException } from '@mikro-orm/core';
|
||||
|
||||
import Database from 'better-sqlite3';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest';
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
@@ -22,61 +19,18 @@ afterAll(async () => {
|
||||
});
|
||||
|
||||
describe('MaintenanceRepository', () => {
|
||||
it('MAINTREPO-001: walCheckpoint renders PRAGMA wal_checkpoint(TRUNCATE) — pinned text, equivalent to the legacy raw call', async () => {
|
||||
// Rendered-SQL-equivalence: the exact text a direct statement on the
|
||||
// handle executes without throwing, string-for-string what the legacy
|
||||
// `db.exec('PRAGMA wal_checkpoint(TRUNCATE)')` issued.
|
||||
const RENDERED = 'PRAGMA wal_checkpoint(TRUNCATE)';
|
||||
// test-sql-allow: the pragma text itself is under test, run as the raw statement it renders to.
|
||||
expect(() => testDb.prepare(RENDERED).run()).not.toThrow();
|
||||
await expect(maintenance.walCheckpoint()).resolves.toBeUndefined();
|
||||
it('MAINTREPO-001: ping sends SELECT 1 and resolves while the database answers', async () => {
|
||||
const spy = vi.spyOn(t.em.getConnection(), 'execute');
|
||||
try {
|
||||
await expect(maintenance.ping()).resolves.toBeUndefined();
|
||||
// The first statement sent; the ORM may read its own metadata afterwards.
|
||||
expect(spy.mock.calls[0]?.[0]).toBe('SELECT 1');
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
describe('vacuumInto', () => {
|
||||
let scratchPath: string;
|
||||
|
||||
afterEach(() => {
|
||||
if (scratchPath && fs.existsSync(scratchPath)) fs.rmSync(scratchPath, { force: true });
|
||||
});
|
||||
|
||||
it("MAINTREPO-002: vacuumInto renders `VACUUM INTO '<path>'` — pinned text, produces a readable snapshot at the given path", async () => {
|
||||
scratchPath = path.join(os.tmpdir(), `maintrepo-vacuum-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
|
||||
await maintenance.vacuumInto(scratchPath);
|
||||
expect(fs.existsSync(scratchPath)).toBe(true);
|
||||
|
||||
const snap = new Database(scratchPath, { readonly: true });
|
||||
try {
|
||||
// test-sql-allow: a snapshot file no ORM is bound to, read through sqlite_master.
|
||||
const row = snap.prepare('SELECT name FROM sqlite_master WHERE type = ? AND name = ?').get('table', 'users');
|
||||
expect(row).toBeTruthy();
|
||||
} finally {
|
||||
snap.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('MAINTREPO-003: single quotes in the path are escaped by doubling, matching the legacy `dbSnap.replaceAll("\'", "\'\'")` shape', async () => {
|
||||
const dirWithQuote = path.join(os.tmpdir(), `maintrepo-o'brien-${Date.now()}`);
|
||||
fs.mkdirSync(dirWithQuote, { recursive: true });
|
||||
scratchPath = path.join(dirWithQuote, 'snap.db');
|
||||
try {
|
||||
await maintenance.vacuumInto(scratchPath);
|
||||
expect(fs.existsSync(scratchPath)).toBe(true);
|
||||
|
||||
// Rendered-SQL-equivalence: the SAME escaped text, run directly
|
||||
// against another target path, succeeds identically.
|
||||
const directPath = path.join(dirWithQuote, 'snap-direct.db');
|
||||
const escaped = directPath.replaceAll("'", "''");
|
||||
// test-sql-allow: the VACUUM INTO text itself is under test, run as the raw statement it renders to.
|
||||
expect(() => testDb.prepare(`VACUUM INTO '${escaped}'`).run()).not.toThrow();
|
||||
expect(fs.existsSync(directPath)).toBe(true);
|
||||
fs.rmSync(directPath, { force: true });
|
||||
} finally {
|
||||
fs.rmSync(dirWithQuote, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('MAINTREPO-004: both methods throw outside a request context (fail-closed, matching TrekRepository)', async () => {
|
||||
it('MAINTREPO-004: ping throws outside a request context (fail-closed, matching TrekRepository)', async () => {
|
||||
const { MikroORM } = await import('@mikro-orm/core');
|
||||
const fresh = await MikroORM.init({
|
||||
entities: [],
|
||||
@@ -87,9 +41,23 @@ describe('MaintenanceRepository', () => {
|
||||
});
|
||||
try {
|
||||
const unwrapped = new MaintenanceRepository(fresh.em);
|
||||
await expect(unwrapped.walCheckpoint()).rejects.toThrow();
|
||||
await expect(unwrapped.ping()).rejects.toThrow();
|
||||
} finally {
|
||||
await fresh.close(false);
|
||||
}
|
||||
});
|
||||
|
||||
it('MAINTREPO-005: deletePluginUserData skips an absent table and rethrows any other failure', async () => {
|
||||
const spy = vi.spyOn(t.em.getConnection(), 'execute');
|
||||
try {
|
||||
// The first statement is the user_config delete, so the one-shot rejection lands on it.
|
||||
spy.mockRejectedValueOnce(new TableNotFoundException(new Error('no such table: plugin_user_config')));
|
||||
await expect(maintenance.deletePluginUserData(1)).resolves.toBeUndefined();
|
||||
|
||||
spy.mockRejectedValueOnce(new Error('connection lost'));
|
||||
await expect(maintenance.deletePluginUserData(1)).rejects.toThrow('connection lost');
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { McpTokens } from '../../../../src/db/entities/McpTokens.entity';
|
||||
import type { McpTokensRepository } from '../../../../src/db/repositories/McpTokens.repository';
|
||||
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
|
||||
@@ -449,7 +450,7 @@ describe('McpTokensRepository', () => {
|
||||
// read, nativeUpdate, flush, still correct — is still worth pinning.
|
||||
const { user } = createUser(testDb);
|
||||
const created = createMcpToken(testDb, user.id, { name: 'stays-original' });
|
||||
const uow = new UnitOfWork(t.em);
|
||||
const uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
|
||||
await uow.transactional(async () => {
|
||||
const basic = await tokens.findBasic(created.id);
|
||||
@@ -516,7 +517,7 @@ describe('McpTokensRepository', () => {
|
||||
it('MCPTOKREPO-023: projection A then projection B then a nativeUpdate on an A-only column, inside uow.transactional — the nativeUpdate survives the closing flush', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const created = createMcpToken(testDb, user.id, { name: 'stays-original', scope_mode: 'all' });
|
||||
const uow = new UnitOfWork(t.em);
|
||||
const uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
|
||||
await uow.transactional(async () => {
|
||||
// Projection A: selects `scope_mode` among its columns.
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { OauthClients } from '../../../../src/db/entities/OauthClients.entity';
|
||||
import { OauthTokens } from '../../../../src/db/entities/OauthTokens.entity';
|
||||
import type { OauthClientsRepository } from '../../../../src/db/repositories/OauthClients.repository';
|
||||
@@ -20,7 +21,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
clients = t.repo(OauthClients);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { OauthClients } from '../../../../src/db/entities/OauthClients.entity';
|
||||
import { OauthConsents } from '../../../../src/db/entities/OauthConsents.entity';
|
||||
import type { OauthConsentsRepository } from '../../../../src/db/repositories/OauthConsents.repository';
|
||||
@@ -19,7 +20,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
consents = t.repo(OauthConsents);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { OauthClients } from '../../../../src/db/entities/OauthClients.entity';
|
||||
import { OauthConsents } from '../../../../src/db/entities/OauthConsents.entity';
|
||||
import { OauthTokens } from '../../../../src/db/entities/OauthTokens.entity';
|
||||
@@ -21,7 +22,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
tokens = t.repo(OauthTokens);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { PasswordResetTokens } from '../../../../src/db/entities/PasswordResetTokens.entity';
|
||||
import type { PasswordResetTokensRepository } from '../../../../src/db/repositories/PasswordResetTokens.repository';
|
||||
import { dbNow } from '../../../../src/db/types';
|
||||
@@ -209,7 +210,7 @@ describe('PasswordResetTokensRepository', () => {
|
||||
it('PWDRESETREPO-011: findByTokenHash then markConsumed inside uow.transactional survives the closing flush (cannot fail by construction — see PWDRESETREPO-012 for the mutation-proved guard)', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const id = await seedToken(user.id, hash('survives'));
|
||||
const uow = new UnitOfWork(t.em);
|
||||
const uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
|
||||
await uow.transactional(async () => {
|
||||
const found = await tokens.findByTokenHash(hash('survives'));
|
||||
@@ -223,7 +224,7 @@ describe('PasswordResetTokensRepository', () => {
|
||||
it('PWDRESETREPO-012: findByTokenHash is the FIRST, wider read (it carries consumed_at); a narrower managed read follows; markConsumed still lands', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const id = await seedToken(user.id, hash('dshape'));
|
||||
const uow = new UnitOfWork(t.em);
|
||||
const uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
|
||||
await uow.transactional(async () => {
|
||||
await tokens.findByTokenHash(hash('dshape')); // projection A — WIDE (carries consumed_at)
|
||||
@@ -241,7 +242,7 @@ describe('PasswordResetTokensRepository', () => {
|
||||
it('PWDRESETREPO-013: same D-shape guard for consumeAllLiveForUser', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const id = await seedToken(user.id, hash('dshape-consume-all'));
|
||||
const uow = new UnitOfWork(t.em);
|
||||
const uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
|
||||
await uow.transactional(async () => {
|
||||
await tokens.findByTokenHash(hash('dshape-consume-all')); // projection A — WIDE (carries consumed_at)
|
||||
|
||||
@@ -640,7 +640,7 @@ describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
|
||||
const inCat = createPlace(testDb, trip.id, { category_id: cat.id });
|
||||
const outCat = createPlace(testDb, trip.id);
|
||||
await deleteRows(t.orm, Categories, { id: { $ne: cat.id } });
|
||||
const rows = await places.listForTrip(String(trip.id), { category: String(cat.id) });
|
||||
const rows = await places.listForTrip(String(trip.id), { category: cat.id });
|
||||
expect(rows.map((r) => r.id)).toEqual([inCat.id]);
|
||||
void outCat;
|
||||
});
|
||||
@@ -652,7 +652,7 @@ describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
|
||||
const tagged = createPlace(testDb, trip.id, { name: 'Tagged' });
|
||||
const untagged = createPlace(testDb, trip.id, { name: 'Untagged' });
|
||||
await tagPlace(t.orm, tagged.id, [tag.id]);
|
||||
const rows = await places.listForTrip(String(trip.id), { tag: String(tag.id) });
|
||||
const rows = await places.listForTrip(String(trip.id), { tag: tag.id });
|
||||
expect(rows.map((r) => r.id)).toEqual([tagged.id]);
|
||||
void untagged;
|
||||
});
|
||||
@@ -684,7 +684,7 @@ describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
|
||||
await tagPlace(t.orm, other.id, [tag.id]);
|
||||
// `other` is left unassigned, so `assignment: 'assigned'` alone should exclude it.
|
||||
const rows = await places.listForTrip(String(trip.id), {
|
||||
searchPattern: '%Central Park%', category: String(cat.id), tag: String(tag.id), assignment: 'assigned',
|
||||
searchPattern: '%Central Park%', category: cat.id, tag: tag.id, assignment: 'assigned',
|
||||
});
|
||||
expect(rows.map((r) => r.id)).toEqual([winner.id]);
|
||||
});
|
||||
@@ -703,7 +703,7 @@ describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
|
||||
describe('PlacesRepository.listForTrip (PL3) — toEqual(legacy) over all 32 filter combinations', () => {
|
||||
function legacyListForTrip(
|
||||
tripId: string,
|
||||
filters: { searchPattern?: string; category?: string; tag?: string; assignment?: 'all' | 'unassigned' | 'assigned' },
|
||||
filters: { searchPattern?: string; category?: number | string; tag?: number | string; assignment?: 'all' | 'unassigned' | 'assigned' },
|
||||
): unknown[] {
|
||||
let query = `
|
||||
SELECT DISTINCT p.*, t.place_id AS tour_place_id, c.name as category_name, c.color as category_color, c.icon as category_icon,
|
||||
@@ -767,8 +767,8 @@ describe('PlacesRepository.listForTrip (PL3) — toEqual(legacy) over all 32 fil
|
||||
createPlace(testDb, otherTrip.id, { name: 'Central Perk' });
|
||||
|
||||
const searchOptions: (string | undefined)[] = [undefined, '%Central%'];
|
||||
const categoryOptions: (string | undefined)[] = [undefined, String(cat.id)];
|
||||
const tagOptions: (string | undefined)[] = [undefined, String(tag.id)];
|
||||
const categoryOptions: (number | undefined)[] = [undefined, cat.id];
|
||||
const tagOptions: (number | undefined)[] = [undefined, tag.id];
|
||||
const assignmentOptions: (('all' | 'unassigned' | 'assigned') | undefined)[] = [undefined, 'all', 'assigned', 'unassigned'];
|
||||
|
||||
let combinations = 0;
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
* ReservationDayPositionsRepository — parity test (Plan 3d Task 7 whole-plan
|
||||
* review, item 9/"missing parity tests": this repository had no standalone
|
||||
* test file). ONE seeded world, one `toEqual(<legacy raw>)` test for the
|
||||
* read method (RS19); RS31 (`upsertScoped`, a write) already has coverage
|
||||
* through `reservations.service.test.ts`'s `updatePositions` cases.
|
||||
* read method (RS19); RS31 (`upsertScoped`, a write) has coverage through
|
||||
* `reservations.service.test.ts`'s `updatePositions` cases and, for the
|
||||
* overwrite and the trip scoping its ON CONFLICT clause holds, below.
|
||||
*/
|
||||
import { ReservationDayPositions } from '../../../../src/db/entities/ReservationDayPositions.entity';
|
||||
import type { ReservationDayPositionsRepository } from '../../../../src/db/repositories/ReservationDayPositions.repository';
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { createDay, createReservation, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { insertRow } from '../../../helpers/factories/rows';
|
||||
import { findRows, insertRow } from '../../../helpers/factories/rows';
|
||||
import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
|
||||
@@ -86,3 +87,30 @@ describe('ReservationDayPositionsRepository — fully seeded world', () => {
|
||||
expect(await repo.listForReservation(999999)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ReservationDayPositionsRepository.upsertScoped', () => {
|
||||
it('RDP-UPSERT-001: a second write to the same reservation and day overwrites its position', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
const reservation = createReservation(testDb, trip.id, { day_id: day.id });
|
||||
|
||||
await repo.upsertScoped(trip.id, reservation.id, day.id, 1.5);
|
||||
await repo.upsertScoped(trip.id, reservation.id, day.id, 4);
|
||||
|
||||
const rows = await findRows(t.orm, ReservationDayPositions, { reservation_id: reservation.id });
|
||||
expect(rows.map((r) => [r.day_id, r.position])).toEqual([[day.id, 4]]);
|
||||
});
|
||||
|
||||
it('RDP-UPSERT-002: a reservation of another trip writes no row', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const otherTrip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
const foreign = createReservation(testDb, otherTrip.id);
|
||||
|
||||
await repo.upsertScoped(trip.id, foreign.id, day.id, 2);
|
||||
|
||||
expect(await findRows(t.orm, ReservationDayPositions, { reservation_id: foreign.id })).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Tags } from '../../../../src/db/entities/Tags.entity';
|
||||
import type { TagsRepository } from '../../../../src/db/repositories/Tags.repository';
|
||||
import { withRequestContext } from '../../../../src/nest/database/request-context';
|
||||
@@ -19,7 +20,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
tags = t.repo(Tags);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* days hold (MDT-02), the trip copy read and write
|
||||
* (TO6/TO8), and the create/edit writes (TO7/TO9).
|
||||
*/
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity';
|
||||
import { Places } from '../../../../src/db/entities/Places.entity';
|
||||
import { TourPackingLinks } from '../../../../src/db/entities/TourPackingLinks.entity';
|
||||
@@ -332,7 +333,9 @@ describe('ToursRepository stages on days (MDT-02)', () => {
|
||||
createTour(testDb, other.id);
|
||||
const otherStage = createDayAssignment(testDb, days[2].id, other.id, { tour_stage_key: 'stage-two' });
|
||||
|
||||
const gone = await new UnitOfWork(t.em).transactional(() => repo.deleteTourStagesNotIn(place.id, ['stage-one']));
|
||||
const gone = await new UnitOfWork(t.em, new SqliteEngine()).transactional(() =>
|
||||
repo.deleteTourStagesNotIn(place.id, ['stage-one']),
|
||||
);
|
||||
expect(gone).toEqual([{ id: second.id, day_id: days[1].id }]);
|
||||
const left = (await findRows(t, DayAssignments, {}, { id: 'asc' })).map((row) => row.id);
|
||||
expect(left).toEqual([first.id, whole.id, otherStage.id]);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Trips } from '../../../../src/db/entities/Trips.entity';
|
||||
import { Users } from '../../../../src/db/entities/Users.entity';
|
||||
import type { TripsRepository } from '../../../../src/db/repositories/Trips.repository';
|
||||
@@ -20,7 +21,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
trips = t.repo(Trips);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { WebauthnCredentials } from '../../../../src/db/entities/WebauthnCredentials.entity';
|
||||
import type { WebauthnCredentialsRepository } from '../../../../src/db/repositories/WebauthnCredentials.repository';
|
||||
import { withRequestContext } from '../../../../src/nest/database/request-context';
|
||||
@@ -317,7 +318,7 @@ describe('WebauthnCredentialsRepository', () => {
|
||||
it('WEBAUTHN-CRED-REPO-017: a nativeUpdate inside uow.transactional is not discarded by a stale entity read earlier under a different projection', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const cred = await insertCredential(user.id, { counter: 1 });
|
||||
const uow = new UnitOfWork(t.em);
|
||||
const uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
|
||||
await withRequestContext(t.orm, async () => {
|
||||
// Projection A — wide, full row, carries `counter` (PK9's shape: findByCredentialId).
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
* no `disableIdentityMap`) makes IMWB-001 fail with the stale write-back this
|
||||
* test is built to catch.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Users } from '../../../../src/db/entities/Users.entity';
|
||||
import type { UsersRepository } from '../../../../src/db/repositories/Users.repository';
|
||||
import { withRequestContext } from '../../../../src/nest/database/request-context';
|
||||
@@ -50,7 +51,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
users = t.repo(Users);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
* backwards until this task; `OAUTHTOKREPO-029` (`OauthTokens.repository
|
||||
* .test.ts`) already had it right and is the shape to copy.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Users } from '../../../../src/db/entities/Users.entity';
|
||||
import { WebauthnChallenges } from '../../../../src/db/entities/WebauthnChallenges.entity';
|
||||
import type { UsersRepository } from '../../../../src/db/repositories/Users.repository';
|
||||
@@ -66,7 +67,7 @@ beforeAll(async () => {
|
||||
t = await createTestOrm(testDb, { allowGlobalContext: false });
|
||||
users = t.repo(Users);
|
||||
challenges = t.repo(WebauthnChallenges);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
// `t.clear()` is deliberately NOT called here (unlike most repository test
|
||||
// files): `EntityManager.clear()` itself calls the VALIDATING `getContext()`
|
||||
|
||||
@@ -71,10 +71,10 @@ describe('a restored database is migrated forward', () => {
|
||||
|
||||
// What reinitialize() does after the file has been swapped in.
|
||||
const restored = await ormFor(dbFile);
|
||||
await runSchemaBootstrap(restored);
|
||||
await runSchemaBootstrap(restored, { snapshot: true });
|
||||
// Twice: a restore can follow a boot that already bootstrapped, and every
|
||||
// seeder is written to tolerate that.
|
||||
await runSchemaBootstrap(restored);
|
||||
await runSchemaBootstrap(restored, { snapshot: true });
|
||||
await restored.close(true);
|
||||
|
||||
const after = new Database(dbFile);
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
* docstring: NESTED propagation) rather than the independent, connection-mutex-
|
||||
* queued transaction two unrelated concurrent requests actually are.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Trips } from '../../../src/db/entities/Trips.entity';
|
||||
import type { TripsRepository } from '../../../src/db/repositories/Trips.repository';
|
||||
import { withRequestContext } from '../../../src/nest/database/request-context';
|
||||
@@ -57,7 +58,7 @@ let uow: UnitOfWork;
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
trips = t.repo(Trips);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* cases assert is which file each step names.
|
||||
*
|
||||
* The port is the real `SqliteDatabaseBackup` over a lifecycle stand-in (the
|
||||
* file, close, reopen) and a MaintenanceRepository stand-in (checkpoint,
|
||||
* file, close, reopen) and a SqliteMaintenance stand-in (checkpoint,
|
||||
* VACUUM INTO). The file system is spied rather than written to, because the
|
||||
* baseline path is fixed at data/travel-baseline.db and a developer running a
|
||||
* demo instance has a real one sitting there.
|
||||
@@ -21,7 +21,7 @@
|
||||
* `withRequestContext` over a real (empty) test database: the reads find
|
||||
* nothing and the restore-writes are skipped.
|
||||
*/
|
||||
import type { MaintenanceRepository } from '../../../src/db/repositories/MaintenanceRepository';
|
||||
import type { SqliteMaintenance } from '../../../src/db/engine/sqlite/sqlite-maintenance';
|
||||
import { resetDemoUser, saveBaseline } from '../../../src/demo/demo-reset';
|
||||
import { SqliteDatabaseBackup } from '../../../src/nest/backup/sqlite-database-backup';
|
||||
import { DatabaseConnectionLostError } from '../../../src/nest/database/database-backup.interface';
|
||||
@@ -65,7 +65,7 @@ describe('demo-reset DB path', () => {
|
||||
maintenance = { walCheckpoint: vi.fn(async () => {}), vacuumInto: vi.fn(async () => {}) };
|
||||
database = new SqliteDatabaseBackup(
|
||||
lifecycle as unknown as DatabaseLifecycle,
|
||||
maintenance as unknown as MaintenanceRepository,
|
||||
maintenance as unknown as SqliteMaintenance,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -140,7 +140,7 @@ declare const repo: { run(sql: string, params?: unknown[]): Promise<unknown>; ge
|
||||
// text slipping in under the exemption, a lower one means the pin (and at 0
|
||||
// the ignores entry) has to follow.
|
||||
const EXEMPT_SQL_STRINGS: Record<string, number> = {
|
||||
'src/db/repositories/MaintenanceRepository.ts': 4,
|
||||
'src/db/repositories/MaintenanceRepository.ts': 2,
|
||||
'src/db/seeders/AdminSeeder.ts': 2,
|
||||
'src/db/seeders/CategorySeeder.ts': 2,
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* the legacy router, because rpc-host.test.ts asserts them and shipped plugins read
|
||||
* them, so these tests pin the exact strings rather than just the refusal.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
|
||||
import { Users } from '../../../src/db/entities/Users.entity';
|
||||
import type { TripsRepository } from '../../../src/db/repositories/Trips.repository';
|
||||
@@ -205,7 +206,7 @@ describe('PluginGuards — canEditAs/canCreateAs against a real UsersRepository
|
||||
beforeEach(async () => {
|
||||
testDb = createSnapshotTestDb();
|
||||
orm = await createTestOrm(testDb);
|
||||
const permissions = new PermissionsService(orm.repo(AppSettings), new UnitOfWork(orm.em));
|
||||
const permissions = new PermissionsService(orm.repo(AppSettings), new UnitOfWork(orm.em, new SqliteEngine()));
|
||||
const db = {
|
||||
findAccessible: vi.fn(async (tripId: number) => (tripId === 1 ? { id: 1, user_id: ownerId } : undefined)),
|
||||
} as unknown as TripsRepository;
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
* filter itself (the disabled row is excluded) alongside the boolean coercion
|
||||
* of the enabled row that remains.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Addons } from '../../../src/db/entities/Addons.entity';
|
||||
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
|
||||
import { PhotoProviderFields } from '../../../src/db/entities/PhotoProviderFields.entity';
|
||||
@@ -195,7 +196,7 @@ beforeEach(() => {
|
||||
photoProviderFieldsRepo,
|
||||
appSettingsRepo,
|
||||
usersRepo,
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* Unit tests for backupService.
|
||||
* Covers BACKUP-031 to BACKUP-060.
|
||||
*/
|
||||
import type { MaintenanceRepository } from '../../../src/db/repositories/MaintenanceRepository';
|
||||
import type { SqliteMaintenance } from '../../../src/db/engine/sqlite/sqlite-maintenance';
|
||||
import {
|
||||
formatSize,
|
||||
parseIntField,
|
||||
@@ -78,9 +78,9 @@ const dbMock = vi.hoisted(() => ({
|
||||
}));
|
||||
|
||||
// The database port's SQLite implementation is built for real below with this
|
||||
// stand-in for the MaintenanceRepository Nest injects into it: the WAL
|
||||
// stand-in for the SqliteMaintenance Nest injects into it: the WAL
|
||||
// checkpoint and the VACUUM INTO snapshot are what a backup and a restore ask
|
||||
// of it. The real statements are MaintenanceRepository.test.ts's coverage.
|
||||
// of it. The real statements are sqlite-maintenance.test.ts's coverage.
|
||||
const maintenanceRepoMock = vi.hoisted(() => ({
|
||||
walCheckpoint: vi.fn().mockResolvedValue(undefined),
|
||||
vacuumInto: vi.fn().mockResolvedValue(undefined),
|
||||
@@ -121,7 +121,7 @@ const lifecycleStub = {
|
||||
function deps(storage: StorageService) {
|
||||
return {
|
||||
storage,
|
||||
database: new SqliteDatabaseBackup(lifecycleStub, maintenanceRepoMock as unknown as MaintenanceRepository),
|
||||
database: new SqliteDatabaseBackup(lifecycleStub, maintenanceRepoMock as unknown as SqliteMaintenance),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* The restore step the two backup ports share (`database-swap.ts`): the swap,
|
||||
* then a reopen that always runs, and how the two failures surface.
|
||||
*/
|
||||
import { keepRestoreCopy, replaceThenReopen } from '../../../../src/nest/backup/database-swap';
|
||||
import { DatabaseConnectionLostError } from '../../../../src/nest/database/database-backup.interface';
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const ok = async () => undefined;
|
||||
const fails = (error: unknown) => async () => {
|
||||
throw error;
|
||||
};
|
||||
|
||||
describe('replaceThenReopen', () => {
|
||||
it('SWAP-001: a swap that lands and a reopen that works resolves with no reopen error', async () => {
|
||||
const swap = vi.fn(ok);
|
||||
const reopen = vi.fn(ok);
|
||||
|
||||
await expect(replaceThenReopen(swap, reopen)).resolves.toEqual({ reopenError: null });
|
||||
expect(swap).toHaveBeenCalledTimes(1);
|
||||
expect(reopen).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('SWAP-002: a swap that lands and a reopen that fails resolves with the reopen error, so the caller says restart', async () => {
|
||||
const lost = new Error('reconnect failed');
|
||||
|
||||
await expect(replaceThenReopen(ok, fails(lost))).resolves.toEqual({ reopenError: lost });
|
||||
});
|
||||
|
||||
it('SWAP-003: a swap that fails still reopens, and its own error is rethrown', async () => {
|
||||
const reopen = vi.fn(ok);
|
||||
const swapError = new Error('rename failed');
|
||||
|
||||
await expect(replaceThenReopen(fails(swapError), reopen)).rejects.toBe(swapError);
|
||||
expect(reopen).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('SWAP-004: a swap and a reopen that both fail go up together, as DatabaseConnectionLostError', async () => {
|
||||
const swapError = new Error('rename failed');
|
||||
const reopenError = new Error('reconnect failed');
|
||||
|
||||
const thrown = await replaceThenReopen(fails(swapError), fails(reopenError)).catch((err: unknown) => err);
|
||||
|
||||
expect(thrown).toBeInstanceOf(DatabaseConnectionLostError);
|
||||
expect((thrown as DatabaseConnectionLostError).swapError).toBe(swapError);
|
||||
expect((thrown as DatabaseConnectionLostError).cause).toBe(reopenError);
|
||||
});
|
||||
|
||||
it('SWAP-005: a step that throws undefined still counts as failed', async () => {
|
||||
// A reopen that throws undefined is a failure: with a swap that also failed, the two go up as the lost connection.
|
||||
await expect(replaceThenReopen(fails(new Error('rename failed')), fails(undefined))).rejects.toBeInstanceOf(
|
||||
DatabaseConnectionLostError,
|
||||
);
|
||||
// A swap that throws undefined rethrows undefined, the same value the step threw.
|
||||
await expect(replaceThenReopen(fails(undefined), ok)).rejects.toBeUndefined();
|
||||
});
|
||||
|
||||
it('SWAP-006: a synchronous swap that throws is caught like an async one', async () => {
|
||||
const swapError = new Error('copy failed');
|
||||
const reopen = vi.fn(ok);
|
||||
|
||||
await expect(
|
||||
replaceThenReopen(() => {
|
||||
throw swapError;
|
||||
}, reopen),
|
||||
).rejects.toBe(swapError);
|
||||
expect(reopen).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('keepRestoreCopy', () => {
|
||||
it('SWAP-007: resolves with the target once the copy is written', async () => {
|
||||
await expect(keepRestoreCopy('/data/pre-restore-1.db', ok)).resolves.toBe('/data/pre-restore-1.db');
|
||||
});
|
||||
|
||||
it('SWAP-008: a copy that cannot be written resolves with null, so the restore goes on', async () => {
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
try {
|
||||
await expect(keepRestoreCopy('/data/pre-restore-2.db', fails(new Error('disk full')))).resolves.toBeNull();
|
||||
} finally {
|
||||
warn.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -6,7 +6,16 @@
|
||||
* calls this file makes. The plugin staging is the one seam stubbed, because
|
||||
* it moves trees under the plugin roots of the machine running the tests.
|
||||
*/
|
||||
import { knownMigrationNames } from '../../../src/db/known-migrations';
|
||||
import { BootRestoreError, restoreOnFirstBoot } from '../../../src/nest/backup/boot-restore';
|
||||
import {
|
||||
answersSchema,
|
||||
answersTables,
|
||||
installFakeTool,
|
||||
listsMigrations,
|
||||
touches,
|
||||
writesRestrictedScript,
|
||||
} from '../../helpers/fake-pg-tools';
|
||||
|
||||
import archiver from 'archiver';
|
||||
import Database from 'better-sqlite3';
|
||||
@@ -125,6 +134,62 @@ describe('restoreOnFirstBoot', () => {
|
||||
expect(fs.existsSync(dbFile)).toBe(false);
|
||||
});
|
||||
|
||||
describe('on Postgres', () => {
|
||||
/**
|
||||
* Stand-in client tools (tests/helpers/fake-pg-tools.ts): psql lists
|
||||
* `tables` and marks `loaded` when it runs a restore; pg_restore lists a
|
||||
* known migration as the dump's history, or writes a restricted script.
|
||||
* Their arguments are pinned in pg-tools.test.ts.
|
||||
*/
|
||||
const postgresPlan = (archivePath: string, tables: string[] = []) => {
|
||||
const bin = path.join(root, 'bin');
|
||||
fs.mkdirSync(bin, { recursive: true });
|
||||
installFakeTool(bin, 'psql', {
|
||||
snippets: [answersSchema(), answersTables(tables), touches(path.join(root, 'loaded'))],
|
||||
});
|
||||
const migration = [...knownMigrationNames()][0];
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations([migration]), writesRestrictedScript()] });
|
||||
return {
|
||||
archive: archivePath,
|
||||
dbFile,
|
||||
dataDir,
|
||||
reclaimResetFile,
|
||||
postgres: { url: 'postgres://trek:pw@db/trek', binDir: bin },
|
||||
};
|
||||
};
|
||||
const loaded = () => fs.existsSync(path.join(root, 'loaded'));
|
||||
|
||||
it('BOOT-RESTORE-004b: a database holding tables is left alone, so a restart never rolls back', async () => {
|
||||
const plan = postgresPlan(await archive({ 'travel.pgdump': 'dump' }), ['users', 'trips']);
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
expect(await restoreOnFirstBoot(plan)).toEqual({ restored: false, reason: 'database-exists' });
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('the Postgres database (2 table(s))'));
|
||||
expect(loaded()).toBe(false);
|
||||
});
|
||||
|
||||
it("BOOT-RESTORE-004c: an empty database is loaded from the archive's dump, with its key and uploads", async () => {
|
||||
const plan = postgresPlan(
|
||||
await archive({ 'travel.pgdump': 'dump', '.encryption_key': 'b'.repeat(64), 'uploads/a.jpg': 'jpeg' }),
|
||||
);
|
||||
const out = await restoreOnFirstBoot(plan);
|
||||
expect(out.restored).toBe(true);
|
||||
expect(loaded()).toBe(true);
|
||||
expect(fs.readFileSync(path.join(dataDir, '.encryption_key'), 'utf8')).toBe('b'.repeat(64));
|
||||
expect(fs.existsSync(reclaimResetFile)).toBe(true);
|
||||
expect(fs.existsSync(dbFile)).toBe(false);
|
||||
});
|
||||
|
||||
it('BOOT-RESTORE-004d: a SQLite backup is refused before anything is loaded, leaving no scratch', async () => {
|
||||
const plan = postgresPlan(await goodArchive());
|
||||
await expect(restoreOnFirstBoot(plan)).rejects.toThrow(
|
||||
'RESTORE_FROM_BACKUP: This backup holds a SQLite database, which cannot be restored into a Postgres database.',
|
||||
);
|
||||
expect(loaded()).toBe(false);
|
||||
expect(fs.existsSync(reclaimResetFile)).toBe(false);
|
||||
expect(fs.readdirSync(dataDir).filter((f) => f.startsWith('restore-boot-'))).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
it('BOOT-RESTORE-005: an archive without a database is refused and leaves no scratch behind', async () => {
|
||||
const zipPath = await archive({ 'notes.txt': 'hello' });
|
||||
await expect(restoreOnFirstBoot({ archive: zipPath, dbFile, dataDir, reclaimResetFile })).rejects.toThrow(
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* DatabaseEngineModule: one engine per container, shared by every injector.
|
||||
*/
|
||||
import {
|
||||
DatabaseEngineModule,
|
||||
DATABASE_ENGINE,
|
||||
DATABASE_SETTINGS,
|
||||
} from '../../../../src/nest/database/database-engine.module';
|
||||
import { Test } from '@nestjs/testing';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe('DatabaseEngineModule', () => {
|
||||
it('ENGINE-DI-001: provides one engine instance, so state an engine keeps is the same wherever it is injected', async () => {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [DatabaseEngineModule] }).compile();
|
||||
|
||||
const engine = moduleRef.get(DATABASE_ENGINE);
|
||||
expect(moduleRef.get(DATABASE_ENGINE)).toBe(engine);
|
||||
expect(engine.id).toBe('sqlite');
|
||||
});
|
||||
|
||||
it('ENGINE-DI-002: the settings are read from the environment once and the engine is resolved from them', async () => {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [DatabaseEngineModule] }).compile();
|
||||
|
||||
const settings = moduleRef.get(DATABASE_SETTINGS);
|
||||
expect(settings.engine).toBe('sqlite');
|
||||
expect(moduleRef.get(DATABASE_SETTINGS)).toBe(settings);
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,8 @@
|
||||
* baseline depends on) and that the ORM is rebuilt around a reopened handle.
|
||||
* The real open, migrate and reopen run in tests/integration/database-lifecycle.test.ts.
|
||||
*/
|
||||
import { PostgresEngine } from '../../../../src/db/engine/postgres/postgres-engine';
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { DatabaseLifecycle } from '../../../../src/nest/database/database-lifecycle.service';
|
||||
import type { MikroORM } from '@mikro-orm/core';
|
||||
|
||||
@@ -15,6 +17,9 @@ import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
/** The settings a Postgres process would read from TREK_DB_URL, given directly rather than through the environment. */
|
||||
const POSTGRES = { engine: 'postgres' as const, url: 'postgres://trek:pw@db/trek', poolMax: 10 };
|
||||
|
||||
const { dbMock, ormMock, calls } = vi.hoisted(() => {
|
||||
const calls: string[] = [];
|
||||
return {
|
||||
@@ -66,15 +71,15 @@ describe('DatabaseLifecycle', () => {
|
||||
it('DBLIFE-001: open() opens the connection, binds the ORM to later swaps, then brings the schema to head', async () => {
|
||||
const { orm } = ormStub();
|
||||
|
||||
await new DatabaseLifecycle(orm).open();
|
||||
await new DatabaseLifecycle(orm, new SqliteEngine()).open();
|
||||
|
||||
expect(calls).toEqual(['getRawConnection', 'registerReinitializeHook', 'runSchemaBootstrap:{}']);
|
||||
expect(ormMock.runSchemaBootstrap).toHaveBeenCalledWith(orm);
|
||||
expect(calls).toEqual(['getRawConnection', 'registerReinitializeHook', 'runSchemaBootstrap:{"snapshot":true}']);
|
||||
expect(ormMock.runSchemaBootstrap).toHaveBeenCalledWith(orm, { snapshot: true });
|
||||
});
|
||||
|
||||
it('DBLIFE-002: the hook open() registers rebuilds the ORM client and migrates the new file without a pre-migrate copy', async () => {
|
||||
const { orm, connection } = ormStub();
|
||||
await new DatabaseLifecycle(orm).open();
|
||||
await new DatabaseLifecycle(orm, new SqliteEngine()).open();
|
||||
const hook = dbMock.registerReinitializeHook.mock.calls[0][0];
|
||||
calls.length = 0;
|
||||
|
||||
@@ -86,7 +91,7 @@ describe('DatabaseLifecycle', () => {
|
||||
|
||||
it('DBLIFE-007: listeners registered with onReopened run after the schema bootstrap of a reopen, in order', async () => {
|
||||
const { orm } = ormStub();
|
||||
const lifecycle = new DatabaseLifecycle(orm);
|
||||
const lifecycle = new DatabaseLifecycle(orm, new SqliteEngine());
|
||||
lifecycle.onReopened(async () => {
|
||||
calls.push('listener:first');
|
||||
});
|
||||
@@ -110,7 +115,7 @@ describe('DatabaseLifecycle', () => {
|
||||
|
||||
it('DBLIFE-008: a listener that throws fails the reopen, so a listener must contain its own failures', async () => {
|
||||
const { orm } = ormStub();
|
||||
const lifecycle = new DatabaseLifecycle(orm);
|
||||
const lifecycle = new DatabaseLifecycle(orm, new SqliteEngine());
|
||||
lifecycle.onReopened(async () => {
|
||||
throw new Error('listener broke');
|
||||
});
|
||||
@@ -120,8 +125,33 @@ describe('DatabaseLifecycle', () => {
|
||||
await expect(hook()).rejects.toThrow('listener broke');
|
||||
});
|
||||
|
||||
it('DBLIFE-009: on Postgres a reopen keeps the pool and only migrates, so it never waits on a connection lent to a job', async () => {
|
||||
const bootstrap = vi
|
||||
.spyOn(PostgresEngine.prototype, 'schemaBootstrap')
|
||||
.mockImplementation(async (_orm, options) => {
|
||||
calls.push(`schemaBootstrap:${JSON.stringify(options)}`);
|
||||
});
|
||||
try {
|
||||
const { orm, connection } = ormStub();
|
||||
const lifecycle = new DatabaseLifecycle(orm, new PostgresEngine(POSTGRES));
|
||||
lifecycle.onReopened(async () => {
|
||||
calls.push('listener');
|
||||
});
|
||||
await lifecycle.open();
|
||||
calls.length = 0;
|
||||
|
||||
await lifecycle.reopen();
|
||||
|
||||
expect(connection.close).not.toHaveBeenCalled();
|
||||
expect(connection.connect).not.toHaveBeenCalled();
|
||||
expect(calls).toEqual(['schemaBootstrap:{"snapshot":false}', 'listener']);
|
||||
} finally {
|
||||
bootstrap.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('DBLIFE-003: close() and reopen() go through the connection module', async () => {
|
||||
const lifecycle = new DatabaseLifecycle(ormStub().orm);
|
||||
const lifecycle = new DatabaseLifecycle(ormStub().orm, new SqliteEngine());
|
||||
|
||||
lifecycle.close();
|
||||
await lifecycle.reopen();
|
||||
@@ -132,7 +162,9 @@ describe('DatabaseLifecycle', () => {
|
||||
it('DBLIFE-004: a reopen that fails is not swallowed', async () => {
|
||||
dbMock.reinitialize.mockRejectedValueOnce(new Error('database is locked'));
|
||||
|
||||
await expect(new DatabaseLifecycle(ormStub().orm).reopen()).rejects.toThrow('database is locked');
|
||||
await expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).reopen()).rejects.toThrow(
|
||||
'database is locked',
|
||||
);
|
||||
});
|
||||
|
||||
describe('file', () => {
|
||||
@@ -145,7 +177,7 @@ describe('DatabaseLifecycle', () => {
|
||||
});
|
||||
|
||||
it('DBLIFE-005: is the in-memory database under test', () => {
|
||||
expect(new DatabaseLifecycle(ormStub().orm).file).toBe(':memory:');
|
||||
expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).file).toBe(':memory:');
|
||||
});
|
||||
|
||||
it('DBLIFE-006: is TREK_DB_FILE when it is set, the one path the connection, the backup and the restore share', () => {
|
||||
@@ -154,7 +186,7 @@ describe('DatabaseLifecycle', () => {
|
||||
vi.stubEnv('NODE_ENV', 'production');
|
||||
vi.stubEnv('TREK_DB_FILE', file);
|
||||
|
||||
expect(new DatabaseLifecycle(ormStub().orm).file).toBe(file);
|
||||
expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).file).toBe(file);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Users } from '../../../../src/db/entities/Users.entity';
|
||||
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
@@ -29,7 +30,7 @@ let uow: UnitOfWork;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
});
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { Days } from '../../../../src/db/entities/Days.entity';
|
||||
import type { DaysRepository } from '../../../../src/db/repositories/Days.repository';
|
||||
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
|
||||
@@ -25,7 +26,7 @@ let days: DaysRepository;
|
||||
*/
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
uow = new UnitOfWork(t.em);
|
||||
uow = new UnitOfWork(t.em, new SqliteEngine());
|
||||
days = t.repo(Days);
|
||||
});
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
import { ADDON_IDS } from '../../../src/addons';
|
||||
import { getMcpSafeUrl } from '../../../src/app-config';
|
||||
import { db as testDb } from '../../../src/db/database';
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { OauthClients } from '../../../src/db/entities/OauthClients.entity';
|
||||
import { OauthConsents } from '../../../src/db/entities/OauthConsents.entity';
|
||||
@@ -132,7 +133,7 @@ beforeAll(async () => {
|
||||
consentsRepo,
|
||||
addonsStub,
|
||||
new AuditService(auditLogRepo, usersRepo),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
cimdStub,
|
||||
);
|
||||
});
|
||||
@@ -1446,7 +1447,7 @@ describe('module-scoped OAuth state', () => {
|
||||
consentsRepo,
|
||||
addonsStub,
|
||||
new AuditService(auditLogRepo, usersRepo),
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
cimdStub,
|
||||
);
|
||||
expect((await secondInstance.consumeAuthCode(code))?.userId).toBe(42);
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
/**
|
||||
* PostgresDatabaseBackup: the backup port on a Postgres database, over
|
||||
* stand-in pg_dump, pg_restore and psql tools (TREK_DB_PG_BIN_DIR, built by
|
||||
* tests/helpers/fake-pg-tools.ts) and a stand-in DatabaseLifecycle. The tools'
|
||||
* own arguments are pinned in tests/unit/db/engine/postgres/pg-tools.test.ts.
|
||||
*/
|
||||
import { deriveDb } from '../../../src/app-config/derive';
|
||||
import { resolveEngine } from '../../../src/db/engine';
|
||||
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 {
|
||||
answersSchema,
|
||||
failsWith,
|
||||
installFakeTool,
|
||||
listsMigrations,
|
||||
writesFile,
|
||||
writesRestrictedScript,
|
||||
} from '../../helpers/fake-pg-tools';
|
||||
|
||||
import { 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;
|
||||
|
||||
/** The adapter over the settings a process reads from these variables, with stand-ins for the lifecycle and the data directory. */
|
||||
function postgresBackup(env: Record<string, string>): PostgresDatabaseBackup {
|
||||
const db = deriveDb(env);
|
||||
return new PostgresDatabaseBackup(
|
||||
{ reopen } as unknown as DatabaseLifecycle,
|
||||
{ dataDir: root } as unknown as DataPathsService,
|
||||
resolveEngine(db),
|
||||
db,
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
root = mkdtempSync(path.join(tmpdir(), 'trek-pg-backup-'));
|
||||
bin = path.join(root, 'bin');
|
||||
extract = path.join(root, 'extract');
|
||||
mkdirSync(bin);
|
||||
mkdirSync(extract);
|
||||
installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
|
||||
reopen = vi.fn(async () => undefined);
|
||||
backup = postgresBackup({ TREK_DB_URL: 'postgresql://trek:pw@db/trek', TREK_DB_PG_BIN_DIR: bin });
|
||||
});
|
||||
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();
|
||||
installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
|
||||
await backup.snapshot(path.join(root, 'a.pgdump'));
|
||||
expect(existsSync(path.join(root, 'a.pgdump'))).toBe(true);
|
||||
installFakeTool(bin, 'pg_dump', { snippets: [writesFile('partial'), failsWith('connection refused')] });
|
||||
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 accepts a TREK dump this build knows', async () => {
|
||||
writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations(KNOWN.slice(0, 3))] });
|
||||
await expect(backup.verify(extract)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('PGBACKUP-002a: verify refuses an archive without a TREK dump in it', async () => {
|
||||
expect(await refusal(backup.verify(extract))).toEqual({
|
||||
status: 400,
|
||||
message: 'Invalid backup: travel.pgdump not found',
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
id: 'PGBACKUP-002b',
|
||||
setup: () => writeFileSync(path.join(extract, 'travel.db'), 'sqlite'),
|
||||
message: /holds a SQLite database/,
|
||||
},
|
||||
{
|
||||
id: 'PGBACKUP-002c',
|
||||
setup: () => {
|
||||
writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
|
||||
installFakeTool(bin, 'pg_restore', {
|
||||
snippets: [listsMigrations([...KNOWN, 'Migration29990101000000_from_the_future'])],
|
||||
});
|
||||
},
|
||||
message: /made by a newer TREK version/,
|
||||
},
|
||||
{
|
||||
id: 'PGBACKUP-002d',
|
||||
setup: () => {
|
||||
writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations([])] });
|
||||
},
|
||||
message: /holds no TREK migration history/,
|
||||
},
|
||||
{
|
||||
id: 'PGBACKUP-002e',
|
||||
setup: () => {
|
||||
writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [failsWith('pg_restore: error: not a valid archive')] });
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
},
|
||||
message: 'Uploaded file is not a valid Postgres dump',
|
||||
},
|
||||
])('$id: verify refuses a dump TREK cannot restore', async ({ setup, message }) => {
|
||||
setup();
|
||||
const refused = await refusal(backup.verify(extract));
|
||||
if (typeof message === 'string') expect(refused.message).toBe(message);
|
||||
else expect(refused.message).toMatch(message);
|
||||
});
|
||||
|
||||
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);
|
||||
installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
|
||||
const kept = await backup.keepCopyBeforeRestore();
|
||||
expect(kept).toMatch(/pre-restore-\d+\.pgdump$/);
|
||||
expect(existsSync(kept as string)).toBe(true);
|
||||
installFakeTool(bin, 'pg_dump', { snippets: [failsWith('connection refused')] });
|
||||
await expect(backup.keepCopyBeforeRestore()).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('PGBACKUP-004: replace restores, then reopens; a reopen failure is reported, not thrown', async () => {
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [writesRestrictedScript()] });
|
||||
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 () => {
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [failsWith('connection refused')] });
|
||||
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');
|
||||
await expect(postgresBackup({}).snapshot(path.join(root, 'c.pgdump'))).rejects.toThrow(/without TREK_DB_URL/);
|
||||
});
|
||||
});
|
||||
@@ -2,13 +2,14 @@
|
||||
* SqliteDatabaseBackup: the database backup port for a SQLite file.
|
||||
*
|
||||
* The snapshot and the swap run for real here, against a database file in a
|
||||
* temp directory: a VACUUM INTO through the real MaintenanceRepository, and the
|
||||
* temp directory: a VACUUM INTO through the real SqliteMaintenance, and the
|
||||
* copy, sidecar removal and rename of a restore. Only the connection lifecycle
|
||||
* is a stand-in, because closing and reopening the process's own connection is
|
||||
* DatabaseLifecycle's business (tests/unit/nest/database/).
|
||||
*/
|
||||
import { MaintenanceRepository } from '../../../src/db/repositories/MaintenanceRepository';
|
||||
import { SqliteMaintenance } from '../../../src/db/engine/sqlite/sqlite-maintenance';
|
||||
import { SqliteDatabaseBackup } from '../../../src/nest/backup/sqlite-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 { withRequestContext } from '../../../src/nest/database/request-context';
|
||||
@@ -40,7 +41,7 @@ function maintenanceStub() {
|
||||
function portWith(maintenance: { walCheckpoint: unknown; vacuumInto: unknown }): SqliteDatabaseBackup {
|
||||
return new SqliteDatabaseBackup(
|
||||
lifecycle as unknown as DatabaseLifecycle,
|
||||
maintenance as unknown as MaintenanceRepository,
|
||||
maintenance as unknown as SqliteMaintenance,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -116,7 +117,7 @@ describe('SqliteDatabaseBackup', () => {
|
||||
// test-sql-allow: the marker table is a bare fixture no entity maps; the write has to land in the WAL of this file.
|
||||
live.prepare('UPDATE marker SET label = ?').run('after');
|
||||
const target = path.join(dir, 'copy.db');
|
||||
const port = portWith(new MaintenanceRepository(orm.em));
|
||||
const port = portWith(new SqliteMaintenance(orm.em));
|
||||
|
||||
await withRequestContext(orm.orm, () => port.snapshot(target));
|
||||
|
||||
@@ -125,7 +126,7 @@ describe('SqliteDatabaseBackup', () => {
|
||||
|
||||
it('SQLBK-005: a VACUUM INTO that fails rejects and leaves nothing at the target', async () => {
|
||||
const target = path.join(dir, 'missing-dir', 'copy.db');
|
||||
const port = portWith(new MaintenanceRepository(orm.em));
|
||||
const port = portWith(new SqliteMaintenance(orm.em));
|
||||
|
||||
await expect(withRequestContext(orm.orm, () => port.snapshot(target))).rejects.toThrow();
|
||||
|
||||
@@ -146,11 +147,16 @@ describe('SqliteDatabaseBackup', () => {
|
||||
expect(fs.existsSync(target)).toBe(false);
|
||||
});
|
||||
|
||||
it('SQLBK-007: verify refuses an unpacked archive without a database', () => {
|
||||
expect(portWith(maintenanceStub()).verify(dir)).toEqual({
|
||||
error: 'Invalid backup: travel.db not found',
|
||||
status: 400,
|
||||
});
|
||||
it('SQLBK-007: verify refuses an unpacked archive without a database, as a DomainError the restore route answers', async () => {
|
||||
const refusal = await portWith(maintenanceStub())
|
||||
.verify(dir)
|
||||
.then(
|
||||
() => null,
|
||||
(err: unknown) => err,
|
||||
);
|
||||
expect(refusal).toBeInstanceOf(DomainError);
|
||||
expect((refusal as DomainError).publicMessage).toBe('Invalid backup: travel.db not found');
|
||||
expect((refusal as DomainError).getStatus()).toBe(400);
|
||||
});
|
||||
|
||||
it('SQLBK-008: keeps a copy of the database a restore replaces next to it, and says where', async () => {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
* bundle), via a real MikroORM over the full migrated schema (`createSnapshotTestDb`
|
||||
* + `createTestOrm`) rather than a hand-rolled `:memory:` table set.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
|
||||
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
|
||||
@@ -47,7 +48,7 @@ beforeAll(async () => {
|
||||
actions: t.repo(PluginActions),
|
||||
settingsFields: t.repo(PluginSettingsFields),
|
||||
errorLog: t.repo(PluginErrorLog),
|
||||
uow: new UnitOfWork(t.em),
|
||||
uow: new UnitOfWork(t.em, new SqliteEngine()),
|
||||
};
|
||||
});
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* https/SSRF guard on the endpoints, the code + refresh token exchanges (mocked fetch),
|
||||
* tokens encrypted at rest, and a stored refresh token that the plugin never sees.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { PluginOauthState } from '../../../src/db/entities/PluginOauthState.entity';
|
||||
import { PluginOauthTokens } from '../../../src/db/entities/PluginOauthTokens.entity';
|
||||
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
|
||||
@@ -90,7 +91,7 @@ async function makeOauthService(): Promise<PluginOAuthService> {
|
||||
orm.repo(PluginOauthTokens),
|
||||
orm.repo(PluginOauthState),
|
||||
orm.repo(PluginSettingsFields),
|
||||
new UnitOfWork(orm.em),
|
||||
new UnitOfWork(orm.em, new SqliteEngine()),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
* defensible: dropping the `logError` call, or falling through to `activate`
|
||||
* anyway, would ship green with no test failing. This file pins it directly.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
@@ -99,7 +100,7 @@ async function buildRuntime(withOrm: boolean): Promise<PluginRuntimeService> {
|
||||
t.repo(NotificationChannelPreferences),
|
||||
// Plan 4 Task 4: `uow` is no longer `@Optional()` — ordered ahead of
|
||||
// `registry?`/`hostFactory?`, matching the real constructor's own order.
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
undefined,
|
||||
undefined,
|
||||
withOrm ? t.orm : undefined,
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
* shared helpers use to let ordinary unit tests call a repository with no
|
||||
* request wrapper at all: that default would hide this exact bug.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
|
||||
import { Trips } from '../../../src/db/entities/Trips.entity';
|
||||
import { Users } from '../../../src/db/entities/Users.entity';
|
||||
@@ -70,7 +71,7 @@ let userId: number;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb, { allowGlobalContext: false });
|
||||
permissions = new PermissionsService(t.repo(AppSettings), new UnitOfWork(t.em));
|
||||
permissions = new PermissionsService(t.repo(AppSettings), new UnitOfWork(t.em, new SqliteEngine()));
|
||||
// Plan 3j Task 1: PluginGuards' own role lookup (canCreateAs, PG4) now goes
|
||||
// through UsersRepository.getRole — a REAL repository bound to `t.em`
|
||||
// (allowGlobalContext: false, same as `permissions`' AppSettingsRepository
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
* which for a notification channel meant being dispatched to everyone with no credentials.
|
||||
*/
|
||||
import { db as testDb } from '../../../src/db/database';
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
@@ -204,7 +205,7 @@ describe('a plugin channel label is bounded by the host', () => {
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
// Plan 4 Task 4: `uow` is no longer `@Optional()`.
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
);
|
||||
// Stand the plugin up as a granted, active notificationChannel provider.
|
||||
(rt as unknown as { supervisor: { running: Map<string, unknown> } }).supervisor.running.set('loud', {
|
||||
|
||||
@@ -128,6 +128,8 @@ describe('dialect-lint.mjs', () => {
|
||||
it('DIALECT-006: PRAGMA is allowed in the src/db lifecycle files, not in a repository or a domain', () => {
|
||||
const pragma = "export const p = 'PRAGMA wal_checkpoint(TRUNCATE)';\n";
|
||||
expect(run(serverRoot({ 'src/db/durability.ts': pragma })).status).toBe(0);
|
||||
expect(run(serverRoot({ 'src/db/engine/sqlite/sqlite-maintenance.ts': pragma })).status).toBe(0);
|
||||
expect(run(serverRoot({ 'src/db/engine/postgres/maintenance.ts': pragma })).status).toBe(1);
|
||||
expect(run(serverRoot({ [REPO]: pragma })).status).toBe(1);
|
||||
expect(run(serverRoot({ 'src/nest/backup/probe.ts': pragma })).status).toBe(1);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* scripts/engine-dispatch-ratchet.mjs (lint:engine-dispatch): an engine check
|
||||
* outside src/db/dialect/ and src/db/engine/ may only shrink. Runs as the real
|
||||
* script against throwaway server roots (--dir) and once on this repository,
|
||||
* so the committed baseline is checked by the unit suite.
|
||||
*/
|
||||
import { countEngineDispatch } from '../../../scripts/engine-dispatch-ratchet.mjs';
|
||||
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
|
||||
const SCRIPT = path.join(__dirname, '../../../scripts/engine-dispatch-ratchet.mjs');
|
||||
const SERVER_ROOT = path.join(__dirname, '../../..');
|
||||
const BASELINE = 'scripts/engine-dispatch-baseline.json';
|
||||
|
||||
const roots: string[] = [];
|
||||
|
||||
function serverRoot(files: Record<string, string>, counts: Record<string, number> = {}): string {
|
||||
const dir = mkdtempSync(path.join(tmpdir(), 'trek-engine-dispatch-'));
|
||||
roots.push(dir);
|
||||
mkdirSync(path.join(dir, 'src'), { recursive: true });
|
||||
mkdirSync(path.join(dir, 'scripts'), { recursive: true });
|
||||
for (const [file, text] of Object.entries(files)) {
|
||||
mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
|
||||
writeFileSync(path.join(dir, file), text);
|
||||
}
|
||||
writeFileSync(path.join(dir, BASELINE), JSON.stringify({ counts }));
|
||||
return dir;
|
||||
}
|
||||
|
||||
function run(dir: string, ...args: string[]): { status: number | null; out: string } {
|
||||
try {
|
||||
const stdout = execFileSync(process.execPath, [SCRIPT, `--dir=${dir}`, ...args], {
|
||||
encoding: 'utf8',
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
return { status: 0, out: stdout };
|
||||
} catch (err) {
|
||||
const { status, stdout, stderr } = err as { status: number | null; stdout: string; stderr: string };
|
||||
return { status, out: `${stdout}${stderr}` };
|
||||
}
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of roots.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
// Spelled in pieces so this file does not read as an engine check itself.
|
||||
const IS_SQLITE = ['isSql', 'ite(platform)'].join('');
|
||||
const IS_POSTGRES = ['isPost', 'gres(p)'].join('');
|
||||
const INSTANCE = ['x instanceof ', 'SqlitePlatform'].join('');
|
||||
|
||||
describe('lint:engine-dispatch', () => {
|
||||
it('ENGDISP-001: counts the platform checks and instanceof tests, not the ones in comments', () => {
|
||||
expect(countEngineDispatch(`if (${IS_SQLITE}) a(); else if (${IS_POSTGRES}) b();`)).toBe(2);
|
||||
expect(countEngineDispatch(`const y = ${INSTANCE};`)).toBe(1);
|
||||
const BY_NAME = ["if (readEnv().db.engine === 'post", 'gres\') a(); if (engine.id !== "sql', 'ite") b();'].join('');
|
||||
expect(countEngineDispatch(BY_NAME)).toBe(2);
|
||||
expect(countEngineDispatch("if (user.id === 'admin') a();")).toBe(0);
|
||||
expect(countEngineDispatch(`// ${IS_SQLITE}\n/* ${INSTANCE} */\nconst ok = 1;`)).toBe(0);
|
||||
});
|
||||
|
||||
it('ENGDISP-002: the dialect layer and the engine port may dispatch, any other file may not', () => {
|
||||
const text = `export const x = ${IS_SQLITE};\n`;
|
||||
expect(run(serverRoot({ 'src/db/dialect/a.ts': text, 'src/db/engine/sqlite/b.ts': text })).status).toBe(0);
|
||||
const domain = run(serverRoot({ 'src/nest/trips/trips.service.ts': text }));
|
||||
expect(domain.status).toBe(1);
|
||||
expect(domain.out).toContain('src/nest/trips/trips.service.ts');
|
||||
});
|
||||
|
||||
it('ENGDISP-003: a baselined file is held at its count, and --update only lowers it', () => {
|
||||
const text = `export const x = ${IS_SQLITE};\n`;
|
||||
const file = 'src/db/repositories/A.repository.ts';
|
||||
expect(run(serverRoot({ [file]: text }, { [file]: 1 })).status).toBe(0);
|
||||
expect(run(serverRoot({ [file]: `${text}${text}` }, { [file]: 1 })).status).toBe(1);
|
||||
const stale = serverRoot({ [file]: 'export const x = 1;\n' }, { [file]: 1 });
|
||||
expect(run(stale).status).toBe(1);
|
||||
expect(run(stale, '--update').status).toBe(0);
|
||||
expect(JSON.parse(readFileSync(path.join(stale, BASELINE), 'utf8'))).toEqual({ counts: {} });
|
||||
});
|
||||
|
||||
it('ENGDISP-004: the repository holds its committed baseline', () => {
|
||||
expect(run(SERVER_ROOT).status).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -7,13 +7,14 @@
|
||||
* sample (or a pinned reason it has none).
|
||||
*/
|
||||
import {
|
||||
PRESENCE_LABEL,
|
||||
planRepositories,
|
||||
readCompilerOptions,
|
||||
sampleString,
|
||||
type MethodPlan,
|
||||
} from '../../../../scripts/pg-probe/arg-samples';
|
||||
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import ts from 'typescript';
|
||||
@@ -47,6 +48,7 @@ export class ThingsRepository {
|
||||
async record(r: Record<string, number>): Promise<void> {}
|
||||
async literals(n: bigint, flag: true): Promise<void> {}
|
||||
async destructured({ id, label }: { id: number; label: string }): Promise<void> {}
|
||||
async presence(write: { name?: readonly [present: boolean, value: string]; flag?: readonly [boolean, number] }): Promise<void> {}
|
||||
}
|
||||
export abstract class AbstractRepository {
|
||||
async skipped(): Promise<void> {}
|
||||
@@ -102,6 +104,7 @@ describe('pg-probe argument samples', () => {
|
||||
'literals',
|
||||
'nullable',
|
||||
'optionalCallback',
|
||||
'presence',
|
||||
'record',
|
||||
'rest',
|
||||
'tuple',
|
||||
@@ -145,6 +148,28 @@ describe('pg-probe argument samples', () => {
|
||||
expect(argsOf('collections')).toEqual([new Set([1]), new Map([['1', 1]])]);
|
||||
});
|
||||
|
||||
it('PGPROBE-037: a presence pair is present, so a patch built from it reaches its SQL', () => {
|
||||
// The repositories' partial updates take `[present: boolean, value]` pairs
|
||||
// and send nothing when no pair is present; a false flag would leave the
|
||||
// method unmeasured. Only the element labelled `present` turns true.
|
||||
expect(argsOf('presence')).toEqual([{ name: [true, 'probe'], flag: [false, 1] }]);
|
||||
});
|
||||
|
||||
it('PGPROBE-038: the presence label the sampler reads is the one presenceSet declares', () => {
|
||||
// The sampler matches the tuple label by name; a rename in the repository
|
||||
// helper would stop the flag being sampled true, silently unmeasuring every
|
||||
// partial update. Read the real signature, not a copy of it.
|
||||
const file = path.join(__dirname, '../../../../src/db/repositories/_shared/presence-set.ts');
|
||||
const source = ts.createSourceFile(file, readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
|
||||
const labels: string[] = [];
|
||||
const visit = (node: ts.Node): void => {
|
||||
if (ts.isNamedTupleMember(node)) labels.push(node.name.text);
|
||||
ts.forEachChild(node, visit);
|
||||
};
|
||||
visit(source);
|
||||
expect(labels).toContain(PRESENCE_LABEL);
|
||||
});
|
||||
|
||||
it('PGPROBE-035: a required callback makes the method unprobeable, an optional one is left off', () => {
|
||||
expect(planOf('withCallback')).toMatchObject({ unprobeable: 'fn: a function parameter' });
|
||||
expect(argsOf('optionalCallback')).toEqual([1]);
|
||||
|
||||
@@ -130,11 +130,13 @@ describe('pg-probe baseline', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('PGPROBE-010: only syntax and feature errors count toward the ratchet, not data errors from the sample values', () => {
|
||||
for (const code of ['42601', '42883', '42804', '42P01', '42703', '0A000'])
|
||||
it('PGPROBE-010: syntax, feature and data errors count toward the ratchet; constraints and timeouts do not', () => {
|
||||
// A data exception (class 22) is a value of the wrong type reaching a
|
||||
// column: a string the SQLite comparison let through where Postgres
|
||||
// refuses it, so it counts once the sampler sends the declared types.
|
||||
for (const code of ['42601', '42883', '42804', '42P01', '42703', '0A000', '22P02', '22007', '22008'])
|
||||
expect(countsTowardRatchet(code)).toBe(true);
|
||||
for (const code of ['22P02', '22007', '22008', '23505', '57014', 'NOSQLSTATE'])
|
||||
expect(countsTowardRatchet(code)).toBe(false);
|
||||
for (const code of ['23505', '23514', '57014', 'NOSQLSTATE']) expect(countsTowardRatchet(code)).toBe(false);
|
||||
});
|
||||
|
||||
it('PGPROBE-008: the checked-in baseline parses and formats back to itself', () => {
|
||||
|
||||
@@ -47,7 +47,7 @@ const RESULTS: MethodResult[] = [
|
||||
{ key: 'F.thrower', statements: [], threw: 'no trip 1' },
|
||||
{
|
||||
key: 'G.sampled',
|
||||
statements: [refused('select $1::date', '22007', 'invalid input syntax for type date: "probe"'), ok('select 5')],
|
||||
statements: [refused('insert into t values ($1)', '23514', 'violates check constraint "t_check"'), ok('select 5')],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -84,26 +84,30 @@ describe('pg-probe report', () => {
|
||||
new Map([
|
||||
['42883', 1],
|
||||
['42601', 1],
|
||||
['22007', 1],
|
||||
['23514', 1],
|
||||
]),
|
||||
);
|
||||
expect(summary.failedStatements.map((f) => `${f.method} ${f.code} ${f.counted}`)).toEqual([
|
||||
'B.broken 42883 true',
|
||||
'B.broken 42601 true',
|
||||
'G.sampled 22007 false',
|
||||
'G.sampled 23514 false',
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGPROBE-068: a data error from a sample value is reported but not held to the baseline', () => {
|
||||
it('PGPROBE-068: a data error is held to the baseline, a constraint over a sample value is only reported', () => {
|
||||
const summary = summarize([
|
||||
{
|
||||
key: 'G.sampled',
|
||||
statements: [refused('select $1::date', '22P02', 'bad'), refused('select x', '0A000', 'nope')],
|
||||
statements: [
|
||||
refused('select $1::date', '22P02', 'bad'),
|
||||
refused('select x', '0A000', 'nope'),
|
||||
refused('insert into t values ($1)', '23514', 'check'),
|
||||
],
|
||||
},
|
||||
]);
|
||||
expect(summary.failingByMethod.get('G.sampled')).toBe(1);
|
||||
expect(splitRefusals(summary).counted.map((f) => f.code)).toEqual(['0A000']);
|
||||
expect(splitRefusals(summary).reported.map((f) => f.code)).toEqual(['22P02']);
|
||||
expect(summary.failingByMethod.get('G.sampled')).toBe(2);
|
||||
expect(splitRefusals(summary).counted.map((f) => f.code)).toEqual(['22P02', '0A000']);
|
||||
expect(splitRefusals(summary).reported.map((f) => f.code)).toEqual(['23514']);
|
||||
});
|
||||
|
||||
it('PGPROBE-069: names every method whose SQL the probe did not see, and why', () => {
|
||||
@@ -187,9 +191,9 @@ describe('pg-probe report', () => {
|
||||
expect(text).toContain('FAIL [postgres] dateOf: expected 1, got 2');
|
||||
expect(text).toContain('collation "NOCASE" does not exist');
|
||||
expect(text).toContain(
|
||||
'Statements: 8 distinct, 3 refused by Postgres (2 as SQL, held to the baseline; 1 over a sample value',
|
||||
"Statements: 8 distinct, 3 refused by Postgres (2 as SQL or over a value's type, held to the baseline; 1 over a constraint",
|
||||
);
|
||||
expect(text).toContain(' 22007: 1 (not held)');
|
||||
expect(text).toContain(' 23514: 1 (not held)');
|
||||
expect(text).toContain(' F.thrower: threw before any statement: no trip 1');
|
||||
expect(text).toContain(
|
||||
'FAIL F.thrower threw before any statement: no trip 1, and the baseline does not list it as uncovered.',
|
||||
@@ -217,9 +221,9 @@ describe('pg-probe report', () => {
|
||||
expect(md).toContain("| postgres | a\\|b | got 'x' |");
|
||||
expect(md).toContain('| 42883 | 1 |');
|
||||
expect(md).toContain('| E.skipped | could not be called: fn: a function parameter |');
|
||||
expect(md).toContain('| Refused by Postgres as SQL (held) | 2 |');
|
||||
expect(md).toContain('| Refused over a sample value or the empty tables (reported only) | 1 |');
|
||||
expect(md).toContain('| G.sampled | 22007 | no | invalid input syntax for type date: "probe" |');
|
||||
expect(md).toContain("| Refused by Postgres as SQL or over a value's type (held) | 2 |");
|
||||
expect(md).toContain('| Refused over a constraint, a timeout or the empty tables (reported only) | 1 |');
|
||||
expect(md).toContain('| G.sampled | 23514 | no | violates check constraint "t_check" |');
|
||||
const ratchet = formatMarkdown(
|
||||
input({ verdict: { ...CLEAN, newlyUncovered: ['C.silent'], nowCovered: ['Z.gone'] } }),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user