diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/1-20/1-20-update-standard-index-view-names.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/1-20/1-20-update-standard-index-view-names.command.ts new file mode 100644 index 0000000000..6a36b27cb1 --- /dev/null +++ b/packages/twenty-server/src/database/commands/upgrade-version-command/1-20/1-20-update-standard-index-view-names.command.ts @@ -0,0 +1,88 @@ +import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; + +import { Command } from 'nest-commander'; +import { DataSource, Repository } from 'typeorm'; + +import { ActiveOrSuspendedWorkspacesMigrationCommandRunner } from 'src/database/commands/command-runners/active-or-suspended-workspaces-migration.command-runner'; +import { RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspaces-migration.command-runner'; +import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity'; +import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service'; +import { GlobalWorkspaceOrmManager } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-orm.manager'; + +@Command({ + name: 'upgrade:1-20:update-standard-index-view-names', + description: + 'Update standard index view names to use translatable template placeholders', +}) +export class UpdateStandardIndexViewNamesCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner { + constructor( + @InjectRepository(WorkspaceEntity) + protected readonly workspaceRepository: Repository, + @InjectDataSource() + private readonly coreDataSource: DataSource, + protected readonly twentyORMGlobalManager: GlobalWorkspaceOrmManager, + protected readonly dataSourceService: DataSourceService, + ) { + super(workspaceRepository, twentyORMGlobalManager, dataSourceService); + } + + override async runOnWorkspace({ + workspaceId, + options, + }: RunOnWorkspaceArgs): Promise { + const dryRun = options?.dryRun ?? false; + + if (dryRun) { + this.logger.log( + `[DRY RUN] Would update standard index view names for workspace ${workspaceId}. Skipping.`, + ); + + return; + } + + const queryRunner = this.coreDataSource.createQueryRunner(); + + await queryRunner.connect(); + await queryRunner.startTransaction(); + + try { + const result = await queryRunner.query( + ` + UPDATE core."view" + SET name = 'All {objectLabelPlural}' + WHERE "workspaceId" = $1 + AND "isCustom" = false + AND key = 'INDEX' + AND name LIKE 'All %' + AND name != 'All {objectLabelPlural}' + `, + [workspaceId], + ); + + const updateCount = result?.[1] ?? 0; + + if (updateCount === 0) { + this.logger.log( + `No standard index views needed updating for workspace ${workspaceId}`, + ); + } else { + this.logger.log( + `Updated ${updateCount} standard index view(s) for workspace ${workspaceId}`, + ); + } + + await queryRunner.commitTransaction(); + } catch (error) { + if (queryRunner.isTransactionActive) { + await queryRunner.rollbackTransaction(); + } + this.logger.error( + `Error updating standard index view names for workspace ${workspaceId}`, + error, + ); + throw error; + } finally { + await queryRunner.release(); + } + } +} diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/1-20/1-20-upgrade-version-command.module.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/1-20/1-20-upgrade-version-command.module.ts index 5c1d7d265a..c739fc1b1a 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version-command/1-20/1-20-upgrade-version-command.module.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version-command/1-20/1-20-upgrade-version-command.module.ts @@ -13,6 +13,7 @@ import { MakePermissionFlagUniversalIdentifierAndApplicationIdNotNullableMigrati import { MigrateMessagingInfrastructureToMetadataCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-migrate-messaging-infrastructure-to-metadata.command'; import { MigrateRichTextToTextCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-migrate-rich-text-to-text.command'; import { SeedCliApplicationRegistrationCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-seed-cli-application-registration.command'; +import { UpdateStandardIndexViewNamesCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-update-standard-index-view-names.command'; import { ApplicationRegistrationModule } from 'src/engine/core-modules/application/application-registration/application-registration.module'; import { ApplicationModule } from 'src/engine/core-modules/application/application.module'; import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module'; @@ -64,6 +65,7 @@ import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace SeedCliApplicationRegistrationCommand, MigrateRichTextToTextCommand, MigrateMessagingInfrastructureToMetadataCommand, + UpdateStandardIndexViewNamesCommand, ], exports: [ IdentifyPermissionFlagMetadataCommand, @@ -78,6 +80,7 @@ import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace SeedCliApplicationRegistrationCommand, MigrateRichTextToTextCommand, MigrateMessagingInfrastructureToMetadataCommand, + UpdateStandardIndexViewNamesCommand, ], }) export class V1_20_UpgradeVersionCommandModule {} diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/upgrade.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/upgrade.command.ts index 6f8121836f..a51d5c6d91 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version-command/upgrade.command.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version-command/upgrade.command.ts @@ -45,6 +45,7 @@ import { MakePermissionFlagUniversalIdentifierAndApplicationIdNotNullableMigrati import { MigrateMessagingInfrastructureToMetadataCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-migrate-messaging-infrastructure-to-metadata.command'; import { MigrateRichTextToTextCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-migrate-rich-text-to-text.command'; import { SeedCliApplicationRegistrationCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-seed-cli-application-registration.command'; +import { UpdateStandardIndexViewNamesCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-update-standard-index-view-names.command'; import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service'; import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity'; import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service'; @@ -107,6 +108,7 @@ export class UpgradeCommand extends UpgradeCommandRunner { protected readonly migrateRichTextToTextCommand: MigrateRichTextToTextCommand, protected readonly migrateMessagingInfrastructureToMetadataCommand: MigrateMessagingInfrastructureToMetadataCommand, protected readonly backfillSelectFieldOptionIdsCommand: BackfillSelectFieldOptionIdsCommand, + protected readonly updateStandardIndexViewNamesCommand: UpdateStandardIndexViewNamesCommand, ) { super( workspaceRepository, @@ -168,6 +170,7 @@ export class UpgradeCommand extends UpgradeCommandRunner { this.seedCliApplicationRegistrationCommand, this.migrateMessagingInfrastructureToMetadataCommand, this.backfillSelectFieldOptionIdsCommand, + this.updateStandardIndexViewNamesCommand, ]; this.allCommands = {