From dbd4292b6a1d24134e58328a0edac3b482664096 Mon Sep 17 00:00:00 2001 From: Charles Bochet Date: Mon, 4 Nov 2024 23:59:23 +0100 Subject: [PATCH] Add backfill view groups command --- .../0-32/0-32-backfill-view-groups.command.ts | 127 ++++++++++++++++++ .../0-32/0-32-upgrade-version.command.ts | 8 ++ .../0-32/0-32-upgrade-version.module.ts | 5 +- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-backfill-view-groups.command.ts diff --git a/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-backfill-view-groups.command.ts b/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-backfill-view-groups.command.ts new file mode 100644 index 000000000000..ba7c406a483a --- /dev/null +++ b/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-backfill-view-groups.command.ts @@ -0,0 +1,127 @@ +import { InjectRepository } from '@nestjs/typeorm'; + +import chalk from 'chalk'; +import { Command } from 'nest-commander'; +import { In, Repository } from 'typeorm'; + +import { + ActiveWorkspacesCommandOptions, + ActiveWorkspacesCommandRunner, +} from 'src/database/commands/active-workspaces.command'; +import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; +import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; +import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; +import { ViewGroupWorkspaceEntity } from 'src/modules/view/standard-objects/view-group.workspace-entity'; +import { ViewWorkspaceEntity } from 'src/modules/view/standard-objects/view.workspace-entity'; + +@Command({ + name: 'upgrade-0.32:backfill-view-groups', + description: 'Backfill view groups', +}) +export class BackfillViewGroupsCommand extends ActiveWorkspacesCommandRunner { + constructor( + @InjectRepository(Workspace, 'core') + protected readonly workspaceRepository: Repository, + @InjectRepository(FieldMetadataEntity, 'metadata') + private readonly fieldMetadataRepository: Repository, + private readonly twentyORMGlobalManager: TwentyORMGlobalManager, + ) { + super(workspaceRepository); + } + + async executeActiveWorkspacesCommand( + _passedParam: string[], + _options: ActiveWorkspacesCommandOptions, + workspaceIds: string[], + ): Promise { + this.logger.log('Running command to fix backfill view groups'); + + for (const workspaceId of workspaceIds) { + this.logger.log(`Running command for workspace ${workspaceId}`); + + try { + const viewRepository = + await this.twentyORMGlobalManager.getRepositoryForWorkspace( + workspaceId, + 'view', + ); + + const viewGroupRepository = + await this.twentyORMGlobalManager.getRepositoryForWorkspace( + workspaceId, + 'viewGroup', + ); + + const kanbanViews = await viewRepository.find({ + where: { + type: 'kanban', + }, + }); + + const kanbanFieldMetadataIds = kanbanViews.map( + (view) => view.kanbanFieldMetadataId, + ); + + const kanbanFieldMetadataItems = + await this.fieldMetadataRepository.find({ + where: { + id: In(kanbanFieldMetadataIds), + }, + }); + + for (const kanbanView of kanbanViews) { + const kanbanFieldMetadataItem = kanbanFieldMetadataItems.find( + (item) => item.id === kanbanView.kanbanFieldMetadataId, + ); + + if (!kanbanFieldMetadataItem) { + this.logger.log( + chalk.red( + `Kanban field metadata with id ${kanbanView.kanbanFieldMetadataId} not found`, + ), + ); + continue; + } + + for (const option of kanbanFieldMetadataItem.options) { + const viewGroup = await viewGroupRepository.findOne({ + where: { + fieldMetadataId: kanbanFieldMetadataItem.id, + fieldValue: option.value, + viewId: kanbanView.id, + }, + }); + + if (viewGroup) { + this.logger.log( + chalk.red(`View group with id ${option.value} already exists`), + ); + continue; + } + + await viewGroupRepository.save({ + fieldMetadataId: kanbanFieldMetadataItem.id, + fieldValue: option.value, + isVisible: true, + viewId: kanbanView.id, + position: option.position, + }); + } + } + } catch (error) { + this.logger.log( + chalk.red( + `Running command on workspace ${workspaceId} failed with error: ${error}`, + ), + ); + continue; + } finally { + this.logger.log( + chalk.green(`Finished running command for workspace ${workspaceId}.`), + ); + } + + this.logger.log(chalk.green(`Command completed!`)); + } + } +} diff --git a/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-upgrade-version.command.ts b/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-upgrade-version.command.ts index fcfaba9be9a4..bc796642ca6e 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-upgrade-version.command.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-upgrade-version.command.ts @@ -4,6 +4,7 @@ import { Command } from 'nest-commander'; import { Repository } from 'typeorm'; import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command'; +import { BackfillViewGroupsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-backfill-view-groups.command'; import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-copy-webhook-operation-into-operations-command'; import { SimplifySearchVectorExpressionCommand } from 'src/database/commands/upgrade-version/0-32/0-32-simplify-search-vector-expression'; import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; @@ -27,6 +28,7 @@ export class UpgradeTo0_32Command extends ActiveWorkspacesCommandRunner { private readonly enforceUniqueConstraintsCommand: EnforceUniqueConstraintsCommand, private readonly simplifySearchVectorExpressionCommand: SimplifySearchVectorExpressionCommand, private readonly copyWebhookOperationIntoOperationsCommand: CopyWebhookOperationIntoOperationsCommand, + private readonly backfillViewGroupsCommand: BackfillViewGroupsCommand, ) { super(workspaceRepository); } @@ -62,5 +64,11 @@ export class UpgradeTo0_32Command extends ActiveWorkspacesCommandRunner { options, workspaceIds, ); + + await this.backfillViewGroupsCommand.executeActiveWorkspacesCommand( + passedParam, + options, + workspaceIds, + ); } } diff --git a/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-upgrade-version.module.ts b/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-upgrade-version.module.ts index 0d0f59548f03..f7f1a30cf324 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-upgrade-version.module.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version/0-32/0-32-upgrade-version.module.ts @@ -1,6 +1,7 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; +import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-copy-webhook-operation-into-operations-command'; import { EnforceUniqueConstraintsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-enforce-unique-constraints.command'; import { SimplifySearchVectorExpressionCommand } from 'src/database/commands/upgrade-version/0-32/0-32-simplify-search-vector-expression'; import { UpgradeTo0_32Command } from 'src/database/commands/upgrade-version/0-32/0-32-upgrade-version.command'; @@ -10,7 +11,8 @@ import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadat import { SearchModule } from 'src/engine/metadata-modules/search/search.module'; import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.module'; import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module'; -import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-copy-webhook-operation-into-operations-command'; + +import { BackfillViewGroupsCommand } from './0-32-backfill-view-groups.command'; @Module({ imports: [ @@ -26,6 +28,7 @@ import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands providers: [ UpgradeTo0_32Command, EnforceUniqueConstraintsCommand, + BackfillViewGroupsCommand, CopyWebhookOperationIntoOperationsCommand, SimplifySearchVectorExpressionCommand, ],