From 39775067963ff33a47958d3c802cd38a65ba6a51 Mon Sep 17 00:00:00 2001 From: Guillim Date: Fri, 20 Jun 2025 14:20:16 +0200 Subject: [PATCH] temporary code (#12757) In order to put back relations on track, we need to allow the preexisting indexes to be created in indexmetadata, but also to avoid failure when the migration runner will try to re-apply a pre-existing index --- .../workspace-migration-runner.service.ts | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service.ts index 2cf25bc1a..581dbd222 100644 --- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service.ts +++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service.ts @@ -1,7 +1,7 @@ import { Injectable, Logger } from '@nestjs/common'; import { isDefined } from 'twenty-shared/utils'; -import { QueryRunner, Table, TableColumn, TableIndex } from 'typeorm'; +import { QueryRunner, Table, TableColumn } from 'typeorm'; import { IndexType } from 'src/engine/metadata-modules/index-metadata/index-metadata.entity'; import { @@ -208,18 +208,16 @@ export class WorkspaceMigrationRunnerService { const quotedColumns = index.columns.map((column) => `"${column}"`); await queryRunner.query(` - CREATE INDEX "${index.name}" ON "${schemaName}"."${tableName}" USING ${index.type} (${quotedColumns.join(', ')}) + CREATE INDEX IF NOT EXISTS "${index.name}" ON "${schemaName}"."${tableName}" USING ${index.type} (${quotedColumns.join(', ')}) `); } else { - await queryRunner.createIndex( - `${schemaName}.${tableName}`, - new TableIndex({ - name: index.name, - columnNames: index.columns, - isUnique: index.isUnique, - where: index.where ?? undefined, - }), - ); + const quotedColumns = index.columns.map((column) => `"${column}"`); + const isUnique = index.isUnique ? 'UNIQUE' : ''; + const whereClause = index.where ? `WHERE ${index.where}` : ''; + + await queryRunner.query(` + CREATE ${isUnique} INDEX IF NOT EXISTS "${index.name}" ON "${schemaName}"."${tableName}" (${quotedColumns.join(', ')}) ${whereClause} + `); } } catch (error) { // Ignore error if index already exists