Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean views without object metadata #7153

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { InjectRepository } from '@nestjs/typeorm';

import chalk from 'chalk';
import { Command } from 'nest-commander';
import { In, Repository } from 'typeorm';

import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { ViewWorkspaceEntity } from 'src/modules/view/standard-objects/view.workspace-entity';

@Command({
name: 'upgrade-0.31:clean-views-with-deleted-object-metadata',
description: 'Clean views with deleted object metadata',
})
export class CleanViewsWithDeletedObjectMetadataCommand extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
@InjectRepository(ObjectMetadataEntity, 'metadata')
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {
super(workspaceRepository);
}

async executeActiveWorkspacesCommand(
_passedParam: string[],
_options: ActiveWorkspacesCommandOptions,
workspaceIds: string[],
): Promise<void> {
this.logger.log('Running command to fix migration');

for (const workspaceId of workspaceIds) {
this.logger.log(`Running command for workspace ${workspaceId}`);

try {
this.logger.log(chalk.green(`Cleaning views of ${workspaceId}.`));

await this.cleanViewsWithDeletedObjectMetadata(
workspaceId,
_options.dryRun ?? false,
);
thomtrp marked this conversation as resolved.
Show resolved Hide resolved

await this.twentyORMGlobalManager.destroyDataSourceForWorkspace(
workspaceId,
);
} catch (error) {
this.logger.log(
chalk.red(
`Running command on workspace ${workspaceId} failed with error: ${error}`,
),
);
continue;
} finally {
this.logger.log(
chalk.green(`Finished running command for workspace ${workspaceId}.`),
);
}

this.logger.log(chalk.green(`Command completed!`));
}
}

