Enhance ActiveWorkspaceCommand to order, limit and startFrom workspaceId (#10378)

I have tested it locally with all combinaisons of params
This commit is contained in:
Charles Bochet
2025-02-21 10:37:04 +01:00
committed by GitHub
parent 4720afe3fb
commit 311fc402d5
2 changed files with 53 additions and 14 deletions

View File

@ -1,7 +1,7 @@
import chalk from 'chalk';
import { Option } from 'nest-commander';
import { WorkspaceActivationStatus } from 'twenty-shared';
import { In, Repository } from 'typeorm';
import { In, MoreThanOrEqual, Repository } from 'typeorm';
import {
BaseCommandOptions,
@ -10,19 +10,55 @@ import {
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
export type ActiveWorkspacesCommandOptions = BaseCommandOptions & {
workspaceId?: string;
startFromWorkspaceId?: string;
workspaceCountLimit?: number;
};
export abstract class ActiveWorkspacesCommandRunner extends BaseCommandRunner {
private workspaceIds: string[] = [];
private startFromWorkspaceId: string | undefined;
private workspaceCountLimit: number | undefined;
constructor(protected readonly workspaceRepository: Repository<Workspace>) {
super();
}
@Option({
flags: '--start-from-workspace-id [workspace_id]',
description:
'Start from a specific workspace id. Workspaces are processed in ascending order of id.',
required: false,
})
parseStartFromWorkspaceId(val: string): string {
this.startFromWorkspaceId = val;
return val;
}
@Option({
flags: '--workspace-count-limit [count]',
description:
'Limit the number of workspaces to process. Workspaces are processed in ascending order of id.',
required: false,
})
parseWorkspaceCountLimit(val: string): number {
this.workspaceCountLimit = parseInt(val);
if (isNaN(this.workspaceCountLimit)) {
throw new Error('Workspace count limit must be a number');
}
if (this.workspaceCountLimit <= 0) {
throw new Error('Workspace count limit must be greater than 0');
}
return this.workspaceCountLimit;
}
@Option({
flags: '-w, --workspace-id [workspace_id]',
description:
'workspace id. Command runs on all active workspaces if not provided',
'workspace id. Command runs on all active workspaces if not provided.',
required: false,
})
parseWorkspaceId(val: string): string[] {
@ -39,10 +75,14 @@ export abstract class ActiveWorkspacesCommandRunner extends BaseCommandRunner {
WorkspaceActivationStatus.ACTIVE,
WorkspaceActivationStatus.SUSPENDED,
]),
...(this.startFromWorkspaceId
? { id: MoreThanOrEqual(this.startFromWorkspaceId) }
: {}),
},
order: {
createdAt: 'ASC',
id: 'ASC',
},
take: this.workspaceCountLimit,
});
return activeWorkspaces.map((workspace) => workspace.id);

View File

@ -105,23 +105,25 @@ export class MigrateRichTextFieldCommand extends ActiveWorkspacesCommandRunner {
this.logger.setVerbose(options.verbose ?? false);
}
try {
for (const [index, workspaceId] of workspaceIds.entries()) {
for (const [index, workspaceId] of workspaceIds.entries()) {
try {
await this.processWorkspace({
workspaceId,
index,
total: workspaceIds.length,
});
await this.twentyORMGlobalManager.destroyDataSourceForWorkspace(
workspaceId,
} catch (error) {
this.logger.log(
chalk.red(`Error in workspace ${workspaceId}: ${error}`),
);
}
this.logger.log(chalk.green('Command completed!'));
} catch (error) {
this.logger.log(chalk.red('Error in workspace'));
await this.twentyORMGlobalManager.destroyDataSourceForWorkspace(
workspaceId,
);
}
this.logger.log(chalk.green('Command completed!'));
}
private async processWorkspace({
@ -170,9 +172,6 @@ export class MigrateRichTextFieldCommand extends ActiveWorkspacesCommandRunner {
);
}
await this.twentyORMGlobalManager.destroyDataSourceForWorkspace(
workspaceId,
);
this.logger.log(
chalk.green(`Command completed for workspace ${workspaceId}`),
);