From 4dbf64b1a4d6dad7fb14f1bd0d92b5ab67a063f6 Mon Sep 17 00:00:00 2001 From: Omar Ajoue Date: Fri, 18 Jun 2021 23:57:58 +0200 Subject: [PATCH] :bug: Add Migration for MySQL to certify that we're using the correct collation (#1905) * Migration for MySQL to certify that we're using the correct collation * Update migration to check for collation existence before running * :zap: Fix format Co-authored-by: Jan Oberhauser --- .../1623936588000-CertifyCorrectCollation.ts | 44 +++++++++++++++++++ .../src/databases/mysqldb/migrations/index.ts | 2 + 2 files changed, 46 insertions(+) create mode 100644 packages/cli/src/databases/mysqldb/migrations/1623936588000-CertifyCorrectCollation.ts diff --git a/packages/cli/src/databases/mysqldb/migrations/1623936588000-CertifyCorrectCollation.ts b/packages/cli/src/databases/mysqldb/migrations/1623936588000-CertifyCorrectCollation.ts new file mode 100644 index 0000000000..079cd9cc70 --- /dev/null +++ b/packages/cli/src/databases/mysqldb/migrations/1623936588000-CertifyCorrectCollation.ts @@ -0,0 +1,44 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; +import config = require('../../../../config'); + +export class CertifyCorrectCollation1623936588000 implements MigrationInterface { + name = 'CertifyCorrectCollation1623936588000'; + + async up(queryRunner: QueryRunner): Promise { + const tablePrefix = config.get('database.tablePrefix'); + const databaseType = config.get('database.type'); + + if (databaseType === 'mariadb') { + // This applies to MySQL only. + return; + } + + const checkCollationExistence = await queryRunner.query(`show collation where collation like 'utf8mb4_0900_ai_ci';`); + let collation = 'utf8mb4_general_ci'; + if (checkCollationExistence.length > 0) { + collation = 'utf8mb4_0900_ai_ci'; + } + + const databaseName = config.get(`database.mysqldb.database`); + + await queryRunner.query(`ALTER DATABASE \`${databaseName}\` CHARACTER SET utf8mb4 COLLATE ${collation};`); + + for (const tableName of [ + 'credentials_entity', + 'execution_entity', + 'tag_entity', + 'webhook_entity', + 'workflow_entity', + 'workflows_tags', + ]) { + await queryRunner.query(`ALTER TABLE ${tablePrefix}${tableName} CONVERT TO CHARACTER SET utf8mb4 COLLATE ${collation};`); + } + } + + async down(queryRunner: QueryRunner): Promise { + // There is nothing to undo in this case as we already expect default collation to be utf8mb4 + // This migration exists simply to enforce that n8n will work with + // older mysql versions + } + +} diff --git a/packages/cli/src/databases/mysqldb/migrations/index.ts b/packages/cli/src/databases/mysqldb/migrations/index.ts index 943359a9f8..1054d68cfc 100644 --- a/packages/cli/src/databases/mysqldb/migrations/index.ts +++ b/packages/cli/src/databases/mysqldb/migrations/index.ts @@ -7,6 +7,7 @@ import { ChangeDataSize1615306975123 } from './1615306975123-ChangeDataSize'; import { ChangeCredentialDataSize1620729500000 } from './1620729500000-ChangeCredentialDataSize'; import { CreateTagEntity1617268711084 } from './1617268711084-CreateTagEntity'; import { UniqueWorkflowNames1620826335440 } from './1620826335440-UniqueWorkflowNames'; +import { CertifyCorrectCollation1623936588000 } from './1623936588000-CertifyCorrectCollation'; export const mysqlMigrations = [ InitialMigration1588157391238, @@ -18,4 +19,5 @@ export const mysqlMigrations = [ ChangeCredentialDataSize1620729500000, CreateTagEntity1617268711084, UniqueWorkflowNames1620826335440, + CertifyCorrectCollation1623936588000, ];