Skip to content

Commit

Permalink
refactor(core): Delete boilerplate code across migrations (no-changelog)
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Jan 30, 2023
1 parent eda3b8a commit e5118c7
Show file tree
Hide file tree
Showing 102 changed files with 625 additions and 1,321 deletions.
1 change: 0 additions & 1 deletion packages/cli/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = {
ignorePatterns: [
'jest.config.js',
// TODO: Remove these
'src/databases/migrations/**',
'src/databases/ormconfig.ts',
],

Expand Down
1 change: 1 addition & 0 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
testEnvironmentOptions: {
url: 'http://localhost/',
},
testTimeout: 10_000,
globalSetup: '<rootDir>/test/setup.ts',
globalTeardown: '<rootDir>/test/teardown.ts',
setupFilesAfterEnv: ['<rootDir>/test/setup-mocks.ts', '<rootDir>/test/extend-expect.ts'],
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/src/Db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
getPostgresConnectionOptions,
getSqliteConnectionOptions,
} from '@db/config';
import type { MigrationClass } from '@db/types';
import { wrapMigration } from '@db/utils/migrationHelpers';

export let isInitialized = false;
export const collections = {} as IDatabaseCollections;
Expand Down Expand Up @@ -121,7 +123,7 @@ export async function init(
synchronize: false,
logging: loggingOption,
maxQueryExecutionTime,
migrationsTransactionMode: 'each',
migrationsRun: false,
});

connection = new Connection(connectionOptions);
Expand All @@ -137,15 +139,17 @@ export async function init(
await connection.query(`SET search_path TO ${searchPath.join(',')};`);
}

(connectionOptions.migrations as MigrationClass[]).forEach((m) => wrapMigration(m));

if (!testConnectionOptions && dbType === '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 entityPrefix = config.getEnv('database.tablePrefix');
const tablePrefix = config.getEnv('database.tablePrefix');
migrations = await connection.query(
`SELECT id FROM ${entityPrefix}migrations where name = "MakeStoppedAtNullable1607431743769"`,
`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.
Expand Down
9 changes: 4 additions & 5 deletions packages/cli/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ import type {
WorkflowExecuteMode,
} from 'n8n-workflow';

import type { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';

import type { WorkflowExecute } from 'n8n-core';

import type PCancelable from 'p-cancelable';
import type { FindOperator, Repository } from 'typeorm';

import type { ChildProcess } from 'child_process';

import type { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import type { DatabaseType } from '@db/types';
import type { AuthIdentity, AuthProviderType } from '@db/entities/AuthIdentity';
import type { AuthProviderSyncHistory } from '@db/entities/AuthProviderSyncHistory';
import type { InstalledNodes } from '@db/entities/InstalledNodes';
Expand Down Expand Up @@ -141,9 +139,10 @@ export type ICredentialsDecryptedDb = ICredentialsBase & ICredentialsDecrypted;

export type ICredentialsDecryptedResponse = ICredentialsDecryptedDb;

export type DatabaseType = 'mariadb' | 'postgresdb' | 'mysqldb' | 'sqlite';
export type SaveExecutionDataType = 'all' | 'none';

export type { DatabaseType } from '@db/types';

export interface IExecutionBase {
id?: string;
mode: WorkflowExecuteMode;
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/databases/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const getDBConnectionOptions = (dbType: DatabaseType) => {
return {
entityPrefix,
entities: Object.values(entities),
migrationsRun: false,
migrationsTableName: `${entityPrefix}migrations`,
cli: { entitiesDir, migrationsDir },
...connectionDetails,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,45 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

import config from '@/config';
import type { MigrationContext, MigrationInterface } from '@db/types';

export class InitialMigration1588157391238 implements MigrationInterface {
name = 'InitialMigration1588157391238';

async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async up({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'CREATE TABLE IF NOT EXISTS `' +
tablePrefix +
'credentials_entity` (`id` int NOT NULL AUTO_INCREMENT, `name` varchar(128) NOT NULL, `data` text NOT NULL, `type` varchar(32) NOT NULL, `nodesAccess` json NOT NULL, `createdAt` datetime NOT NULL, `updatedAt` datetime NOT NULL, INDEX `IDX_' +
tablePrefix +
'07fde106c0b471d8cc80a64fc8` (`type`), PRIMARY KEY (`id`)) ENGINE=InnoDB',
undefined,
);
await queryRunner.query(
'CREATE TABLE IF NOT EXISTS `' +
tablePrefix +
'execution_entity` (`id` int NOT NULL AUTO_INCREMENT, `data` text NOT NULL, `finished` tinyint NOT NULL, `mode` varchar(255) NOT NULL, `retryOf` varchar(255) NULL, `retrySuccessId` varchar(255) NULL, `startedAt` datetime NOT NULL, `stoppedAt` datetime NOT NULL, `workflowData` json NOT NULL, `workflowId` varchar(255) NULL, INDEX `IDX_' +
tablePrefix +
'c4d999a5e90784e8caccf5589d` (`workflowId`), PRIMARY KEY (`id`)) ENGINE=InnoDB',
undefined,
);
await queryRunner.query(
'CREATE TABLE IF NOT EXISTS`' +
tablePrefix +
'workflow_entity` (`id` int NOT NULL AUTO_INCREMENT, `name` varchar(128) NOT NULL, `active` tinyint NOT NULL, `nodes` json NOT NULL, `connections` json NOT NULL, `createdAt` datetime NOT NULL, `updatedAt` datetime NOT NULL, `settings` json NULL, `staticData` json NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB',
undefined,
);
}

