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.
This commit is contained in:
Isaias Tavares
2026-11-03 09:06:41 -03:00
parent c72b680fec
commit 3f1a786313
5 changed files with 54 additions and 8 deletions
+7
View File
@@ -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<Options>;
/**
* 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
@@ -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<void> {
@@ -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()}`;
@@ -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<void> {
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 });
@@ -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);