Skip to content

Refactor sync metadata to handle index creation if already exists #12757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Would still escape params here

`);
}
} catch (error) {
// Ignore error if index already exists
Expand Down
Loading