mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
98533f3dbe
The settings for the database were one flat record whose url and pool size
were meaningful only for Postgres, so every reader had to re-check the URL.
They are now a discriminated union per engine (DbEngineSettings): the
Postgres arm carries its URL and a pool size already capped at the ceiling,
and the SQLite arm carries nothing else. The env schema uses the same ceiling
constant, so the boot check and the derivation cannot drift apart.
The engine was resolved from the environment by every caller that needed it
(the lifecycle, the unit of work, the backup adapters), each building its own
instance. The engine is now provided once per container by
DatabaseEngineModule and injected everywhere, so the state an engine keeps
(Postgres's reopen hook) is the same wherever it is used. resolveEngine stays
for the MikroORM CLI config and the DI factory, and nothing else calls it.
The engine's reopen no longer carries a `reopenSwapsConnection` flag. The
engine itself decides what a reopen needs: SQLite closes and reconnects the
ORM's connection before the schema bootstrap (its handle is swapped under
Kysely's cache), and Postgres only migrates, because a restore replaces the
contents of the database the pool already reaches. The Postgres pool is never
closed on a reopen, since ending it waits for connections lent out during the
restore.
The backup ports answer every method the same way. verify() now throws a
DomainError for a refused archive on both engines, and the restore path maps
it back to the same { success: false, error, status } result, so the HTTP
response of a refused restore (status and body) is unchanged. The restore
step that swaps the database and then always reopens it is shared in
nest/backup/database-swap.ts, with the error-description helper in
nest/common, instead of two copies that could drift.
86 lines
3.5 KiB
TypeScript
86 lines
3.5 KiB
TypeScript
/**
|
|
* 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();
|
|
}
|
|
});
|
|
});
|