Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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