From 575d9fb758d57fb002ea1e5090df4be5efd458e1 Mon Sep 17 00:00:00 2001 From: Ben Hesseldieck Date: Thu, 14 Jan 2021 16:18:43 +0100 Subject: [PATCH] :construction: :elephant: postgres migration --- packages/cli/src/Db.ts | 24 ++--------- .../src/databases/postgresdb/WebhookEntity.ts | 5 +++ .../1587669153312-InitialMigration.ts | 2 +- .../migrations/1589476000887-WebhookModel.ts | 2 +- .../1594828256133-CreateIndexStoppedAt.ts | 2 +- .../migrations/1610632462428-AddWebhookId.ts | 33 ++++++++++++++ .../databases/postgresdb/migrations/index.ts | 13 ++++-- .../migrations/1610570498276-AddWebhookId.ts | 43 +++++++++++-------- 8 files changed, 81 insertions(+), 43 deletions(-) create mode 100644 packages/cli/src/databases/postgresdb/migrations/1610632462428-AddWebhookId.ts diff --git a/packages/cli/src/Db.ts b/packages/cli/src/Db.ts index f4522e52ed..4dce5490c4 100644 --- a/packages/cli/src/Db.ts +++ b/packages/cli/src/Db.ts @@ -32,11 +32,7 @@ export let collections: IDatabaseCollections = { Webhook: null, }; -import { - CreateIndexStoppedAt1594828256133, - InitialMigration1587669153312, - WebhookModel1589476000887, -} from './databases/postgresdb/migrations'; +import postgresMigrations from './databases/postgresdb/migrations'; import { CreateIndexStoppedAt1594910478695, @@ -50,11 +46,7 @@ import { WebhookModel1592447867632, } from './databases/mysqldb/migrations'; -import { - CreateIndexStoppedAt1594825041918, - InitialMigration1588102412422, - WebhookModel1592445003908, -} from './databases/sqlite/migrations'; +import sqliteMigrations from './databases/sqlite/migrations'; import * as path from 'path'; @@ -112,11 +104,7 @@ export async function init(): Promise { port: await GenericHelpers.getConfigValue('database.postgresdb.port') as number, username: await GenericHelpers.getConfigValue('database.postgresdb.user') as string, schema: config.get('database.postgresdb.schema'), - migrations: [ - InitialMigration1587669153312, - WebhookModel1589476000887, - CreateIndexStoppedAt1594828256133, - ], + migrations: postgresMigrations, migrationsRun: true, migrationsTableName: `${entityPrefix}migrations`, ssl, @@ -151,11 +139,7 @@ export async function init(): Promise { type: 'sqlite', database: path.join(n8nFolder, 'database.sqlite'), entityPrefix, - migrations: [ - InitialMigration1588102412422, - WebhookModel1592445003908, - CreateIndexStoppedAt1594825041918, - ], + migrations: sqliteMigrations, migrationsRun: true, migrationsTableName: `${entityPrefix}migrations`, }; diff --git a/packages/cli/src/databases/postgresdb/WebhookEntity.ts b/packages/cli/src/databases/postgresdb/WebhookEntity.ts index 6e511cde74..db08a49307 100644 --- a/packages/cli/src/databases/postgresdb/WebhookEntity.ts +++ b/packages/cli/src/databases/postgresdb/WebhookEntity.ts @@ -2,6 +2,7 @@ import { Column, Entity, PrimaryColumn, + Index, } from 'typeorm'; import { @@ -9,6 +10,7 @@ import { } from '../../'; @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/postgresdb/migrations/1587669153312-InitialMigration.ts b/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts index eace7a92fb..911f236d23 100644 --- a/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts +++ b/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts @@ -3,7 +3,7 @@ import { import * as config from '../../../../config'; -export class InitialMigration1587669153312 implements MigrationInterface { +export default class InitialMigration1587669153312 implements MigrationInterface { name = 'InitialMigration1587669153312'; async up(queryRunner: QueryRunner): Promise { diff --git a/packages/cli/src/databases/postgresdb/migrations/1589476000887-WebhookModel.ts b/packages/cli/src/databases/postgresdb/migrations/1589476000887-WebhookModel.ts index 0c195f9d54..e4ae54ae09 100644 --- a/packages/cli/src/databases/postgresdb/migrations/1589476000887-WebhookModel.ts +++ b/packages/cli/src/databases/postgresdb/migrations/1589476000887-WebhookModel.ts @@ -5,7 +5,7 @@ import { import * as config from '../../../../config'; -export class WebhookModel1589476000887 implements MigrationInterface { +export default class WebhookModel1589476000887 implements MigrationInterface { name = 'WebhookModel1589476000887'; async up(queryRunner: QueryRunner): Promise { diff --git a/packages/cli/src/databases/postgresdb/migrations/1594828256133-CreateIndexStoppedAt.ts b/packages/cli/src/databases/postgresdb/migrations/1594828256133-CreateIndexStoppedAt.ts index 664834082e..c0c99d313a 100644 --- a/packages/cli/src/databases/postgresdb/migrations/1594828256133-CreateIndexStoppedAt.ts +++ b/packages/cli/src/databases/postgresdb/migrations/1594828256133-CreateIndexStoppedAt.ts @@ -2,7 +2,7 @@ import { MigrationInterface, QueryRunner } from "typeorm"; import * as config from '../../../../config'; -export class CreateIndexStoppedAt1594828256133 implements MigrationInterface { +export default class CreateIndexStoppedAt1594828256133 implements MigrationInterface { name = 'CreateIndexStoppedAt1594828256133'; async up(queryRunner: QueryRunner): Promise { diff --git a/packages/cli/src/databases/postgresdb/migrations/1610632462428-AddWebhookId.ts b/packages/cli/src/databases/postgresdb/migrations/1610632462428-AddWebhookId.ts new file mode 100644 index 0000000000..2700096b88 --- /dev/null +++ b/packages/cli/src/databases/postgresdb/migrations/1610632462428-AddWebhookId.ts @@ -0,0 +1,33 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +import * as config from '../../../../config'; + +export default class AddWebhookId1610632462428 implements MigrationInterface { + name = 'AddWebhookId1610632462428'; + + public async up(queryRunner: QueryRunner): Promise { + let tablePrefix = config.get('database.tablePrefix'); + const tablePrefixPure = tablePrefix; + const schema = config.get('database.postgresdb.schema'); + if (schema) { + tablePrefix = schema + '.' + tablePrefix; + } + + await queryRunner.query(`ALTER TABLE ${tablePrefix}webhook_entity ADD "webhookId" character varying`); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_${tablePrefixPure}5c698bcd4092bc271cabdf7814" ON ${tablePrefix}webhook_entity ("webhookId", "method") `, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + let tablePrefix = config.get('database.tablePrefix'); + const tablePrefixPure = tablePrefix; + const schema = config.get('database.postgresdb.schema'); + if (schema) { + tablePrefix = schema + '.' + tablePrefix; + } + + await queryRunner.query(`DROP INDEX IDX_${tablePrefixPure}5c698bcd4092bc271cabdf7814`); + await queryRunner.query(`ALTER TABLE ${tablePrefix}webhook_entity DROP COLUMN "webhookId"`); + } +} diff --git a/packages/cli/src/databases/postgresdb/migrations/index.ts b/packages/cli/src/databases/postgresdb/migrations/index.ts index 3b10537067..5ac9425e53 100644 --- a/packages/cli/src/databases/postgresdb/migrations/index.ts +++ b/packages/cli/src/databases/postgresdb/migrations/index.ts @@ -1,4 +1,11 @@ -export * from './1587669153312-InitialMigration'; -export * from './1589476000887-WebhookModel'; -export * from './1594828256133-CreateIndexStoppedAt'; +import InitialMigration1587669153312 from './1587669153312-InitialMigration'; +import WebhookModel1589476000887 from './1589476000887-WebhookModel'; +import CreateIndexStoppedAt1594828256133 from './1594828256133-CreateIndexStoppedAt'; +import AddWebhookId1610632462428 from './1610632462428-AddWebhookId' +export default [ + InitialMigration1587669153312, + WebhookModel1589476000887, + CreateIndexStoppedAt1594828256133, + AddWebhookId1610632462428, +] diff --git a/packages/cli/src/databases/sqlite/migrations/1610570498276-AddWebhookId.ts b/packages/cli/src/databases/sqlite/migrations/1610570498276-AddWebhookId.ts index 6879eaf9b1..693cdeee4f 100644 --- a/packages/cli/src/databases/sqlite/migrations/1610570498276-AddWebhookId.ts +++ b/packages/cli/src/databases/sqlite/migrations/1610570498276-AddWebhookId.ts @@ -1,28 +1,37 @@ -import {MigrationInterface, QueryRunner} from "typeorm"; +import { MigrationInterface, QueryRunner } from 'typeorm'; import * as config from '../../../../config'; export default class AddWebhookId1610570498276 implements MigrationInterface { - name = 'AddWebhookId1610570498276' + name = 'AddWebhookId1610570498276'; - public async up(queryRunner: QueryRunner): Promise { + public async up(queryRunner: QueryRunner): Promise { const tablePrefix = config.get('database.tablePrefix'); - await queryRunner.query(`CREATE TABLE "temporary_webhook_entity" ("workflowId" integer NOT NULL, "webhookPath" varchar NOT NULL, "method" varchar NOT NULL, "node" varchar NOT NULL, "webhookId" varchar, PRIMARY KEY ("webhookPath", "method"))`); - await queryRunner.query(`INSERT INTO "temporary_webhook_entity"("workflowId", "webhookPath", "method", "node") SELECT "workflowId", "webhookPath", "method", "node" FROM "${tablePrefix}webhook_entity"`); - await queryRunner.query(`DROP TABLE "${tablePrefix}webhook_entity"`); - await queryRunner.query(`ALTER TABLE "temporary_webhook_entity" RENAME TO "${tablePrefix}webhook_entity"`); - await queryRunner.query(`CREATE UNIQUE INDEX "IDX_${tablePrefix}e1dddabccea3081178199d6004" ON "${tablePrefix}webhook_entity" ("webhookId", "method") `); - } + await queryRunner.query( + `CREATE TABLE "temporary_webhook_entity" ("workflowId" integer NOT NULL, "webhookPath" varchar NOT NULL, "method" varchar NOT NULL, "node" varchar NOT NULL, "webhookId" varchar, PRIMARY KEY ("webhookPath", "method"))`, + ); + await queryRunner.query( + `INSERT INTO "temporary_webhook_entity"("workflowId", "webhookPath", "method", "node") SELECT "workflowId", "webhookPath", "method", "node" FROM "${tablePrefix}webhook_entity"`, + ); + await queryRunner.query(`DROP TABLE "${tablePrefix}webhook_entity"`); + await queryRunner.query(`ALTER TABLE "temporary_webhook_entity" RENAME TO "${tablePrefix}webhook_entity"`); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_${tablePrefix}e1dddabccea3081178199d6004" ON "${tablePrefix}webhook_entity" ("webhookId", "method") `, + ); + } - public async down(queryRunner: QueryRunner): Promise { + public async down(queryRunner: QueryRunner): Promise { const tablePrefix = config.get('database.tablePrefix'); - await queryRunner.query(`DROP INDEX "IDX_${tablePrefix}e1dddabccea3081178199d6004"`); - await queryRunner.query(`ALTER TABLE "${tablePrefix}webhook_entity" RENAME TO "temporary_webhook_entity"`); - await queryRunner.query(`CREATE TABLE "${tablePrefix}webhook_entity" ("workflowId" integer NOT NULL, "webhookPath" varchar NOT NULL, "method" varchar NOT NULL, "node" varchar NOT NULL, PRIMARY KEY ("webhookPath", "method"))`); - await queryRunner.query(`INSERT INTO "${tablePrefix}webhook_entity"("workflowId", "webhookPath", "method", "node") SELECT "workflowId", "webhookPath", "method", "node" FROM "temporary_webhook_entity"`); - await queryRunner.query(`DROP TABLE "temporary_webhook_entity"`); - } - + await queryRunner.query(`DROP INDEX "IDX_${tablePrefix}e1dddabccea3081178199d6004"`); + await queryRunner.query(`ALTER TABLE "${tablePrefix}webhook_entity" RENAME TO "temporary_webhook_entity"`); + await queryRunner.query( + `CREATE TABLE "${tablePrefix}webhook_entity" ("workflowId" integer NOT NULL, "webhookPath" varchar NOT NULL, "method" varchar NOT NULL, "node" varchar NOT NULL, PRIMARY KEY ("webhookPath", "method"))`, + ); + await queryRunner.query( + `INSERT INTO "${tablePrefix}webhook_entity"("workflowId", "webhookPath", "method", "node") SELECT "workflowId", "webhookPath", "method", "node" FROM "temporary_webhook_entity"`, + ); + await queryRunner.query(`DROP TABLE "temporary_webhook_entity"`); + } }