Enhance ActiveWorkspaceCommand to order, limit and startFrom workspaceId (#10378)
I have tested it locally with all combinaisons of params
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import { Option } from 'nest-commander';
|
import { Option } from 'nest-commander';
|
||||||
import { WorkspaceActivationStatus } from 'twenty-shared';
|
import { WorkspaceActivationStatus } from 'twenty-shared';
|
||||||
import { In, Repository } from 'typeorm';
|
import { In, MoreThanOrEqual, Repository } from 'typeorm';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
BaseCommandOptions,
|
BaseCommandOptions,
|
||||||
@ -10,19 +10,55 @@ import {
|
|||||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||||
export type ActiveWorkspacesCommandOptions = BaseCommandOptions & {
|
export type ActiveWorkspacesCommandOptions = BaseCommandOptions & {
|
||||||
workspaceId?: string;
|
workspaceId?: string;
|
||||||
|
startFromWorkspaceId?: string;
|
||||||
|
workspaceCountLimit?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export abstract class ActiveWorkspacesCommandRunner extends BaseCommandRunner {
|
export abstract class ActiveWorkspacesCommandRunner extends BaseCommandRunner {
|
||||||
private workspaceIds: string[] = [];
|
private workspaceIds: string[] = [];
|
||||||
|
private startFromWorkspaceId: string | undefined;
|
||||||
|
private workspaceCountLimit: number | undefined;
|
||||||
|
|
||||||
constructor(protected readonly workspaceRepository: Repository<Workspace>) {
|
constructor(protected readonly workspaceRepository: Repository<Workspace>) {
|
||||||
super();
|
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({
|
@Option({
|
||||||
flags: '-w, --workspace-id [workspace_id]',
|
flags: '-w, --workspace-id [workspace_id]',
|
||||||
description:
|
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,
|
required: false,
|
||||||
})
|
})
|
||||||
parseWorkspaceId(val: string): string[] {
|
parseWorkspaceId(val: string): string[] {
|
||||||
@ -39,10 +75,14 @@ export abstract class ActiveWorkspacesCommandRunner extends BaseCommandRunner {
|
|||||||
WorkspaceActivationStatus.ACTIVE,
|
WorkspaceActivationStatus.ACTIVE,
|
||||||
WorkspaceActivationStatus.SUSPENDED,
|
WorkspaceActivationStatus.SUSPENDED,
|
||||||
]),
|
]),
|
||||||
|
...(this.startFromWorkspaceId
|
||||||
|
? { id: MoreThanOrEqual(this.startFromWorkspaceId) }
|
||||||
|
: {}),
|
||||||
},
|
},
|
||||||
order: {
|
order: {
|
||||||
createdAt: 'ASC',
|
id: 'ASC',
|
||||||
},
|
},
|
||||||
|
take: this.workspaceCountLimit,
|
||||||
});
|
});
|
||||||
|
|
||||||
return activeWorkspaces.map((workspace) => workspace.id);
|
return activeWorkspaces.map((workspace) => workspace.id);
|
||||||
|
|||||||
@ -105,23 +105,25 @@ export class MigrateRichTextFieldCommand extends ActiveWorkspacesCommandRunner {
|
|||||||
this.logger.setVerbose(options.verbose ?? false);
|
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({
|
await this.processWorkspace({
|
||||||
workspaceId,
|
workspaceId,
|
||||||
index,
|
index,
|
||||||
total: workspaceIds.length,
|
total: workspaceIds.length,
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
await this.twentyORMGlobalManager.destroyDataSourceForWorkspace(
|
this.logger.log(
|
||||||
workspaceId,
|
chalk.red(`Error in workspace ${workspaceId}: ${error}`),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.log(chalk.green('Command completed!'));
|
await this.twentyORMGlobalManager.destroyDataSourceForWorkspace(
|
||||||
} catch (error) {
|
workspaceId,
|
||||||
this.logger.log(chalk.red('Error in workspace'));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.log(chalk.green('Command completed!'));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async processWorkspace({
|
private async processWorkspace({
|
||||||
@ -170,9 +172,6 @@ export class MigrateRichTextFieldCommand extends ActiveWorkspacesCommandRunner {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.twentyORMGlobalManager.destroyDataSourceForWorkspace(
|
|
||||||
workspaceId,
|
|
||||||
);
|
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
chalk.green(`Command completed for workspace ${workspaceId}`),
|
chalk.green(`Command completed for workspace ${workspaceId}`),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user