Skip to content

Commit

Permalink
fix(core): Ensure DB repositories are initialized before the DB migra…
Browse files Browse the repository at this point in the history
…tions are run (#6220)

also remove the need to re-open sqlite db connection
  • Loading branch information
netroy authored May 10, 2023
1 parent ed3bc15 commit 500c0eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 40 deletions.
45 changes: 7 additions & 38 deletions packages/cli/src/Db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 500c0eb

Please sign in to comment.