fix(core): Ensure DB repositories are initialized before the DB migrations are run (#6220)

also remove the need to re-open sqlite db connection
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-05-10 15:53:04 +00:00 committed by GitHub
parent ed3bc154b0
commit 500c0ebce3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 40 deletions

View file

@ -126,12 +126,6 @@ export function getConnectionOptions(dbType: DatabaseType): ConnectionOptions {
}
}
const openConnection = async (options: ConnectionOptions) => {
connection = new Connection(options);
await connection.initialize();
Container.set(Connection, connection);
};
export async function init(testConnectionOptions?: ConnectionOptions): Promise<void> {
if (connectionState.connected) return;
@ -160,7 +154,9 @@ export async function init(testConnectionOptions?: ConnectionOptions): Promise<v
migrationsRun: false,
});
await openConnection(connectionOptions);
connection = new Connection(connectionOptions);
Container.set(Connection, connection);
await connection.initialize();
if (dbType === 'postgresdb') {
const schema = config.getEnv('database.postgresdb.schema');
@ -173,37 +169,6 @@ export async function init(testConnectionOptions?: ConnectionOptions): Promise<v
}
connectionState.connected = true;
}
export async function migrate() {
(connection.options.migrations as Migration[]).forEach(wrapMigration);
if (!inTest && connection.options.type === 'sqlite') {
// This specific migration changes database metadata.
// A field is now nullable. We need to reconnect so that
// n8n knows it has changed. Happens only on sqlite.
let migrations = [];
try {
const tablePrefix = config.getEnv('database.tablePrefix');
migrations = await connection.query(
`SELECT id FROM ${tablePrefix}migrations where name = "MakeStoppedAtNullable1607431743769"`,
);
} catch (error) {
// Migration table does not exist yet - it will be created after migrations run for the first time.
}
// If you remove this call, remember to turn back on the
// setting to run migrations automatically above.
await connection.runMigrations({ transaction: 'each' });
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (migrations.length === 0) {
await connection.destroy();
await openConnection(connection.options);
}
} else {
await connection.runMigrations({ transaction: 'each' });
}
collections.AuthIdentity = Container.get(AuthIdentityRepository);
collections.AuthProviderSyncHistory = Container.get(AuthProviderSyncHistoryRepository);
@ -224,7 +189,11 @@ export async function migrate() {
collections.Workflow = Container.get(WorkflowRepository);
collections.WorkflowStatistics = Container.get(WorkflowStatisticsRepository);
collections.WorkflowTagMapping = Container.get(WorkflowTagMappingRepository);
}
export async function migrate() {
(connection.options.migrations as Migration[]).forEach(wrapMigration);
await connection.runMigrations({ transaction: 'each' });
connectionState.migrated = true;
}

View file

@ -28,7 +28,7 @@ import type { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import type { WorkflowExecute } from 'n8n-core';
import type PCancelable from 'p-cancelable';
import type { FindOperator } from 'typeorm';
import type { FindOperator, Repository } from 'typeorm';
import type { ChildProcess } from 'child_process';
@ -83,7 +83,7 @@ export interface ICredentialsOverwrite {
}
/* eslint-disable @typescript-eslint/naming-convention */
export interface IDatabaseCollections {
export interface IDatabaseCollections extends Record<string, Repository<any>> {
AuthIdentity: AuthIdentityRepository;
AuthProviderSyncHistory: AuthProviderSyncHistoryRepository;
Credentials: CredentialsRepository;