Skip to content

Commit

Permalink
Add backfill view groups command
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Nov 4, 2024
1 parent 4fc690b commit dbd4292
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<Workspace>,
@InjectRepository(FieldMetadataEntity, 'metadata')
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {
super(workspaceRepository);
}

async executeActiveWorkspacesCommand(
_passedParam: string[],
_options: ActiveWorkspacesCommandOptions,
workspaceIds: string[],
): Promise<void> {
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<ViewWorkspaceEntity>(
workspaceId,
'view',
);

const viewGroupRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewGroupWorkspaceEntity>(
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!`));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}
Expand Down Expand Up @@ -62,5 +64,11 @@ export class UpgradeTo0_32Command extends ActiveWorkspacesCommandRunner {
options,
workspaceIds,
);

await this.backfillViewGroupsCommand.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
);
}
}
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: [
Expand All @@ -26,6 +28,7 @@ import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands
providers: [
UpgradeTo0_32Command,
EnforceUniqueConstraintsCommand,
BackfillViewGroupsCommand,
CopyWebhookOperationIntoOperationsCommand,
SimplifySearchVectorExpressionCommand,
],
Expand Down

0 comments on commit dbd4292

Please sign in to comment.