From dc00374585a0ff3f7686a422143c5c128ddbb87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Thu, 10 Nov 2022 12:57:52 +0100 Subject: [PATCH] fix(schema): do not cache knex instance When a database is missing, the ensuring mechanism is reconnecting, hence the cached knex instance becomes outdated. Instead of replacing it, it's better to use a getter and always have up to date value. Closes #3713 --- packages/knex/src/schema/SchemaGenerator.ts | 5 ++++- packages/migrations/src/MigrationStorage.ts | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/knex/src/schema/SchemaGenerator.ts b/packages/knex/src/schema/SchemaGenerator.ts index d2d46c95945d..26ea662b2841 100644 --- a/packages/knex/src/schema/SchemaGenerator.ts +++ b/packages/knex/src/schema/SchemaGenerator.ts @@ -13,7 +13,6 @@ import { SchemaComparator } from './SchemaComparator'; export class SchemaGenerator extends AbstractSchemaGenerator { private readonly helper = this.platform.getSchemaHelper()!; - private readonly knex = this.connection.getKnex(); private readonly options = this.config.get('schemaGenerator'); /** @deprecated use `dropSchema` and `createSchema` commands respectively */ @@ -586,4 +585,8 @@ export class SchemaGenerator extends AbstractSchemaGenerator return tmp ? tmp + append : ''; } + private get knex() { + return this.connection.getKnex(); + } + } diff --git a/packages/migrations/src/MigrationStorage.ts b/packages/migrations/src/MigrationStorage.ts index 9e60a55ba8e1..589119e58eaa 100644 --- a/packages/migrations/src/MigrationStorage.ts +++ b/packages/migrations/src/MigrationStorage.ts @@ -7,7 +7,6 @@ import type { MigrationRow } from './typings'; export class MigrationStorage implements UmzugStorage { private readonly connection = this.driver.getConnection(); - private readonly knex = this.connection.getKnex(); private readonly helper = this.driver.getPlatform().getSchemaHelper()!; private masterTransaction?: Transaction; @@ -110,4 +109,8 @@ export class MigrationStorage implements UmzugStorage { return { tableName, schemaName }; } + private get knex() { + return this.connection.getKnex(); + } + }