async down(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

await queryRunner.query('DROP TABLE `' + tablePrefix + 'workflow_entity`', undefined);
async down({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query('DROP TABLE `' + tablePrefix + 'workflow_entity`');
await queryRunner.query(
'DROP INDEX `IDX_' +
tablePrefix +
'c4d999a5e90784e8caccf5589d` ON `' +
tablePrefix +
'execution_entity`',
undefined,
);
await queryRunner.query('DROP TABLE `' + tablePrefix + 'execution_entity`', undefined);
await queryRunner.query('DROP TABLE `' + tablePrefix + 'execution_entity`');
await queryRunner.query(
'DROP INDEX `IDX_' +
tablePrefix +
'07fde106c0b471d8cc80a64fc8` ON `' +
tablePrefix +
'credentials_entity`',
undefined,
);
await queryRunner.query('DROP TABLE `' + tablePrefix + 'credentials_entity`', undefined);
await queryRunner.query('DROP TABLE `' + tablePrefix + 'credentials_entity`');
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

import config from '@/config';
import type { MigrationContext, MigrationInterface } from '@db/types';

export class WebhookModel1592447867632 implements MigrationInterface {
name = 'WebhookModel1592447867632';

async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async up({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
`CREATE TABLE IF NOT EXISTS ${tablePrefix}webhook_entity (workflowId int NOT NULL, webhookPath varchar(255) NOT NULL, method varchar(255) NOT NULL, node varchar(255) NOT NULL, PRIMARY KEY (webhookPath, method)) ENGINE=InnoDB`,
);
}

async down(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');
async down({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(`DROP TABLE ${tablePrefix}webhook_entity`);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

import config from '@/config';
import type { MigrationContext, MigrationInterface } from '@db/types';

export class CreateIndexStoppedAt1594902918301 implements MigrationInterface {
name = 'CreateIndexStoppedAt1594902918301';

async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async up({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'CREATE INDEX `IDX_' +
tablePrefix +
Expand All @@ -17,9 +11,7 @@ export class CreateIndexStoppedAt1594902918301 implements MigrationInterface {
);
}

async down(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async down({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'DROP INDEX `IDX_' +
tablePrefix +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

import config from '@/config';
import type { MigrationContext, MigrationInterface } from '@db/types';

export class MakeStoppedAtNullable1607431743767 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');
async up({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'ALTER TABLE `' + tablePrefix + 'execution_entity` MODIFY `stoppedAt` datetime',
undefined,
);
}

async down(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');
async down({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'ALTER TABLE `' + tablePrefix + 'execution_entity` MODIFY `stoppedAt` datetime NOT NULL',
undefined,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import config from '@/config';
import type { MigrationContext, MigrationInterface } from '@db/types';

export class AddWebhookId1611149998770 implements MigrationInterface {
name = 'AddWebhookId1611149998770';

async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async up({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'ALTER TABLE `' + tablePrefix + 'webhook_entity` ADD `webhookId` varchar(255) NULL',
);
Expand All @@ -22,9 +17,7 @@ export class AddWebhookId1611149998770 implements MigrationInterface {
);
}

async down(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async down({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'DROP INDEX `IDX_' +
tablePrefix +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import config from '@/config';
import type { MigrationContext, MigrationInterface } from '@db/types';

export class ChangeDataSize1615306975123 implements MigrationInterface {
name = 'ChangeDataSize1615306975123';

async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async up({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'ALTER TABLE `' + tablePrefix + 'execution_entity` MODIFY COLUMN `data` MEDIUMTEXT NOT NULL',
);
}

async down(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async down({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'ALTER TABLE `' + tablePrefix + 'execution_entity` MODIFY COLUMN `data` TEXT NOT NULL',
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import config from '@/config';
import type { MigrationContext, MigrationInterface } from '@db/types';

export class CreateTagEntity1617268711084 implements MigrationInterface {
name = 'CreateTagEntity1617268711084';

async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async up({ queryRunner, tablePrefix }: MigrationContext) {
// create tags table + relationship with workflow entity

await queryRunner.query(
Expand Down Expand Up @@ -78,9 +73,7 @@ export class CreateTagEntity1617268711084 implements MigrationInterface {
);
}

async down(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async down({ queryRunner, tablePrefix }: MigrationContext) {
// `createdAt` and `updatedAt`

await queryRunner.query(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import config from '@/config';
import type { MigrationContext, MigrationInterface } from '@db/types';

export class ChangeCredentialDataSize1620729500000 implements MigrationInterface {
name = 'ChangeCredentialDataSize1620729500000';

async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async up({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'ALTER TABLE `' +
tablePrefix +
'credentials_entity` MODIFY COLUMN `type` varchar(128) NOT NULL',
);
}

async down(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.getEnv('database.tablePrefix');

async down({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.query(
'ALTER TABLE `' +
tablePrefix +
Expand Down
Loading

0 comments on commit e5118c7

Please sign in to comment.