a28c3a905a
## Problem Since the centralized metadata side-effect engine landed in v2.19, `WorkspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigrationFromRecord` runs `metadataSideEffectEngineService.expandWithSideEffects(...)` before building. As a result every historical upgrade command (`upgrade-version-command/1-21/*` … `2-18/*`), authored before the engine existed, now flows through it. Their operation matrix is no longer applied literally: the engine injects/cascades companions (system fields, `searchVector` field + GIN index, `searchFieldMetadata` rows, unique backing indexes) and can hard-fail on reserved-identifier collisions (`RESERVED_SYSTEM_UNIVERSAL_IDENTIFIER`). Two hazards for already-shipped commands: 1. **Collision → hard failure**: a command declaring a companion the engine now owns collides with the engine's deterministic `universalIdentifier`. 2. **Silent drift**: on object/field create/delete the engine adds/cascades companions the command author never intended, so workspaces upgraded now differ structurally from those upgraded incrementally before 2.19. Suspected real-world impact: a self-hosted user upgrading v2.6.1 → v2.21.0 hit `duplicate key value violates unique constraint "IDX_SEARCH_FIELD_METADATA_OBJECT_FIELD_UNIQUE"` in `upgrade:2-16:backfill-search-field-metadata`, because object-creating commands now cascade and pre-create the deterministic `searchFieldMetadata` rows the standalone backfill then re-inserts. ## Changes - `workspace-migration-validate-build-and-run-service.ts`: extract the shared compute-and-run tail into a private method, and add `validateBuildAndRunLegacyWorkspaceMigration` (marked `@deprecated`) that skips `expandWithSideEffects` and applies the matrix literally. The existing side-effect entry points are unchanged (the live API and application manifests depend on them). - Repoint **all** pre-2.19 upgrade command call sites (1-21 … 2-18, including `2-10 sync-call-recording-standard-objects`) to the legacy method. Only the four `2-20/*` commands (target version ≥ 2.19) remain on the side-effect path. - `2-16 backfill-search-field-metadata`: recompute `flatSearchFieldMetadataMaps` from the database before building the existing-rows dedupe set. The migration runner only invalidates the flat-maps keys a migration touched, so during a cross-version upgrade earlier commands can leave this map stale; a stale map breaks the dedupe and re-inserts rows, tripping `IDX_SEARCH_FIELD_METADATA_OBJECT_FIELD_UNIQUE`. This is the direct fix for the reported failure. - Export `FlatEntityMapsBundle` so the shared tail can be typed. - Document the side-effect vs legacy path and the selection rule in `packages/twenty-server/docs/UPGRADE_COMMANDS.md`. Selection rule: target version **< 2.19** → legacy path; **≥ 2.19** → side-effect path (default). No exceptions. ## Known gap / merge ordering The static twenty-standard definition declares all of `callRecording`'s fields (including the `searchVector` system field) but **not** its `searchVector` GIN index — every other searchable standard object declares its GIN index statically. On the legacy path, workspaces upgrading through `2-10 sync-call-recording-standard-objects` therefore create the `searchVector` column unindexed (`searchFieldMetadata` rows are created later in the same pipeline by the 2-16 backfill). The static GIN index declaration plus a backfill for already-upgraded workspaces land in a follow-up (twentyhq/core-team-issues#2672), which must ship in the same release as this PR. ## Out of scope (separate follow-ups) - `UpgradeMigrationService.getLastAttemptedInstanceCommand()` ordering. - callRecording `searchVector` GIN index static declaration + backfill (twentyhq/core-team-issues#2672, same-release dependency, see above). ## Test plan - `nx typecheck twenty-server` passes. - `nx lint:diff-with-main twenty-server` (oxlint + oxfmt) clean on changed files. - 2-20 command specs (which exercise the unchanged side-effect path) pass. --------- Co-authored-by: twenty <noreply@twenty.com>
143 lines
5.6 KiB
TypeScript
143 lines
5.6 KiB
TypeScript
import { Command } from 'nest-commander';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
|
|
import { ActiveOrSuspendedWorkspaceCommandRunner } from 'src/database/commands/command-runners/active-or-suspended-workspace.command-runner';
|
|
import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';
|
|
import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner';
|
|
import { ApplicationService } from 'src/engine/core-modules/application/application.service';
|
|
import { RegisteredWorkspaceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-workspace-command.decorator';
|
|
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
|
|
import { STANDARD_COMMAND_MENU_ITEMS } from 'src/engine/workspace-manager/twenty-standard-application/constants/standard-command-menu-item.constant';
|
|
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';
|
|
|
|
const UNIVERSAL_IDENTIFIERS_TO_FIX = new Set<string>([
|
|
STANDARD_COMMAND_MENU_ITEMS.createNewRecord.universalIdentifier,
|
|
STANDARD_COMMAND_MENU_ITEMS.importRecords.universalIdentifier,
|
|
STANDARD_COMMAND_MENU_ITEMS.exportView.universalIdentifier,
|
|
STANDARD_COMMAND_MENU_ITEMS.seeDeletedRecords.universalIdentifier,
|
|
STANDARD_COMMAND_MENU_ITEMS.createNewView.universalIdentifier,
|
|
STANDARD_COMMAND_MENU_ITEMS.hideDeletedRecords.universalIdentifier,
|
|
]);
|
|
|
|
@RegisteredWorkspaceCommand('1.23.0', 1780000005000)
|
|
@Command({
|
|
name: 'upgrade:1-23:update-global-object-context-command-menu-items',
|
|
description:
|
|
'Update command menu items that require object context from GLOBAL to GLOBAL_OBJECT_CONTEXT',
|
|
})
|
|
export class UpdateGlobalObjectContextCommandMenuItemsCommand extends ActiveOrSuspendedWorkspaceCommandRunner {
|
|
constructor(
|
|
protected readonly workspaceIteratorService: WorkspaceIteratorService,
|
|
private readonly applicationService: ApplicationService,
|
|
private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService,
|
|
private readonly workspaceCacheService: WorkspaceCacheService,
|
|
) {
|
|
super(workspaceIteratorService);
|
|
}
|
|
|
|
override async runOnWorkspace({
|
|
workspaceId,
|
|
options,
|
|
}: RunOnWorkspaceArgs): Promise<void> {
|
|
const isDryRun = options.dryRun ?? false;
|
|
|
|
this.logger.log(
|
|
`${isDryRun ? '[DRY RUN] ' : ''}Starting GLOBAL_OBJECT_CONTEXT availability type update for workspace ${workspaceId}`,
|
|
);
|
|
|
|
const { twentyStandardFlatApplication } =
|
|
await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
|
|
{ workspaceId },
|
|
);
|
|
|
|
const { flatCommandMenuItemMaps: existingFlatCommandMenuItemMaps } =
|
|
await this.workspaceCacheService.getOrRecompute(workspaceId, [
|
|
'flatCommandMenuItemMaps',
|
|
]);
|
|
|
|
const { allFlatEntityMaps: standardAllFlatEntityMaps } =
|
|
computeTwentyStandardApplicationAllFlatEntityMaps({
|
|
now: new Date().toISOString(),
|
|
workspaceId,
|
|
twentyStandardApplicationId: twentyStandardFlatApplication.id,
|
|
});
|
|
|
|
const itemsToUpdate = [...UNIVERSAL_IDENTIFIERS_TO_FIX]
|
|
.map((universalIdentifier) => {
|
|
const standardItem =
|
|
standardAllFlatEntityMaps.flatCommandMenuItemMaps
|
|
.byUniversalIdentifier[universalIdentifier];
|
|
const existingItem =
|
|
existingFlatCommandMenuItemMaps.byUniversalIdentifier[
|
|
universalIdentifier
|
|
];
|
|
|
|
if (
|
|
!isDefined(standardItem) ||
|
|
!isDefined(existingItem) ||
|
|
existingItem.availabilityType === standardItem.availabilityType
|
|
) {
|
|
return undefined;
|
|
}
|
|
|
|
return {
|
|
...existingItem,
|
|
availabilityType: standardItem.availabilityType,
|
|
updatedAt: new Date().toISOString(),
|
|
};
|
|
})
|
|
.filter(isDefined);
|
|
|
|
if (itemsToUpdate.length === 0) {
|
|
this.logger.log(
|
|
`Command menu item availability types already up to date for workspace ${workspaceId}`,
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
this.logger.log(
|
|
`Found ${itemsToUpdate.length} command menu item(s) to update for workspace ${workspaceId}`,
|
|
);
|
|
|
|
if (isDryRun) {
|
|
this.logger.log(
|
|
`[DRY RUN] Would update ${itemsToUpdate.length} command menu item availability type(s) for workspace ${workspaceId}`,
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
const validateAndBuildResult =
|
|
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunLegacyWorkspaceMigration(
|
|
{
|
|
allFlatEntityOperationByMetadataName: {
|
|
commandMenuItem: {
|
|
flatEntityToCreate: [],
|
|
flatEntityToDelete: [],
|
|
flatEntityToUpdate: itemsToUpdate,
|
|
},
|
|
},
|
|
workspaceId,
|
|
applicationUniversalIdentifier:
|
|
twentyStandardFlatApplication.universalIdentifier,
|
|
},
|
|
);
|
|
|
|
if (validateAndBuildResult.status === 'fail') {
|
|
this.logger.error(
|
|
`Failed to update command menu item availability types:\n${JSON.stringify(validateAndBuildResult, null, 2)}`,
|
|
);
|
|
|
|
throw new Error(
|
|
`Failed to update command menu item availability types for workspace ${workspaceId}`,
|
|
);
|
|
}
|
|
|
|
this.logger.log(
|
|
`Successfully updated ${itemsToUpdate.length} command menu item availability type(s) for workspace ${workspaceId}`,
|
|
);
|
|
}
|
|
}
|