private async cleanViewsWithDeletedObjectMetadata(
workspaceId: string,
dryRun: boolean,
): Promise<void> {
const viewRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewWorkspaceEntity>(
workspaceId,
'view',
false,
);

const allViews = await viewRepository.find();
thomtrp marked this conversation as resolved.
Show resolved Hide resolved

const viewObjectMetadataIds = allViews.map((view) => view.objectMetadataId);

const objectMetadataEntities = await this.objectMetadataRepository.find({
where: {
id: In(viewObjectMetadataIds),
},
});

const validObjectMetadataIds = new Set(
objectMetadataEntities.map((entity) => entity.id),
);

const viewIdsToDelete = allViews
.filter((view) => !validObjectMetadataIds.has(view.objectMetadataId))
.map((view) => view.id);

if (dryRun) {
this.logger.log(
chalk.green(
`Found ${viewIdsToDelete.length} views to clean in workspace ${workspaceId}.`,
),
);
}

if (viewIdsToDelete.length > 0 && !dryRun) {
await viewRepository.delete(viewIdsToDelete);
this.logger.log(chalk.green(`Cleaning ${viewIdsToDelete.length} views.`));
thomtrp marked this conversation as resolved.
Show resolved Hide resolved
}

if (viewIdsToDelete.length === 0) {
this.logger.log(chalk.green(`No views to clean.`));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { Repository } from 'typeorm';

import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
import { BackfillWorkspaceFavoritesCommand } from 'src/database/commands/upgrade-version/0-31/0-31-backfill-workspace-favorites.command';
import { CleanViewsWithDeletedObjectMetadataCommand } from 'src/database/commands/upgrade-version/0-31/0-31-clean-views-with-deleted-object-metadata.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { SyncWorkspaceMetadataCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/sync-workspace-metadata.command';

interface UpdateTo0_31CommandOptions {
workspaceId?: string;
Expand All @@ -19,7 +21,9 @@ export class UpgradeTo0_31Command extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
private readonly syncWorkspaceMetadataCommand: SyncWorkspaceMetadataCommand,
private readonly backfillWorkspaceFavoritesCommand: BackfillWorkspaceFavoritesCommand,
private readonly cleanViewsWithDeletedObjectMetadataCommand: CleanViewsWithDeletedObjectMetadataCommand,
) {
super(workspaceRepository);
}
Expand All @@ -29,6 +33,21 @@ export class UpgradeTo0_31Command extends ActiveWorkspacesCommandRunner {
options: UpdateTo0_31CommandOptions,
workspaceIds: string[],
): Promise<void> {
await this.syncWorkspaceMetadataCommand.executeActiveWorkspacesCommand(
passedParam,
{
...options,
force: true,
},
workspaceIds,
);
await this.cleanViewsWithDeletedObjectMetadataCommand.executeActiveWorkspacesCommand(
passedParam,
{
...options,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ ... options } ==> options

},
workspaceIds,
);
await this.backfillWorkspaceFavoritesCommand.executeActiveWorkspacesCommand(
passedParam,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { BackfillWorkspaceFavoritesCommand } from 'src/database/commands/upgrade-version/0-31/0-31-backfill-workspace-favorites.command';
import { CleanViewsWithDeletedObjectMetadataCommand } from 'src/database/commands/upgrade-version/0-31/0-31-clean-views-with-deleted-object-metadata.command';
import { UpgradeTo0_31Command } from 'src/database/commands/upgrade-version/0-31/0-31-upgrade-version.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module';

@Module({
imports: [TypeOrmModule.forFeature([Workspace], 'core')],
providers: [UpgradeTo0_31Command, BackfillWorkspaceFavoritesCommand],
imports: [
TypeOrmModule.forFeature([Workspace], 'core'),
TypeOrmModule.forFeature([ObjectMetadataEntity], 'metadata'),
WorkspaceSyncMetadataCommandsModule,
],
providers: [
UpgradeTo0_31Command,
BackfillWorkspaceFavoritesCommand,
CleanViewsWithDeletedObjectMetadataCommand,
],
})
export class UpgradeTo0_31CommandModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { NestjsQueryTypeOrmModule } from '@ptc-org/nestjs-query-typeorm';

import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
import { WorkspaceAuthGuard } from 'src/engine/guards/workspace-auth.guard';
import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
Expand Down Expand Up @@ -44,7 +43,6 @@ import { UpdateObjectPayload } from './dtos/update-object.input';
WorkspaceMigrationModule,
WorkspaceMigrationRunnerModule,
WorkspaceMetadataVersionModule,
FeatureFlagModule,
RemoteTableRelationsModule,
],
services: [ObjectMetadataService],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { FindManyOptions, FindOneOptions, In, Repository } from 'typeorm';
import { FieldMetadataSettings } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-settings.interface';

import { TypeORMService } from 'src/database/typeorm/typeorm.service';
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import {
FieldMetadataEntity,
Expand Down Expand Up @@ -60,6 +59,7 @@ import {
createRelationDeterministicUuid,
} from 'src/engine/workspace-manager/workspace-sync-metadata/utils/create-deterministic-uuid.util';
import { FavoriteWorkspaceEntity } from 'src/modules/favorite/standard-objects/favorite.workspace-entity';
import { ViewWorkspaceEntity } from 'src/modules/view/standard-objects/view.workspace-entity';

import { ObjectMetadataEntity } from './object-metadata.entity';

Expand All @@ -85,7 +85,6 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService,
private readonly workspaceMetadataVersionService: WorkspaceMetadataVersionService,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
private readonly featureFlagService: FeatureFlagService,
) {
super(objectMetadataRepository);
}
Expand Down Expand Up @@ -143,6 +142,24 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
await this.deleteAllRelationsAndDropTable(objectMetadata, workspaceId);
}

// DELETE VIEWS
const viewRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewWorkspaceEntity>(
workspaceId,
'view',
);

const views = await viewRepository.find({
where: {
objectMetadataId: objectMetadata.id,
},
});

if (views.length >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Condition should be views.length > 0 instead of >= 0

await viewRepository.delete(views.map((view) => view.id));
}

// DELETE OBJECT
await this.objectMetadataRepository.delete(objectMetadata.id);

await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
Expand Down
Loading