Repository: twentyhq/twenty
File: packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service.ts (TypeScript)

Code before:
    }

    return {
      fromToAllFlatEntityMaps,
      inferDeletionFromMissingEntities,
      dependencyAllFlatEntityMaps,
      additionalCacheDataMaps,
    };
  }

  public async validateBuildAndRunWorkspaceMigrationFromTo(
    args: WorkspaceMigrationOrchestratorBuildArgs,
  ) {
    const validateAndBuildResult =
      await this.workspaceMigrationBuildOrchestratorService
        .buildWorkspaceMigration(args)
        .catch((error) => {
          this.logger.error(error);
          throw new WorkspaceMigrationV2Exception(
            WorkspaceMigrationV2ExceptionCode.BUILDER_INTERNAL_SERVER_ERROR,
            error.message,
          );
        });

    if (validateAndBuildResult.status === 'fail') {
      this.logger.debug(JSON.stringify(validateAndBuildResult, null, 2));
      return validateAndBuildResult;
    }

    // Note: This should be removed once we've refactored the runner optimistic rendering
    // As with the current implementation passing an empty workspace migration might result
    // in dependency flat entity maps invalidation
    if (validateAndBuildResult.workspaceMigration.actions.length === 0) {
      return;
    }

    await this.workspaceMigrationRunnerService.run(
      validateAndBuildResult.workspaceMigration,
    );
  }

  public async validateBuildAndRunWorkspaceMigration({
    allFlatEntityOperationByMetadataName: allFlatEntities,
    workspaceId,
    isSystemBuild = false,
  }: ValidateBuildAndRunWorkspaceMigrationFromMatriceArgs): Promise<
    WorkspaceMigrationOrchestratorFailedResult | undefined
  > {
    const {
      fromToAllFlatEntityMaps,
      inferDeletionFromMissingEntities,

Review comment (bug):
check log lvl debug even before in order to avoid stringify for nothing, even redundant lets avoid it for perf motivation

Code after:
        flatEntityToDelete,
        flatEntityToUpdate,
      });

      if (flatEntityToDelete.length > 0) {
        inferDeletionFromMissingEntities[metadataName] = true;
      }
    }

    return {
      fromToAllFlatEntityMaps,
      inferDeletionFromMissingEntities,
      dependencyAllFlatEntityMaps,
      additionalCacheDataMaps,
    };
  }

  public async validateBuildAndRunWorkspaceMigrationFromTo(
    args: WorkspaceMigrationOrchestratorBuildArgs,
  ) {
    const validateAndBuildResult =
      await this.workspaceMigrationBuildOrchestratorService
        .buildWorkspaceMigration(args)
        .catch((error) => {
          this.logger.error(error);
          throw new WorkspaceMigrationV2Exception(
            WorkspaceMigrationV2ExceptionCode.BUILDER_INTERNAL_SERVER_ERROR,
            error.message,
          );
        });

    if (validateAndBuildResult.status === 'fail') {
      if (this.isDebugEnabled) {
        this.logger.debug(JSON.stringify(validateAndBuildResult, null, 2));
      }

      return validateAndBuildResult;
    }

    // Note: This should be removed once we've refactored the runner optimistic rendering
    // As with the current implementation passing an empty workspace migration might result
    // in dependency flat entity maps invalidation
    if (validateAndBuildResult.workspaceMigration.actions.length === 0) {
      return;
    }

    await this.workspaceMigrationRunnerService.run(
      validateAndBuildResult.workspaceMigration,
    );
  }