mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 21:07:28 -08:00
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:
parent
ed3bc154b0
commit
500c0ebce3
|
@ -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> {
|
export async function init(testConnectionOptions?: ConnectionOptions): Promise<void> {
|
||||||
if (connectionState.connected) return;
|
if (connectionState.connected) return;
|
||||||
|
|
||||||
|
@ -160,7 +154,9 @@ export async function init(testConnectionOptions?: ConnectionOptions): Promise<v
|
||||||
migrationsRun: false,
|
migrationsRun: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
await openConnection(connectionOptions);
|
connection = new Connection(connectionOptions);
|
||||||
|
Container.set(Connection, connection);
|
||||||
|
await connection.initialize();
|
||||||
|
|
||||||
if (dbType === 'postgresdb') {
|
if (dbType === 'postgresdb') {
|
||||||
const schema = config.getEnv('database.postgresdb.schema');
|
const schema = config.getEnv('database.postgresdb.schema');
|
||||||
|
@ -173,37 +169,6 @@ export async function init(testConnectionOptions?: ConnectionOptions): Promise<v
|
||||||
}
|
}
|
||||||
|
|
||||||
connectionState.connected = true;
|
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.AuthIdentity = Container.get(AuthIdentityRepository);
|
||||||
collections.AuthProviderSyncHistory = Container.get(AuthProviderSyncHistoryRepository);
|
collections.AuthProviderSyncHistory = Container.get(AuthProviderSyncHistoryRepository);
|
||||||
|
@ -224,7 +189,11 @@ export async function migrate() {
|
||||||
collections.Workflow = Container.get(WorkflowRepository);
|
collections.Workflow = Container.get(WorkflowRepository);
|
||||||
collections.WorkflowStatistics = Container.get(WorkflowStatisticsRepository);
|
collections.WorkflowStatistics = Container.get(WorkflowStatisticsRepository);
|
||||||
collections.WorkflowTagMapping = Container.get(WorkflowTagMappingRepository);
|
collections.WorkflowTagMapping = Container.get(WorkflowTagMappingRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function migrate() {
|
||||||
|
(connection.options.migrations as Migration[]).forEach(wrapMigration);
|
||||||
|
await connection.runMigrations({ transaction: 'each' });
|
||||||
connectionState.migrated = true;
|
connectionState.migrated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ import type { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
||||||
import type { WorkflowExecute } from 'n8n-core';
|
import type { WorkflowExecute } from 'n8n-core';
|
||||||
|
|
||||||
import type PCancelable from 'p-cancelable';
|
import type PCancelable from 'p-cancelable';
|
||||||
import type { FindOperator } from 'typeorm';
|
import type { FindOperator, Repository } from 'typeorm';
|
||||||
|
|
||||||
import type { ChildProcess } from 'child_process';
|
import type { ChildProcess } from 'child_process';
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ export interface ICredentialsOverwrite {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/naming-convention */
|
/* eslint-disable @typescript-eslint/naming-convention */
|
||||||
export interface IDatabaseCollections {
|
export interface IDatabaseCollections extends Record<string, Repository<any>> {
|
||||||
AuthIdentity: AuthIdentityRepository;
|
AuthIdentity: AuthIdentityRepository;
|
||||||
AuthProviderSyncHistory: AuthProviderSyncHistoryRepository;
|
AuthProviderSyncHistory: AuthProviderSyncHistoryRepository;
|
||||||
Credentials: CredentialsRepository;
|
Credentials: CredentialsRepository;
|
||||||
|
|
Loading…
Reference in a new issue