-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add page layout backfill command #18095
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
221 changes: 221 additions & 0 deletions
221
.../src/database/commands/upgrade-version-command/1-20/1-20-backfill-page-layouts.command.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { InjectRepository } from '@nestjs/typeorm'; | ||
|
|
||
| import { Command } from 'nest-commander'; | ||
| import { ViewType } from 'twenty-shared/types'; | ||
| import { isDefined } from 'twenty-shared/utils'; | ||
| import { Repository } from 'typeorm'; | ||
|
|
||
| import { ActiveOrSuspendedWorkspacesMigrationCommandRunner } from 'src/database/commands/command-runners/active-or-suspended-workspaces-migration.command-runner'; | ||
| import { RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspaces-migration.command-runner'; | ||
| import { ApplicationService } from 'src/engine/core-modules/application/services/application.service'; | ||
| import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum'; | ||
| import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service'; | ||
| import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
| import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service'; | ||
| import { PageLayoutType } from 'src/engine/metadata-modules/page-layout/enums/page-layout-type.enum'; | ||
| import { GlobalWorkspaceOrmManager } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-orm.manager'; | ||
| import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service'; | ||
| import { computeTwentyStandardApplicationAllFlatEntityMaps } from 'src/engine/workspace-manager/twenty-standard-application/utils/twenty-standard-application-all-flat-entity-maps.constant'; | ||
| import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service'; | ||
|
|
||
| @Command({ | ||
| name: 'upgrade:1-20:backfill-page-layouts', | ||
| description: | ||
| 'Backfill RECORD_PAGE page layouts for legacy workspaces and enable the IS_RECORD_PAGE_LAYOUT_EDITING_ENABLED feature flag', | ||
| }) | ||
| export class BackfillPageLayoutsCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner { | ||
| constructor( | ||
| @InjectRepository(WorkspaceEntity) | ||
| protected readonly workspaceRepository: Repository<WorkspaceEntity>, | ||
| protected readonly twentyORMGlobalManager: GlobalWorkspaceOrmManager, | ||
| protected readonly dataSourceService: DataSourceService, | ||
| private readonly applicationService: ApplicationService, | ||
| private readonly workspaceCacheService: WorkspaceCacheService, | ||
| private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService, | ||
| private readonly featureFlagService: FeatureFlagService, | ||
| ) { | ||
| super(workspaceRepository, twentyORMGlobalManager, dataSourceService); | ||
| } | ||
|
|
||
| override async runOnWorkspace({ | ||
| workspaceId, | ||
| options, | ||
| }: RunOnWorkspaceArgs): Promise<void> { | ||
| const isDryRun = options.dryRun ?? false; | ||
|
|
||
| this.logger.log( | ||
| `${isDryRun ? '[DRY RUN] ' : ''}Starting backfill of page layouts for workspace ${workspaceId}`, | ||
| ); | ||
|
|
||
| const isAlreadyEnabled = await this.featureFlagService.isFeatureEnabled( | ||
| FeatureFlagKey.IS_RECORD_PAGE_LAYOUT_EDITING_ENABLED, | ||
| workspaceId, | ||
| ); | ||
|
|
||
| if (isAlreadyEnabled) { | ||
| this.logger.log( | ||
| `IS_RECORD_PAGE_LAYOUT_EDITING_ENABLED already enabled for workspace ${workspaceId}, skipping`, | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (isDryRun) { | ||
| this.logger.log( | ||
| `[DRY RUN] Would create RECORD_PAGE page layouts and enable IS_RECORD_PAGE_LAYOUT_EDITING_ENABLED for workspace ${workspaceId}`, | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const { twentyStandardFlatApplication } = | ||
| await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow( | ||
| { workspaceId }, | ||
| ); | ||
|
|
||
| const { allFlatEntityMaps: standardAllFlatEntityMaps } = | ||
| computeTwentyStandardApplicationAllFlatEntityMaps({ | ||
| shouldIncludeRecordPageLayouts: true, | ||
| now: new Date().toISOString(), | ||
| workspaceId, | ||
| twentyStandardApplicationId: twentyStandardFlatApplication.id, | ||
| }); | ||
|
|
||
| const recordPageLayoutUniversalIdentifiers = new Set<string>(); | ||
|
|
||
| const pageLayoutsToCreate = Object.values( | ||
| standardAllFlatEntityMaps.flatPageLayoutMaps.byUniversalIdentifier, | ||
| ) | ||
| .filter(isDefined) | ||
| .filter((pageLayout) => { | ||
| if (pageLayout.type !== PageLayoutType.RECORD_PAGE) { | ||
| return false; | ||
| } | ||
|
|
||
| recordPageLayoutUniversalIdentifiers.add( | ||
| pageLayout.universalIdentifier, | ||
| ); | ||
|
|
||
| return true; | ||
| }); | ||
|
|
||
| const tabUniversalIdentifiers = new Set<string>(); | ||
|
|
||
| const pageLayoutTabsToCreate = Object.values( | ||
| standardAllFlatEntityMaps.flatPageLayoutTabMaps.byUniversalIdentifier, | ||
| ) | ||
| .filter(isDefined) | ||
| .filter((tab) => { | ||
| if ( | ||
| !recordPageLayoutUniversalIdentifiers.has( | ||
| tab.pageLayoutUniversalIdentifier, | ||
| ) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| tabUniversalIdentifiers.add(tab.universalIdentifier); | ||
|
|
||
| return true; | ||
| }); | ||
|
|
||
| const pageLayoutWidgetsToCreate = Object.values( | ||
| standardAllFlatEntityMaps.flatPageLayoutWidgetMaps.byUniversalIdentifier, | ||
| ) | ||
| .filter(isDefined) | ||
| .filter((widget) => | ||
| tabUniversalIdentifiers.has(widget.pageLayoutTabUniversalIdentifier), | ||
| ); | ||
|
|
||
| const viewUniversalIdentifiers = new Set<string>(); | ||
|
|
||
| const viewsToCreate = Object.values( | ||
| standardAllFlatEntityMaps.flatViewMaps.byUniversalIdentifier, | ||
| ) | ||
| .filter(isDefined) | ||
| .filter((view) => { | ||
| if (view.type !== ViewType.FIELDS_WIDGET) { | ||
| return false; | ||
| } | ||
|
|
||
| viewUniversalIdentifiers.add(view.universalIdentifier); | ||
|
|
||
| return true; | ||
| }); | ||
|
|
||
| const viewFieldsToCreate = Object.values( | ||
| standardAllFlatEntityMaps.flatViewFieldMaps.byUniversalIdentifier, | ||
| ) | ||
| .filter(isDefined) | ||
| .filter((viewField) => | ||
| viewUniversalIdentifiers.has(viewField.viewUniversalIdentifier), | ||
| ); | ||
|
|
||
| const viewFieldGroupsToCreate = Object.values( | ||
| standardAllFlatEntityMaps.flatViewFieldGroupMaps.byUniversalIdentifier, | ||
| ) | ||
| .filter(isDefined) | ||
| .filter((viewFieldGroup) => | ||
| viewUniversalIdentifiers.has(viewFieldGroup.viewUniversalIdentifier), | ||
| ); | ||
|
|
||
| const validateAndBuildResult = | ||
| await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration( | ||
| { | ||
| allFlatEntityOperationByMetadataName: { | ||
| pageLayout: { | ||
| flatEntityToCreate: pageLayoutsToCreate, | ||
| flatEntityToDelete: [], | ||
| flatEntityToUpdate: [], | ||
| }, | ||
| pageLayoutTab: { | ||
| flatEntityToCreate: pageLayoutTabsToCreate, | ||
| flatEntityToDelete: [], | ||
| flatEntityToUpdate: [], | ||
| }, | ||
| pageLayoutWidget: { | ||
| flatEntityToCreate: pageLayoutWidgetsToCreate, | ||
| flatEntityToDelete: [], | ||
| flatEntityToUpdate: [], | ||
| }, | ||
| view: { | ||
| flatEntityToCreate: viewsToCreate, | ||
| flatEntityToDelete: [], | ||
| flatEntityToUpdate: [], | ||
| }, | ||
| viewField: { | ||
| flatEntityToCreate: viewFieldsToCreate, | ||
| flatEntityToDelete: [], | ||
| flatEntityToUpdate: [], | ||
| }, | ||
| viewFieldGroup: { | ||
| flatEntityToCreate: viewFieldGroupsToCreate, | ||
| flatEntityToDelete: [], | ||
| flatEntityToUpdate: [], | ||
| }, | ||
| }, | ||
| workspaceId, | ||
| applicationUniversalIdentifier: | ||
| twentyStandardFlatApplication.universalIdentifier, | ||
| }, | ||
| ); | ||
|
|
||
| if (validateAndBuildResult.status === 'fail') { | ||
| this.logger.error( | ||
| `Failed to create page layouts:\n${JSON.stringify(validateAndBuildResult, null, 2)}`, | ||
| ); | ||
| throw new Error( | ||
| `Failed to create page layouts for workspace ${workspaceId}`, | ||
| ); | ||
| } | ||
|
|
||
| await this.featureFlagService.enableFeatureFlags( | ||
| [FeatureFlagKey.IS_RECORD_PAGE_LAYOUT_EDITING_ENABLED], | ||
| workspaceId, | ||
| ); | ||
|
|
||
| this.logger.log( | ||
| `Successfully created page layouts and enabled IS_RECORD_PAGE_LAYOUT_EDITING_ENABLED for workspace ${workspaceId}`, | ||
| ); | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
...src/database/commands/upgrade-version-command/1-20/1-20-upgrade-version-command.module.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
|
||
| import { BackfillPageLayoutsCommand } from 'src/database/commands/upgrade-version-command/1-20/1-20-backfill-page-layouts.command'; | ||
| import { ApplicationModule } from 'src/engine/core-modules/application/application.module'; | ||
| import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module'; | ||
| import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
| import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module'; | ||
| import { WorkspaceMetadataVersionModule } from 'src/engine/metadata-modules/workspace-metadata-version/workspace-metadata-version.module'; | ||
| import { WorkspaceCacheStorageModule } from 'src/engine/workspace-cache-storage/workspace-cache-storage.module'; | ||
| import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache.module'; | ||
| import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/workspace-migration-runner.module'; | ||
| import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace-migration/workspace-migration.module'; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| TypeOrmModule.forFeature([WorkspaceEntity]), | ||
| DataSourceModule, | ||
| WorkspaceCacheModule, | ||
| WorkspaceCacheStorageModule, | ||
| WorkspaceMetadataVersionModule, | ||
| WorkspaceMigrationRunnerModule, | ||
| ApplicationModule, | ||
| WorkspaceMigrationModule, | ||
| FeatureFlagModule, | ||
| ], | ||
| providers: [BackfillPageLayoutsCommand], | ||
| exports: [BackfillPageLayoutsCommand], | ||
| }) | ||
| export class V1_20_UpgradeVersionCommandModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; | |
| import { V1_17_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/1-17/1-17-upgrade-version-command.module'; | ||
| import { V1_18_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/1-18/1-18-upgrade-version-command.module'; | ||
| import { V1_19_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/1-19/1-19-upgrade-version-command.module'; | ||
| import { V1_20_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/1-20/1-20-upgrade-version-command.module'; | ||
| import { UpgradeCommand } from 'src/database/commands/upgrade-version-command/upgrade.command'; | ||
| import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
| import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module'; | ||
|
|
@@ -14,6 +15,7 @@ import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-s | |
| V1_17_UpgradeVersionCommandModule, | ||
| V1_18_UpgradeVersionCommandModule, | ||
| V1_19_UpgradeVersionCommandModule, | ||
| V1_20_UpgradeVersionCommandModule, | ||
|
||
| DataSourceModule, | ||
| ], | ||
| providers: [UpgradeCommand], | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
workspaceCacheServiceis injected but never used. The cache invalidation is handled byworkspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(), so this dependency can be removed along with the import on line 17.