Refactor page layout tab and widgets migration (#16367)
# Introduction Making columns nullable instead of required + metadata on the fly migration in typeorm migration that could affect other breaking change migrations to be run
This commit is contained in:
+143
@@ -0,0 +1,143 @@
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Command } from 'nest-commander';
|
||||
import { IsNull, Repository } from 'typeorm';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
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/application.service';
|
||||
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
|
||||
import { PageLayoutTabEntity } from 'src/engine/metadata-modules/page-layout/entities/page-layout-tab.entity';
|
||||
import { PageLayoutWidgetEntity } from 'src/engine/metadata-modules/page-layout/entities/page-layout-widget.entity';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
|
||||
|
||||
@Command({
|
||||
name: 'upgrade:1-13:backfill-page-layout-universal-identifiers',
|
||||
description:
|
||||
'Backfill universalIdentifier and applicationId for pageLayoutWidget and pageLayoutTab',
|
||||
})
|
||||
export class BackfillPageLayoutUniversalIdentifiersCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {
|
||||
constructor(
|
||||
@InjectRepository(WorkspaceEntity)
|
||||
protected readonly workspaceRepository: Repository<WorkspaceEntity>,
|
||||
@InjectRepository(PageLayoutWidgetEntity)
|
||||
private readonly pageLayoutWidgetRepository: Repository<PageLayoutWidgetEntity>,
|
||||
@InjectRepository(PageLayoutTabEntity)
|
||||
private readonly pageLayoutTabRepository: Repository<PageLayoutTabEntity>,
|
||||
protected readonly twentyORMGlobalManager: TwentyORMGlobalManager,
|
||||
protected readonly dataSourceService: DataSourceService,
|
||||
private readonly applicationService: ApplicationService,
|
||||
private readonly workspaceCacheService: WorkspaceCacheService,
|
||||
) {
|
||||
super(workspaceRepository, twentyORMGlobalManager, dataSourceService);
|
||||
}
|
||||
|
||||
override async runOnWorkspace({
|
||||
workspaceId,
|
||||
options,
|
||||
}: RunOnWorkspaceArgs): Promise<void> {
|
||||
this.logger.log(
|
||||
`Starting backfill of universalIdentifier and applicationId for workspace ${workspaceId}`,
|
||||
);
|
||||
|
||||
const { workspaceCustomFlatApplication } =
|
||||
await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
|
||||
{
|
||||
workspaceId,
|
||||
},
|
||||
);
|
||||
|
||||
const applicationId = workspaceCustomFlatApplication.id;
|
||||
|
||||
const widgetsToUpdate = await this.pageLayoutWidgetRepository.find({
|
||||
where: [
|
||||
{ workspaceId, universalIdentifier: IsNull() },
|
||||
{ workspaceId, applicationId: IsNull() },
|
||||
],
|
||||
select: ['id', 'workspaceId'],
|
||||
});
|
||||
|
||||
this.logger.log(
|
||||
`Found ${widgetsToUpdate.length} pageLayoutWidget records to backfill`,
|
||||
);
|
||||
|
||||
for (const widget of widgetsToUpdate) {
|
||||
const universalIdentifier = v4();
|
||||
|
||||
if (options.dryRun) {
|
||||
this.logger.log(
|
||||
`[DRY RUN] Would update pageLayoutWidget ${widget.id} with universalIdentifier ${universalIdentifier} and applicationId ${applicationId}`,
|
||||
);
|
||||
} else {
|
||||
await this.pageLayoutWidgetRepository.update(
|
||||
{ id: widget.id },
|
||||
{
|
||||
universalIdentifier,
|
||||
applicationId,
|
||||
},
|
||||
);
|
||||
|
||||
this.logger.log(
|
||||
`Updated pageLayoutWidget ${widget.id} with universalIdentifier ${universalIdentifier}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const tabsToUpdate = await this.pageLayoutTabRepository.find({
|
||||
where: [
|
||||
{ workspaceId, universalIdentifier: IsNull() },
|
||||
{ workspaceId, applicationId: IsNull() },
|
||||
],
|
||||
select: ['id', 'workspaceId'],
|
||||
});
|
||||
|
||||
this.logger.log(
|
||||
`Found ${tabsToUpdate.length} pageLayoutTab records to backfill`,
|
||||
);
|
||||
|
||||
for (const tab of tabsToUpdate) {
|
||||
const universalIdentifier = v4();
|
||||
|
||||
if (options.dryRun) {
|
||||
this.logger.log(
|
||||
`[DRY RUN] Would update pageLayoutTab ${tab.id} with universalIdentifier ${universalIdentifier} and applicationId ${applicationId}`,
|
||||
);
|
||||
} else {
|
||||
await this.pageLayoutTabRepository.update(
|
||||
{ id: tab.id },
|
||||
{
|
||||
universalIdentifier,
|
||||
applicationId,
|
||||
},
|
||||
);
|
||||
|
||||
this.logger.log(
|
||||
`Updated pageLayoutTab ${tab.id} with universalIdentifier ${universalIdentifier}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.log(
|
||||
`${options.dryRun ? '[DRY RUN] Would have ' : ''}Successfully backfilled ${widgetsToUpdate.length} widgets and ${tabsToUpdate.length} tabs`,
|
||||
);
|
||||
|
||||
if (
|
||||
!options.dryRun &&
|
||||
(tabsToUpdate.length > 0 || widgetsToUpdate.length > 0)
|
||||
) {
|
||||
this.logger.log(
|
||||
`Invalidating and recomputing cache for workspace ${workspaceId}`,
|
||||
);
|
||||
|
||||
await this.workspaceCacheService.invalidateAndRecompute(workspaceId, [
|
||||
'flatPageLayoutTabMaps',
|
||||
'flatPageLayoutWidgetMaps',
|
||||
]);
|
||||
|
||||
this.logger.log(`Cache invalidated and recomputed successfully`);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -1,18 +1,23 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { BackfillPageLayoutUniversalIdentifiersCommand } from 'src/database/commands/upgrade-version-command/1-13/1-13-backfill-page-layout-universal-identifiers.command';
|
||||
import { BackfillViewMainGroupByFieldMetadataIdCommand } from 'src/database/commands/upgrade-version-command/1-13/1-13-backfill-view-main-group-by-field-metadata-id.command';
|
||||
import { CleanEmptyStringNullInTextFieldsCommand } from 'src/database/commands/upgrade-version-command/1-13/1-13-clean-empty-string-null-in-text-fields.command';
|
||||
import { DeduplicateRoleTargetsCommand } from 'src/database/commands/upgrade-version-command/1-13/1-13-deduplicate-role-targets.command';
|
||||
import { UpdateRoleTargetsUniqueConstraintMigrationCommand } from 'src/database/commands/upgrade-version-command/1-13/1-13-update-role-targets-unique-constraint-migration.command';
|
||||
import { ApplicationModule } from 'src/engine/core-modules/application/application.module';
|
||||
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module';
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { PageLayoutTabEntity } from 'src/engine/metadata-modules/page-layout/entities/page-layout-tab.entity';
|
||||
import { PageLayoutWidgetEntity } from 'src/engine/metadata-modules/page-layout/entities/page-layout-widget.entity';
|
||||
import { RoleTargetEntity } from 'src/engine/metadata-modules/role-target/role-target.entity';
|
||||
import { ViewGroupEntity } from 'src/engine/metadata-modules/view-group/entities/view-group.entity';
|
||||
import { ViewEntity } from 'src/engine/metadata-modules/view/entities/view.entity';
|
||||
import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -24,18 +29,24 @@ import { ViewEntity } from 'src/engine/metadata-modules/view/entities/view.entit
|
||||
ViewGroupEntity,
|
||||
FeatureFlagEntity,
|
||||
RoleTargetEntity,
|
||||
PageLayoutWidgetEntity,
|
||||
PageLayoutTabEntity,
|
||||
]),
|
||||
DataSourceModule,
|
||||
ApplicationModule,
|
||||
WorkspaceCacheModule,
|
||||
],
|
||||
providers: [
|
||||
CleanEmptyStringNullInTextFieldsCommand,
|
||||
BackfillViewMainGroupByFieldMetadataIdCommand,
|
||||
BackfillPageLayoutUniversalIdentifiersCommand,
|
||||
DeduplicateRoleTargetsCommand,
|
||||
UpdateRoleTargetsUniqueConstraintMigrationCommand,
|
||||
],
|
||||
exports: [
|
||||
CleanEmptyStringNullInTextFieldsCommand,
|
||||
BackfillViewMainGroupByFieldMetadataIdCommand,
|
||||
BackfillPageLayoutUniversalIdentifiersCommand,
|
||||
DeduplicateRoleTargetsCommand,
|
||||
UpdateRoleTargetsUniqueConstraintMigrationCommand,
|
||||
],
|
||||
|
||||
+3
@@ -27,6 +27,7 @@ import { CleanNullEquivalentValuesCommand } from 'src/database/commands/upgrade-
|
||||
import { CreateWorkspaceCustomApplicationCommand } from 'src/database/commands/upgrade-version-command/1-12/1-12-create-workspace-custom-application.command';
|
||||
import { SetStandardApplicationNotUninstallableCommand } from 'src/database/commands/upgrade-version-command/1-12/1-12-set-standard-application-not-uninstallable.command';
|
||||
import { WorkspaceCustomApplicationIdNonNullableCommand } from 'src/database/commands/upgrade-version-command/1-12/1-12-workspace-custom-application-id-non-nullable-migration.command';
|
||||
import { BackfillPageLayoutUniversalIdentifiersCommand } from 'src/database/commands/upgrade-version-command/1-13/1-13-backfill-page-layout-universal-identifiers.command';
|
||||
import { DeduplicateRoleTargetsCommand } from 'src/database/commands/upgrade-version-command/1-13/1-13-deduplicate-role-targets.command';
|
||||
import { UpdateRoleTargetsUniqueConstraintMigrationCommand } from 'src/database/commands/upgrade-version-command/1-13/1-13-update-role-targets-unique-constraint-migration.command';
|
||||
import { FixLabelIdentifierPositionAndVisibilityCommand } from 'src/database/commands/upgrade-version-command/1-6/1-6-fix-label-identifier-position-and-visibility.command';
|
||||
@@ -98,6 +99,7 @@ export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
// 1.13 Commands
|
||||
protected readonly deduplicateRoleTargetsCommand: DeduplicateRoleTargetsCommand,
|
||||
protected readonly updateRoleTargetsUniqueConstraintMigrationCommand: UpdateRoleTargetsUniqueConstraintMigrationCommand,
|
||||
protected readonly backfillPageLayoutUniversalIdentifiersCommand: BackfillPageLayoutUniversalIdentifiersCommand,
|
||||
) {
|
||||
super(
|
||||
workspaceRepository,
|
||||
@@ -169,6 +171,7 @@ export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
beforeSyncMetadata: [
|
||||
this.deduplicateRoleTargetsCommand,
|
||||
this.updateRoleTargetsUniqueConstraintMigrationCommand,
|
||||
this.backfillPageLayoutUniversalIdentifiersCommand,
|
||||
],
|
||||
afterSyncMetadata: [],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user