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

Update clean view command + add dry run to favorite backfill #7268

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -51,6 +51,7 @@ export class BackfillWorkspaceFavoritesCommand extends ActiveWorkspacesCommandRu
await this.createViewWorkspaceFavorites(
workspaceId,
activeWorkspaceIndexViews.map((view) => view.id),
_options.dryRun ?? false,
);

this.logger.log(
Expand Down Expand Up @@ -117,6 +118,7 @@ export class BackfillWorkspaceFavoritesCommand extends ActiveWorkspacesCommandRu
private async createViewWorkspaceFavorites(
workspaceId: string,
viewIds: string[],
dryRun: boolean,
) {
const favoriteRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<FavoriteWorkspaceEntity>(
Expand All @@ -125,6 +127,7 @@ export class BackfillWorkspaceFavoritesCommand extends ActiveWorkspacesCommandRu
);

let nextFavoritePosition = await favoriteRepository.count();
let createdFavorites = 0;

for (const viewId of viewIds) {
const existingFavorites = await favoriteRepository.find({
Expand All @@ -137,14 +140,23 @@ export class BackfillWorkspaceFavoritesCommand extends ActiveWorkspacesCommandRu
continue;
}

await favoriteRepository.insert(
favoriteRepository.create({
viewId,
position: nextFavoritePosition,
}),
);
if (!dryRun) {
await favoriteRepository.insert(
favoriteRepository.create({
viewId,
position: nextFavoritePosition,
}),
);
}

createdFavorites++;
nextFavoritePosition++;
}

this.logger.log(
chalk.green(
`Found ${createdFavorites} favorites to backfill in workspace ${workspaceId}.`,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import {
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 { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
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',
name: 'upgrade-0.31:clean-views-associated-with-outdated-objects',
description:
'Clean views associated with deleted object metadata or activities',
})
export class CleanViewsWithDeletedObjectMetadataCommand extends ActiveWorkspacesCommandRunner {
export class CleanViewsAssociatedWithOutdatedObjectsCommand extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
Expand Down Expand Up @@ -88,7 +90,9 @@ export class CleanViewsWithDeletedObjectMetadataCommand extends ActiveWorkspaces
});

const validObjectMetadataIds = new Set(
objectMetadataEntities.map((entity) => entity.id),
objectMetadataEntities
.filter((entity) => entity.standardId !== STANDARD_OBJECT_IDS.activity)
.map((entity) => entity.id),
);

const viewIdsToDelete = allViews
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Repository } from 'typeorm';
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
import { AddIndexKeyToTasksAndNotesViewsCommand } from 'src/database/commands/upgrade-version/0-31/0-31-add-index-key-to-tasks-and-notes-views.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 { CleanViewsAssociatedWithOutdatedObjectsCommand } from 'src/database/commands/upgrade-version/0-31/0-31-clean-views-associated-with-outdated-objects.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';

Expand All @@ -24,7 +24,7 @@ export class UpgradeTo0_31Command extends ActiveWorkspacesCommandRunner {
protected readonly workspaceRepository: Repository<Workspace>,
private readonly syncWorkspaceMetadataCommand: SyncWorkspaceMetadataCommand,
private readonly backfillWorkspaceFavoritesCommand: BackfillWorkspaceFavoritesCommand,
private readonly cleanViewsWithDeletedObjectMetadataCommand: CleanViewsWithDeletedObjectMetadataCommand,
private readonly cleanViewsAssociatedWithOutdatedObjectsCommand: CleanViewsAssociatedWithOutdatedObjectsCommand,
private readonly addIndexKeyToTasksAndNotesViewsCommand: AddIndexKeyToTasksAndNotesViewsCommand,
) {
super(workspaceRepository);
Expand All @@ -43,7 +43,7 @@ export class UpgradeTo0_31Command extends ActiveWorkspacesCommandRunner {
},
workspaceIds,
);
await this.cleanViewsWithDeletedObjectMetadataCommand.executeActiveWorkspacesCommand(
await this.cleanViewsAssociatedWithOutdatedObjectsCommand.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';

import { AddIndexKeyToTasksAndNotesViewsCommand } from 'src/database/commands/upgrade-version/0-31/0-31-add-index-key-to-tasks-and-notes-views.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 { CleanViewsAssociatedWithOutdatedObjectsCommand } from 'src/database/commands/upgrade-version/0-31/0-31-clean-views-associated-with-outdated-objects.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';
Expand All @@ -18,7 +18,7 @@ import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manage
providers: [
UpgradeTo0_31Command,
BackfillWorkspaceFavoritesCommand,
CleanViewsWithDeletedObjectMetadataCommand,
CleanViewsAssociatedWithOutdatedObjectsCommand,
AddIndexKeyToTasksAndNotesViewsCommand,
],
})
Expand Down
Loading