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:
Isaias Tavares
2026-11-05 01:15:21 -03:00
committed by Maurice
parent 17b47b38b9
commit 2e3762afe1
176 changed files with 6728 additions and 777 deletions
@@ -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(() => {