mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
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:
@@ -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()}`;
|
||||
|
||||
Reference in New Issue
Block a user