|
| 1 | +import { Command } from 'nest-commander'; |
| 2 | + |
| 3 | +import { ActiveOrSuspendedWorkspaceCommandRunner } from 'src/database/commands/command-runners/active-or-suspended-workspace.command-runner'; |
| 4 | +import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service'; |
| 5 | +import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner'; |
| 6 | +import { ApplicationService } from 'src/engine/core-modules/application/application.service'; |
| 7 | +import { RegisteredWorkspaceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-workspace-command.decorator'; |
| 8 | +import { findFlatEntityByUniversalIdentifier } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-universal-identifier.util'; |
| 9 | +import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type'; |
| 10 | +import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service'; |
| 11 | +import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service'; |
| 12 | + |
| 13 | +const MESSAGE_DIRECTION_FIELD_UNIVERSAL_IDENTIFIER = |
| 14 | + '20202020-0203-4118-8e2a-05b9bdae6dab'; |
| 15 | + |
| 16 | +@RegisteredWorkspaceCommand('2.3.0', 1777400000000) |
| 17 | +@Command({ |
| 18 | + name: 'upgrade:2-3:drop-message-direction-field', |
| 19 | + description: |
| 20 | + 'Drop the leftover message.direction field metadata and its workspace column', |
| 21 | +}) |
| 22 | +export class DropMessageDirectionFieldCommand extends ActiveOrSuspendedWorkspaceCommandRunner { |
| 23 | + constructor( |
| 24 | + protected readonly workspaceIteratorService: WorkspaceIteratorService, |
| 25 | + private readonly applicationService: ApplicationService, |
| 26 | + private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService, |
| 27 | + private readonly workspaceCacheService: WorkspaceCacheService, |
| 28 | + ) { |
| 29 | + super(workspaceIteratorService); |
| 30 | + } |
| 31 | + |
| 32 | + override async runOnWorkspace({ |
| 33 | + workspaceId, |
| 34 | + options, |
| 35 | + }: RunOnWorkspaceArgs): Promise<void> { |
| 36 | + const isDryRun = options.dryRun ?? false; |
| 37 | + |
| 38 | + this.logger.log( |
| 39 | + `${isDryRun ? '[DRY RUN] ' : ''}Starting message.direction field removal for workspace ${workspaceId}`, |
| 40 | + ); |
| 41 | + |
| 42 | + const { flatFieldMetadataMaps } = |
| 43 | + await this.workspaceCacheService.getOrRecompute(workspaceId, [ |
| 44 | + 'flatFieldMetadataMaps', |
| 45 | + ]); |
| 46 | + |
| 47 | + const directionFieldMetadata = |
| 48 | + findFlatEntityByUniversalIdentifier<FlatFieldMetadata>({ |
| 49 | + flatEntityMaps: flatFieldMetadataMaps, |
| 50 | + universalIdentifier: MESSAGE_DIRECTION_FIELD_UNIVERSAL_IDENTIFIER, |
| 51 | + }); |
| 52 | + |
| 53 | + if (!directionFieldMetadata) { |
| 54 | + this.logger.log( |
| 55 | + `message.direction field already absent for workspace ${workspaceId}`, |
| 56 | + ); |
| 57 | + |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (isDryRun) { |
| 62 | + this.logger.log( |
| 63 | + `[DRY RUN] Would delete message.direction field for workspace ${workspaceId}`, |
| 64 | + ); |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const { twentyStandardFlatApplication } = |
| 70 | + await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow( |
| 71 | + { workspaceId }, |
| 72 | + ); |
| 73 | + |
| 74 | + const validateAndBuildResult = |
| 75 | + await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration( |
| 76 | + { |
| 77 | + allFlatEntityOperationByMetadataName: { |
| 78 | + fieldMetadata: { |
| 79 | + flatEntityToCreate: [], |
| 80 | + flatEntityToDelete: [directionFieldMetadata], |
| 81 | + flatEntityToUpdate: [], |
| 82 | + }, |
| 83 | + }, |
| 84 | + workspaceId, |
| 85 | + applicationUniversalIdentifier: |
| 86 | + twentyStandardFlatApplication.universalIdentifier, |
| 87 | + }, |
| 88 | + ); |
| 89 | + |
| 90 | + if (validateAndBuildResult.status === 'fail') { |
| 91 | + this.logger.error( |
| 92 | + `Failed to delete message.direction field:\n${JSON.stringify(validateAndBuildResult, null, 2)}`, |
| 93 | + ); |
| 94 | + |
| 95 | + throw new Error( |
| 96 | + `Failed to delete message.direction field for workspace ${workspaceId}`, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + this.logger.log( |
| 101 | + `Deleted message.direction field for workspace ${workspaceId}`, |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments