|
| 1 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 2 | + |
| 3 | +import chalk from 'chalk'; |
| 4 | +import { Command } from 'nest-commander'; |
| 5 | +import { In, Repository } from 'typeorm'; |
| 6 | + |
| 7 | +import { |
| 8 | + ActiveWorkspacesCommandOptions, |
| 9 | + ActiveWorkspacesCommandRunner, |
| 10 | +} from 'src/database/commands/active-workspaces.command'; |
| 11 | +import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; |
| 12 | +import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity'; |
| 13 | +import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; |
| 14 | +import { ViewWorkspaceEntity } from 'src/modules/view/standard-objects/view.workspace-entity'; |
| 15 | + |
| 16 | +@Command({ |
| 17 | + name: 'upgrade-0.31:clean-views-with-deleted-object-metadata', |
| 18 | + description: 'Clean views with deleted object metadata', |
| 19 | +}) |
| 20 | +export class CleanViewsWithDeletedObjectMetadataCommand extends ActiveWorkspacesCommandRunner { |
| 21 | + constructor( |
| 22 | + @InjectRepository(Workspace, 'core') |
| 23 | + protected readonly workspaceRepository: Repository<Workspace>, |
| 24 | + @InjectRepository(ObjectMetadataEntity, 'metadata') |
| 25 | + private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>, |
| 26 | + private readonly twentyORMGlobalManager: TwentyORMGlobalManager, |
| 27 | + ) { |
| 28 | + super(workspaceRepository); |
| 29 | + } |
| 30 | + |
| 31 | + async executeActiveWorkspacesCommand( |
| 32 | + _passedParam: string[], |
| 33 | + _options: ActiveWorkspacesCommandOptions, |
| 34 | + workspaceIds: string[], |
| 35 | + ): Promise<void> { |
| 36 | + this.logger.log('Running command to fix migration'); |
| 37 | + |
| 38 | + for (const workspaceId of workspaceIds) { |
| 39 | + this.logger.log(`Running command for workspace ${workspaceId}`); |
| 40 | + |
| 41 | + try { |
| 42 | + this.logger.log(chalk.green(`Cleaning views of ${workspaceId}.`)); |
| 43 | + |
| 44 | + await this.cleanViewsWithDeletedObjectMetadata( |
| 45 | + workspaceId, |
| 46 | + _options.dryRun ?? false, |
| 47 | + ); |
| 48 | + |
| 49 | + await this.twentyORMGlobalManager.destroyDataSourceForWorkspace( |
| 50 | + workspaceId, |
| 51 | + ); |
| 52 | + } catch (error) { |
| 53 | + this.logger.log( |
| 54 | + chalk.red( |
| 55 | + `Running command on workspace ${workspaceId} failed with error: ${error}`, |
| 56 | + ), |
| 57 | + ); |
| 58 | + continue; |
| 59 | + } finally { |
| 60 | + this.logger.log( |
| 61 | + chalk.green(`Finished running command for workspace ${workspaceId}.`), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + this.logger.log(chalk.green(`Command completed!`)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private async cleanViewsWithDeletedObjectMetadata( |
| 70 | + workspaceId: string, |
| 71 | + dryRun: boolean, |
| 72 | + ): Promise<void> { |
| 73 | + const viewRepository = |
| 74 | + await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewWorkspaceEntity>( |
| 75 | + workspaceId, |
| 76 | + 'view', |
| 77 | + false, |
| 78 | + ); |
| 79 | + |
| 80 | + const allViews = await viewRepository.find(); |
| 81 | + |
| 82 | + const viewObjectMetadataIds = allViews.map((view) => view.objectMetadataId); |
| 83 | + |
| 84 | + const objectMetadataEntities = await this.objectMetadataRepository.find({ |
| 85 | + where: { |
| 86 | + id: In(viewObjectMetadataIds), |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + const validObjectMetadataIds = new Set( |
| 91 | + objectMetadataEntities.map((entity) => entity.id), |
| 92 | + ); |
| 93 | + |
| 94 | + const viewIdsToDelete = allViews |
| 95 | + .filter((view) => !validObjectMetadataIds.has(view.objectMetadataId)) |
| 96 | + .map((view) => view.id); |
| 97 | + |
| 98 | + if (dryRun) { |
| 99 | + this.logger.log( |
| 100 | + chalk.green( |
| 101 | + `Found ${viewIdsToDelete.length} views to clean in workspace ${workspaceId}.`, |
| 102 | + ), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + if (viewIdsToDelete.length > 0 && !dryRun) { |
| 107 | + await viewRepository.delete(viewIdsToDelete); |
| 108 | + this.logger.log(chalk.green(`Cleaning ${viewIdsToDelete.length} views.`)); |
| 109 | + } |
| 110 | + |
| 111 | + if (viewIdsToDelete.length === 0) { |
| 112 | + this.logger.log(chalk.green(`No views to clean.`)); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments