From b0b033aec9fed61ddefd31b5f21bb7b7d2464571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20M?= Date: Mon, 12 Feb 2024 16:17:17 +0100 Subject: [PATCH] fix: workspace health (#3916) * fix: workspace health applying migrations multiple times * fix: remove log * fix: use logger --- .../commands/workspace-health.command.ts | 23 +++++++++------- .../workspace-fix-default-value.service.ts | 27 ++++++++----------- .../workspace-fix-nullable.service.ts | 27 ++++++++----------- .../services/workspace-fix-type.service.ts | 27 ++++++++----------- 4 files changed, 47 insertions(+), 57 deletions(-) diff --git a/packages/twenty-server/src/workspace/workspace-health/commands/workspace-health.command.ts b/packages/twenty-server/src/workspace/workspace-health/commands/workspace-health.command.ts index 3bace0e17..7f49352c5 100644 --- a/packages/twenty-server/src/workspace/workspace-health/commands/workspace-health.command.ts +++ b/packages/twenty-server/src/workspace/workspace-health/commands/workspace-health.command.ts @@ -1,3 +1,5 @@ +import { Logger } from '@nestjs/common'; + import { Command, CommandRunner, Option } from 'nest-commander'; import chalk from 'chalk'; @@ -20,6 +22,7 @@ interface WorkspaceHealthCommandOptions { description: 'Check health of the given workspace.', }) export class WorkspaceHealthCommand extends CommandRunner { + private readonly logger = new Logger(WorkspaceHealthCommand.name); private readonly commandLogger = new CommandLogger( WorkspaceHealthCommand.name, ); @@ -40,21 +43,23 @@ export class WorkspaceHealthCommand extends CommandRunner { ); if (issues.length === 0) { - console.log(chalk.green('Workspace is healthy')); + this.logger.log(chalk.green('Workspace is healthy')); } else { - console.log(chalk.red('Workspace is not healthy')); + this.logger.log( + chalk.red(`Workspace is not healthy, found ${issues.length} issues`), + ); if (options.verbose) { - console.group(chalk.red('Issues')); - issues.forEach((issue) => { - console.log(chalk.yellow(JSON.stringify(issue, null, 2))); - }); - console.groupEnd(); + await this.commandLogger.writeLog( + `workspace-health-issues-${options.workspaceId}`, + issues, + ); + this.logger.log(chalk.yellow('Issues written to log')); } } if (options.fix) { - console.log(chalk.yellow('Fixing issues')); + this.logger.log(chalk.yellow('Fixing issues')); const workspaceMigrations = await this.workspaceHealthService.fixIssues( options.workspaceId, @@ -71,7 +76,7 @@ export class WorkspaceHealthCommand extends CommandRunner { workspaceMigrations, ); } else { - console.log( + this.logger.log( chalk.green( `Fixed ${workspaceMigrations.length}/${issues.length} issues`, ), diff --git a/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-default-value.service.ts b/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-default-value.service.ts index c25260aaa..2d45eeb8e 100644 --- a/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-default-value.service.ts +++ b/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-default-value.service.ts @@ -28,24 +28,19 @@ export class WorkspaceFixDefaultValueService { issues: WorkspaceHealthDefaultValueIssue[], ): Promise[]> { const workspaceMigrations: Partial[] = []; + const defaultValueIssues = issues.filter( + (issue) => + issue.type === WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT, + ) as WorkspaceHealthColumnIssue[]; - for (const issue of issues) { - switch (issue.type) { - case WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT: { - const columnNullabilityWorkspaceMigrations = - await this.fixColumnDefaultValueIssues( - objectMetadataCollection, - issues.filter( - (issue) => - issue.type === - WorkspaceHealthIssueType.COLUMN_DEFAULT_VALUE_CONFLICT, - ) as WorkspaceHealthColumnIssue[], - ); + if (defaultValueIssues.length > 0) { + const columnDefaultValueWorkspaceMigrations = + await this.fixColumnDefaultValueIssues( + objectMetadataCollection, + defaultValueIssues, + ); - workspaceMigrations.push(...columnNullabilityWorkspaceMigrations); - break; - } - } + workspaceMigrations.push(...columnDefaultValueWorkspaceMigrations); } return workspaceMigrations; diff --git a/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-nullable.service.ts b/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-nullable.service.ts index a92e18c4a..b9353e8b6 100644 --- a/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-nullable.service.ts +++ b/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-nullable.service.ts @@ -27,24 +27,19 @@ export class WorkspaceFixNullableService { issues: WorkspaceHealthNullableIssue[], ): Promise[]> { const workspaceMigrations: Partial[] = []; + const nullabilityIssues = issues.filter( + (issue) => + issue.type === WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT, + ) as WorkspaceHealthColumnIssue[]; - for (const issue of issues) { - switch (issue.type) { - case WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT: { - const columnNullabilityWorkspaceMigrations = - await this.fixColumnNullabilityIssues( - objectMetadataCollection, - issues.filter( - (issue) => - issue.type === - WorkspaceHealthIssueType.COLUMN_NULLABILITY_CONFLICT, - ) as WorkspaceHealthColumnIssue[], - ); + if (nullabilityIssues.length > 0) { + const columnNullabilityWorkspaceMigrations = + await this.fixColumnNullabilityIssues( + objectMetadataCollection, + nullabilityIssues, + ); - workspaceMigrations.push(...columnNullabilityWorkspaceMigrations); - break; - } - } + workspaceMigrations.push(...columnNullabilityWorkspaceMigrations); } return workspaceMigrations; diff --git a/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-type.service.ts b/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-type.service.ts index 582eb552c..f516e071d 100644 --- a/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-type.service.ts +++ b/packages/twenty-server/src/workspace/workspace-health/services/workspace-fix-type.service.ts @@ -30,24 +30,19 @@ export class WorkspaceFixTypeService { issues: WorkspaceHealthTypeIssue[], ): Promise[]> { const workspaceMigrations: Partial[] = []; + const columnTypeIssues = issues.filter( + (issue) => + issue.type === WorkspaceHealthIssueType.COLUMN_DATA_TYPE_CONFLICT, + ) as WorkspaceHealthColumnIssue[]; - for (const issue of issues) { - switch (issue.type) { - case WorkspaceHealthIssueType.COLUMN_DATA_TYPE_CONFLICT: { - const columnNullabilityWorkspaceMigrations = - await this.fixColumnTypeIssues( - objectMetadataCollection, - issues.filter( - (issue) => - issue.type === - WorkspaceHealthIssueType.COLUMN_DATA_TYPE_CONFLICT, - ) as WorkspaceHealthColumnIssue[], - ); + if (columnTypeIssues.length > 0) { + const columnNullabilityWorkspaceMigrations = + await this.fixColumnTypeIssues( + objectMetadataCollection, + columnTypeIssues, + ); - workspaceMigrations.push(...columnNullabilityWorkspaceMigrations); - break; - } - } + workspaceMigrations.push(...columnNullabilityWorkspaceMigrations); } return workspaceMigrations;