From 3f1a786313586f2a86022126b2b0713b2907e354 Mon Sep 17 00:00:00 2001 From: Isaias Tavares Date: Tue, 3 Nov 2026 09:06:41 -0300 Subject: [PATCH] fix(db): keep the Postgres pool across a restore The reopen after a restore closed the ORM's pool and waited for it to end. Ending a pg pool waits for every connection it has lent out, and one lent to a job while the restore ran was never handed back, so the restore request never returned and every later query failed on a destroyed driver. A Postgres restore replaces the contents of the same database, so the pool now stays and the reopen only migrates; SQLite still reconnects around its swapped file. --- server/src/db/engine/engine.ts | 7 +++++ .../src/db/engine/postgres/postgres-engine.ts | 10 +++++-- server/src/db/engine/sqlite/sqlite-engine.ts | 1 + .../database/database-lifecycle.service.ts | 16 +++++++---- .../database-lifecycle.service.test.ts | 28 +++++++++++++++++++ 5 files changed, 54 insertions(+), 8 deletions(-) diff --git a/server/src/db/engine/engine.ts b/server/src/db/engine/engine.ts index 821c051e5..08681c22e 100644 --- a/server/src/db/engine/engine.ts +++ b/server/src/db/engine/engine.ts @@ -21,6 +21,13 @@ export interface DatabaseEngine { describe(): string; /** What MikroORM needs to reach the database: the driver and its target, the pool, driver options. */ ormOptions(): Partial; + /** + * Whether a reopen hands the ORM a different connection, so its client has + * to be closed and connected again around it. SQLite swaps the file under + * one handle; a Postgres restore replaces the contents of the same database, + * which the pool's connections keep reaching. + */ + readonly reopenSwapsConnection: boolean; /** * Called once at boot, before the schema bootstrap. `onReopened` is what * has to follow a later reopen (rebinding the ORM, migrating the restored diff --git a/server/src/db/engine/postgres/postgres-engine.ts b/server/src/db/engine/postgres/postgres-engine.ts index 883e32d78..f453f14d1 100644 --- a/server/src/db/engine/postgres/postgres-engine.ts +++ b/server/src/db/engine/postgres/postgres-engine.ts @@ -67,6 +67,12 @@ const TREK_WRITE_LOCK = 0x5452454b; */ export class PostgresEngine implements DatabaseEngine { readonly id = 'postgres'; + /** + * The pool stays: ending it waits for every connection it has lent out, and + * one lent to a job while the restore ran was never handed back to the + * ending pool, so the restore never returned. + */ + readonly reopenSwapsConnection = false; constructor( private readonly settings: PostgresSettings, @@ -85,8 +91,8 @@ export class PostgresEngine implements DatabaseEngine { } /** - * After `pg_restore` replaced the database's contents: the ORM reconnects - * and the restored database is migrated forward (`onReopened`, which + * After `pg_restore` replaced the database's contents: the restored + * database is migrated forward on the same pool (`onReopened`, which * `DatabaseLifecycle` hands to `open`). */ async reopen(): Promise { diff --git a/server/src/db/engine/sqlite/sqlite-engine.ts b/server/src/db/engine/sqlite/sqlite-engine.ts index 0f3523298..ff46499f5 100644 --- a/server/src/db/engine/sqlite/sqlite-engine.ts +++ b/server/src/db/engine/sqlite/sqlite-engine.ts @@ -14,6 +14,7 @@ import type { EntityManager } from '@mikro-orm/core'; */ export class SqliteEngine implements DatabaseEngine { readonly id = 'sqlite'; + readonly reopenSwapsConnection = true; describe(): string { return `SQLite file ${resolveDbPath()}`; diff --git a/server/src/nest/database/database-lifecycle.service.ts b/server/src/nest/database/database-lifecycle.service.ts index a5f3db92c..a9a24dfa5 100644 --- a/server/src/nest/database/database-lifecycle.service.ts +++ b/server/src/nest/database/database-lifecycle.service.ts @@ -78,14 +78,18 @@ export class DatabaseLifecycle { /** * Kysely caches whatever handle it was given, so without this the ORM would * keep talking to the closed one. Closing and reconnecting sends the driver - * back through `createKyselyDialect()`, which picks up the new handle. + * back through `createKyselyDialect()`, which picks up the new handle. An + * engine whose reopen keeps the connection (Postgres) skips that and only + * migrates. */ private async rebindOrm(): Promise { - const connection = this.orm.em.getConnection(); - // The handle is owned and closed by `db/database.ts`; the bound driver's - // `destroy()` is a no-op (see `orm-driver.ts`), so this close never reaches it. - await connection.close(true); - await connection.connect(); + if (this.engine.reopenSwapsConnection) { + const connection = this.orm.em.getConnection(); + // The handle is owned and closed by `db/database.ts`; the bound driver's + // `destroy()` is a no-op (see `orm-driver.ts`), so this close never reaches it. + await connection.close(true); + await connection.connect(); + } // No pre-migrate snapshot: the file was just unpacked from an archive that // is still there to go back to. await this.engine.schemaBootstrap(this.orm, { snapshot: false }); diff --git a/server/tests/unit/nest/database/database-lifecycle.service.test.ts b/server/tests/unit/nest/database/database-lifecycle.service.test.ts index d639c1fef..77fafa990 100644 --- a/server/tests/unit/nest/database/database-lifecycle.service.test.ts +++ b/server/tests/unit/nest/database/database-lifecycle.service.test.ts @@ -7,6 +7,7 @@ * 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 { DatabaseLifecycle } from '../../../../src/nest/database/database-lifecycle.service'; import type { MikroORM } from '@mikro-orm/core'; @@ -120,6 +121,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 () => { + vi.stubEnv('TREK_DB_URL', 'postgres://trek:pw@db/trek'); + 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); + 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(); + vi.unstubAllEnvs(); + } + }); + it('DBLIFE-003: close() and reopen() go through the connection module', async () => { const lifecycle = new DatabaseLifecycle(ormStub().orm);