From 3419d4b69f1e7d5bffad76454e9b036023f5e113 Mon Sep 17 00:00:00 2001 From: Ben Hesseldieck Date: Thu, 14 Jan 2021 17:26:56 +0100 Subject: [PATCH] :construction: add mySQL migration --- docker/images/n8n/README.md | 2 +- packages/cli/src/Db.ts | 27 +++---------------- .../src/databases/mysqldb/WebhookEntity.ts | 5 ++++ .../1588157391238-InitialMigration.ts | 2 +- .../migrations/1592447867632-WebhookModel.ts | 2 +- .../1594902918301-CreateIndexStoppedAt.ts | 2 +- .../migrations/1610640521099-AddWebhookId.ts | 23 ++++++++++++++++ .../src/databases/mysqldb/migrations/index.ts | 14 +++++++--- 8 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 packages/cli/src/databases/mysqldb/migrations/1610640521099-AddWebhookId.ts diff --git a/docker/images/n8n/README.md b/docker/images/n8n/README.md index 5a8198ed9d..08d8c45287 100644 --- a/docker/images/n8n/README.md +++ b/docker/images/n8n/README.md @@ -110,7 +110,7 @@ By default n8n uses SQLite to save credentials, past executions and workflows. n8n however also supports MongoDB and PostgresDB. To use them simply a few environment variables have to be set. -It is important to still persist the data in the `/root/.n8` folder. The reason +It is important to still persist the data in the `/root/.n8n` folder. The reason is that it contains n8n user data. That is the name of the webhook (in case) the n8n tunnel gets used and even more important the encryption key for the credentials. If none gets found n8n creates automatically one on diff --git a/packages/cli/src/Db.ts b/packages/cli/src/Db.ts index 4dce5490c4..4f689a348e 100644 --- a/packages/cli/src/Db.ts +++ b/packages/cli/src/Db.ts @@ -33,19 +33,8 @@ export let collections: IDatabaseCollections = { }; import postgresMigrations from './databases/postgresdb/migrations'; - -import { - CreateIndexStoppedAt1594910478695, - InitialMigration1587563438936, - WebhookModel1592679094242, -} from './databases/mongodb/migrations'; - -import { - CreateIndexStoppedAt1594902918301, - InitialMigration1588157391238, - WebhookModel1592447867632, -} from './databases/mysqldb/migrations'; - +import mongodbMigrations from './databases/mongodb/migrations'; +import mysqlMigrations from './databases/mysqldb/migrations'; import sqliteMigrations from './databases/sqlite/migrations'; import * as path from 'path'; @@ -67,11 +56,7 @@ export async function init(): Promise { entityPrefix, url: await GenericHelpers.getConfigValue('database.mongodb.connectionUrl') as string, useNewUrlParser: true, - migrations: [ - InitialMigration1587563438936, - WebhookModel1592679094242, - CreateIndexStoppedAt1594910478695, - ], + migrations: mongodbMigrations, migrationsRun: true, migrationsTableName: `${entityPrefix}migrations`, }; @@ -123,11 +108,7 @@ export async function init(): Promise { password: await GenericHelpers.getConfigValue('database.mysqldb.password') as string, port: await GenericHelpers.getConfigValue('database.mysqldb.port') as number, username: await GenericHelpers.getConfigValue('database.mysqldb.user') as string, - migrations: [ - InitialMigration1588157391238, - WebhookModel1592447867632, - CreateIndexStoppedAt1594902918301, - ], + migrations: mysqlMigrations, migrationsRun: true, migrationsTableName: `${entityPrefix}migrations`, }; diff --git a/packages/cli/src/databases/mysqldb/WebhookEntity.ts b/packages/cli/src/databases/mysqldb/WebhookEntity.ts index a78fd34ae9..04e2360981 100644 --- a/packages/cli/src/databases/mysqldb/WebhookEntity.ts +++ b/packages/cli/src/databases/mysqldb/WebhookEntity.ts @@ -2,6 +2,7 @@ import { Column, Entity, PrimaryColumn, + Index, } from 'typeorm'; import { @@ -9,6 +10,7 @@ import { } from '../../Interfaces'; @Entity() +@Index(["webhookId", "method"], { unique: true }) export class WebhookEntity implements IWebhookDb { @Column() @@ -22,4 +24,7 @@ export class WebhookEntity implements IWebhookDb { @Column() node: string; + + @Column({ nullable: true }) + webhookId: string; } diff --git a/packages/cli/src/databases/mysqldb/migrations/1588157391238-InitialMigration.ts b/packages/cli/src/databases/mysqldb/migrations/1588157391238-InitialMigration.ts index 1d1d4d8cc5..fa788c101d 100644 --- a/packages/cli/src/databases/mysqldb/migrations/1588157391238-InitialMigration.ts +++ b/packages/cli/src/databases/mysqldb/migrations/1588157391238-InitialMigration.ts @@ -2,7 +2,7 @@ import { MigrationInterface, QueryRunner } from 'typeorm'; import * as config from '../../../../config'; -export class InitialMigration1588157391238 implements MigrationInterface { +export default class InitialMigration1588157391238 implements MigrationInterface { name = 'InitialMigration1588157391238'; async up(queryRunner: QueryRunner): Promise { diff --git a/packages/cli/src/databases/mysqldb/migrations/1592447867632-WebhookModel.ts b/packages/cli/src/databases/mysqldb/migrations/1592447867632-WebhookModel.ts index 13969d72ef..4fc5a36566 100644 --- a/packages/cli/src/databases/mysqldb/migrations/1592447867632-WebhookModel.ts +++ b/packages/cli/src/databases/mysqldb/migrations/1592447867632-WebhookModel.ts @@ -5,7 +5,7 @@ import { import * as config from '../../../../config'; -export class WebhookModel1592447867632 implements MigrationInterface { +export default class WebhookModel1592447867632 implements MigrationInterface { name = 'WebhookModel1592447867632'; async up(queryRunner: QueryRunner): Promise { diff --git a/packages/cli/src/databases/mysqldb/migrations/1594902918301-CreateIndexStoppedAt.ts b/packages/cli/src/databases/mysqldb/migrations/1594902918301-CreateIndexStoppedAt.ts index 24556ea6bd..9990e0e64b 100644 --- a/packages/cli/src/databases/mysqldb/migrations/1594902918301-CreateIndexStoppedAt.ts +++ b/packages/cli/src/databases/mysqldb/migrations/1594902918301-CreateIndexStoppedAt.ts @@ -2,7 +2,7 @@ import { MigrationInterface, QueryRunner } from "typeorm"; import * as config from '../../../../config'; -export class CreateIndexStoppedAt1594902918301 implements MigrationInterface { +export default class CreateIndexStoppedAt1594902918301 implements MigrationInterface { name = 'CreateIndexStoppedAt1594902918301'; async up(queryRunner: QueryRunner): Promise { diff --git a/packages/cli/src/databases/mysqldb/migrations/1610640521099-AddWebhookId.ts b/packages/cli/src/databases/mysqldb/migrations/1610640521099-AddWebhookId.ts new file mode 100644 index 0000000000..5723b7c738 --- /dev/null +++ b/packages/cli/src/databases/mysqldb/migrations/1610640521099-AddWebhookId.ts @@ -0,0 +1,23 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +import * as config from '../../../../config'; + +export default class AddWebhookId1610640521099 implements MigrationInterface { + name = 'AddWebhookId1610640521099'; + + public async up(queryRunner: QueryRunner): Promise { + const tablePrefix = config.get('database.tablePrefix'); + + await queryRunner.query('ALTER TABLE `' + tablePrefix + 'webhook_entity` ADD `webhookId` varchar(255) NULL'); + await queryRunner.query('CREATE UNIQUE INDEX `IDX_' + tablePrefix + 'e1dddabccea3081178199d6004` ON `' + tablePrefix + 'webhook_entity` (`webhookId`, `method`)'); + } + + public async down(queryRunner: QueryRunner): Promise { + const tablePrefix = config.get('database.tablePrefix'); + + await queryRunner.query( + 'DROP INDEX `IDX_' + tablePrefix + 'e1dddabccea3081178199d6004` ON `' + tablePrefix + 'webhook_entity`', + ); + await queryRunner.query('ALTER TABLE `' + tablePrefix + 'webhook_entity` DROP COLUMN `webhookId`'); + } +} diff --git a/packages/cli/src/databases/mysqldb/migrations/index.ts b/packages/cli/src/databases/mysqldb/migrations/index.ts index 7c0cb217ef..baf42085de 100644 --- a/packages/cli/src/databases/mysqldb/migrations/index.ts +++ b/packages/cli/src/databases/mysqldb/migrations/index.ts @@ -1,3 +1,11 @@ -export * from './1588157391238-InitialMigration'; -export * from './1592447867632-WebhookModel'; -export * from './1594902918301-CreateIndexStoppedAt'; +import InitialMigration1588157391238 from './1588157391238-InitialMigration'; +import WebhookModel1592447867632 from './1592447867632-WebhookModel'; +import CreateIndexStoppedAt1594902918301 from './1594902918301-CreateIndexStoppedAt'; +import AddWebhookId1610640521099 from './1610640521099-AddWebhookId' + +export default [ + InitialMigration1588157391238, + WebhookModel1592447867632, + CreateIndexStoppedAt1594902918301, + AddWebhookId1610640521099, +]