|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License.AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { MigrationInterface, QueryRunner } from "typeorm"; |
| 8 | + |
| 9 | +export class FixCollationMismatch1750344728596 implements MigrationInterface { |
| 10 | + public async up(queryRunner: QueryRunner): Promise<void> { |
| 11 | + const versionRow = await queryRunner.query(`SELECT VERSION() as version`); |
| 12 | + const version = versionRow[0].version; |
| 13 | + |
| 14 | + // MySQL docs: https://dev.mysql.com/blog-archive/mysql-8-0-collations-migrating-from-older-collations/ |
| 15 | + // 5.7 default: utf8mb4_general_ci |
| 16 | + // 8.0 default: utf8mb4_0900_ai_ci |
| 17 | + let collation: string; |
| 18 | + if (version.startsWith("5.7")) { |
| 19 | + collation = "utf8mb4_general_ci"; |
| 20 | + } else { |
| 21 | + // Assume MySQL 8.0+ for all other versions |
| 22 | + collation = "utf8mb4_0900_ai_ci"; |
| 23 | + } |
| 24 | + |
| 25 | + const dbNameRow = await queryRunner.query(`SELECT DATABASE() as dbName`); |
| 26 | + const dbName = dbNameRow[0].dbName; |
| 27 | + |
| 28 | + await queryRunner.query(`ALTER DATABASE ${dbName} CHARACTER SET utf8mb4 COLLATE ${collation};`); |
| 29 | + |
| 30 | + await queryRunner.query(`ALTER TABLE d_b_org_env_var DEFAULT CHARACTER SET utf8mb4 COLLATE ${collation};`); |
| 31 | + await queryRunner.query(`ALTER TABLE d_b_org_env_var CONVERT TO CHARACTER SET utf8mb4 COLLATE ${collation};`); |
| 32 | + |
| 33 | + await queryRunner.query( |
| 34 | + `ALTER TABLE d_b_workspace_instance_metrics DEFAULT CHARACTER SET utf8mb4 COLLATE ${collation};`, |
| 35 | + ); |
| 36 | + await queryRunner.query( |
| 37 | + `ALTER TABLE d_b_workspace_instance_metrics CONVERT TO CHARACTER SET utf8mb4 COLLATE ${collation};`, |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public async down(queryRunner: QueryRunner): Promise<void> { |
| 42 | + // This migration is not reversible as it changes the database collation. |
| 43 | + } |
| 44 | +} |
0 commit comments