From 311fc402d591e5734370da36886e0294d944a546 Mon Sep 17 00:00:00 2001 From: Charles Bochet Date: Fri, 21 Feb 2025 10:37:04 +0100 Subject: [PATCH] Enhance ActiveWorkspaceCommand to order, limit and startFrom workspaceId (#10378) I have tested it locally with all combinaisons of params --- .../commands/active-workspaces.command.ts | 46 +++++++++++++++++-- .../0-42-migrate-rich-text-field.command.ts | 21 ++++----- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/packages/twenty-server/src/database/commands/active-workspaces.command.ts b/packages/twenty-server/src/database/commands/active-workspaces.command.ts index 3bff6c6a8..21192dc14 100644 --- a/packages/twenty-server/src/database/commands/active-workspaces.command.ts +++ b/packages/twenty-server/src/database/commands/active-workspaces.command.ts @@ -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) { 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); diff --git a/packages/twenty-server/src/database/commands/upgrade-version/0-42/0-42-migrate-rich-text-field.command.ts b/packages/twenty-server/src/database/commands/upgrade-version/0-42/0-42-migrate-rich-text-field.command.ts index 3cc3edb78..e98f5d527 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version/0-42/0-42-migrate-rich-text-field.command.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version/0-42/0-42-migrate-rich-text-field.command.ts @@ -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}`), );