Add command to remove duplicate messageChannelMessageAssociations (#9886)
In this PR: - create a command to remove duplicate (groupBy messageId, messageChannelId) within messageChannelmessageAssociaitions. THis is needed to apply a constraint already present on messageChannelmessageAssociaitions (to be executed before syncing metadata)
This commit is contained in:
@ -0,0 +1,96 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Command } from 'nest-commander';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import {
|
||||
ActiveWorkspacesCommandOptions,
|
||||
ActiveWorkspacesCommandRunner,
|
||||
} from 'src/database/commands/active-workspaces.command';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
|
||||
@Command({
|
||||
name: 'upgrade-0.41:remove-duplicate-mcmas',
|
||||
description: 'Remove duplicate mcmas.',
|
||||
})
|
||||
export class RemoveDuplicateMcmasCommand extends ActiveWorkspacesCommandRunner {
|
||||
protected readonly logger: Logger;
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Workspace, 'core')
|
||||
protected readonly workspaceRepository: Repository<Workspace>,
|
||||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
|
||||
) {
|
||||
super(workspaceRepository);
|
||||
this.logger = new Logger(this.constructor.name);
|
||||
}
|
||||
|
||||
async executeActiveWorkspacesCommand(
|
||||
_passedParam: string[],
|
||||
_options: ActiveWorkspacesCommandOptions,
|
||||
workspaceIds: string[],
|
||||
): Promise<void> {
|
||||
const { dryRun } = _options;
|
||||
|
||||
for (const workspaceId of workspaceIds) {
|
||||
try {
|
||||
await this.execute(workspaceId, dryRun);
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Error removing duplicate mcmas for workspace ${workspaceId}: ${error}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async execute(workspaceId: string, dryRun = false): Promise<void> {
|
||||
this.logger.log(`Removing duplicate mcmas for workspace: ${workspaceId}`);
|
||||
|
||||
const repository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace(
|
||||
workspaceId,
|
||||
'messageChannelMessageAssociation',
|
||||
);
|
||||
|
||||
const queryBuilder = repository.createQueryBuilder(
|
||||
'messageChannelMessageAssociation',
|
||||
);
|
||||
|
||||
const duplicateMcmas = await queryBuilder
|
||||
.select(`"messageChannelId"`)
|
||||
.addSelect(`"messageId"`)
|
||||
.where(`"deletedAt" IS NULL`)
|
||||
.groupBy(`"messageId"`)
|
||||
.addGroupBy(`"messageChannelId"`)
|
||||
.having(`COUNT("messageChannelId") > 1`)
|
||||
.getRawMany();
|
||||
|
||||
this.logger.log(`Found ${duplicateMcmas.length} duplicate mcmas`);
|
||||
|
||||
for (const duplicateMca of duplicateMcmas) {
|
||||
const mcmas = await repository.find({
|
||||
where: {
|
||||
messageId: duplicateMca.messageId,
|
||||
messageChannelId: duplicateMca.messageChannelId,
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(
|
||||
`Found ${mcmas.length} mcmas for message ${duplicateMca.messageId} and message channel ${duplicateMca.messageChannelId}`,
|
||||
);
|
||||
|
||||
const mcaIdsToDelete = mcmas.slice(1).map((mca) => mca.id);
|
||||
|
||||
if (mcaIdsToDelete.length > 0) {
|
||||
this.logger.log(`Deleting ${mcaIdsToDelete.length} mcas`);
|
||||
if (!dryRun) {
|
||||
await repository.delete(mcaIdsToDelete);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.twentyORMGlobalManager.destroyDataSourceForWorkspace(workspaceId);
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,7 @@ import { Repository } from 'typeorm';
|
||||
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
|
||||
import { BaseCommandOptions } from 'src/database/commands/base.command';
|
||||
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version/0-41/0-41-migrate-relations-to-field-metadata.command';
|
||||
import { RemoveDuplicateMcmasCommand } from 'src/database/commands/upgrade-version/0-41/0-41-remove-duplicate-mcmas';
|
||||
import { SeedWorkflowViewsCommand } from 'src/database/commands/upgrade-version/0-41/0-41-seed-workflow-views.command';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { SyncWorkspaceMetadataCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/sync-workspace-metadata.command';
|
||||
@ -21,6 +22,7 @@ export class UpgradeTo0_41Command extends ActiveWorkspacesCommandRunner {
|
||||
private readonly seedWorkflowViewsCommand: SeedWorkflowViewsCommand,
|
||||
private readonly syncWorkspaceMetadataCommand: SyncWorkspaceMetadataCommand,
|
||||
private readonly migrateRelationsToFieldMetadata: MigrateRelationsToFieldMetadataCommand,
|
||||
private readonly removeDuplicateMcmasCommand: RemoveDuplicateMcmasCommand,
|
||||
) {
|
||||
super(workspaceRepository);
|
||||
}
|
||||
@ -32,6 +34,12 @@ export class UpgradeTo0_41Command extends ActiveWorkspacesCommandRunner {
|
||||
): Promise<void> {
|
||||
this.logger.log('Running command to upgrade to 0.41');
|
||||
|
||||
await this.removeDuplicateMcmasCommand.executeActiveWorkspacesCommand(
|
||||
passedParam,
|
||||
options,
|
||||
workspaceIds,
|
||||
);
|
||||
|
||||
await this.syncWorkspaceMetadataCommand.executeActiveWorkspacesCommand(
|
||||
passedParam,
|
||||
{
|
||||
|
||||
@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version/0-41/0-41-migrate-relations-to-field-metadata.command';
|
||||
import { RemoveDuplicateMcmasCommand } from 'src/database/commands/upgrade-version/0-41/0-41-remove-duplicate-mcmas';
|
||||
import { SeedWorkflowViewsCommand } from 'src/database/commands/upgrade-version/0-41/0-41-seed-workflow-views.command';
|
||||
import { UpgradeTo0_41Command } from 'src/database/commands/upgrade-version/0-41/0-41-upgrade-version.command';
|
||||
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
|
||||
@ -32,6 +33,7 @@ import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/worksp
|
||||
SeedWorkflowViewsCommand,
|
||||
UpgradeTo0_41Command,
|
||||
MigrateRelationsToFieldMetadataCommand,
|
||||
RemoveDuplicateMcmasCommand,
|
||||
],
|
||||
})
|
||||
export class UpgradeTo0_41CommandModule {}
|
||||
|
||||
Reference in New Issue
Block a user