Skip to content

The open alternative to Salesforce, designed for AI.

fix(upgrade): tolerate missing fields when creating message record pages (#26447)

Félix Malfait3017493e382b
diff --git a/packages/twenty-server/src/database/commands/command-runners/__tests__/workspace-iterator.service.spec.ts b/packages/twenty-server/src/database/commands/command-runners/__tests__/workspace-iterator.service.spec.ts
index e0230129746..08805f030a3 100644
--- a/packages/twenty-server/src/database/commands/command-runners/__tests__/workspace-iterator.service.spec.ts
+++ b/packages/twenty-server/src/database/commands/command-runners/__tests__/workspace-iterator.service.spec.ts
@@ -7,6 +7,9 @@ import { WorkspaceIteratorService } from 'src/database/commands/command-runners/
import { type WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
import { type WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';
import { type WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
+import { ViewExceptionCode } from 'src/engine/metadata-modules/view/exceptions/view.exception';
+import { EMPTY_ORCHESTRATOR_FAILURE_REPORT } from 'src/engine/workspace-manager/workspace-migration/constant/empty-orchestrator-failure-report.constant';
+import { WorkspaceMigrationBuilderException } from 'src/engine/workspace-manager/workspace-migration/exceptions/workspace-migration-builder-exception';
describe('WorkspaceIteratorService', () => {
let workspaceCacheService: jest.Mocked<
@@ -95,4 +98,61 @@ describe('WorkspaceIteratorService', () => {
workspaceCacheService.evictWorkspaceFromLocalCache,
).toHaveBeenCalledWith('workspace-1');
});
+
+ it.each([1, 200])(
+ 'logs all %s validation errors and the stack when a migration cannot be built',
+ async (errorCount) => {
+ const error = new WorkspaceMigrationBuilderException(
+ {
+ status: 'fail',
+ report: {
+ ...EMPTY_ORCHESTRATOR_FAILURE_REPORT(),
+ viewField: [
+ {
+ metadataName: 'viewField',
+ type: 'create',
+ flatEntityMinimalInformation: {
+ universalIdentifier: 'missing-field-view',
+ fieldMetadataUniversalIdentifier: 'missing-field',
+ },
+ errors: Array.from({ length: errorCount }, (_, index) => ({
+ code: ViewExceptionCode.INVALID_VIEW_DATA,
+ message: `Field metadata not found ${index}`,
+ })),
+ },
+ ],
+ },
+ },
+ 'Failed to sync the message record page',
+ );
+
+ const report = await service.iterate({
+ workspaceIds: ['workspace-1', 'workspace-2'],
+ callback: async ({ workspaceId }) => {
+ if (workspaceId === 'workspace-1') {
+ throw error;
+ }
+ },
+ });
+
+ expect(report.fail).toEqual([{ workspaceId: 'workspace-1', error }]);
+ expect(report.success).toEqual([{ workspaceId: 'workspace-2' }]);
+ expect(Logger.prototype.error).toHaveBeenCalledWith(
+ expect.stringContaining('Field metadata not found'),
+ );
+ expect(Logger.prototype.error).toHaveBeenCalledWith(
+ expect.stringContaining('missing-field'),
+ );
+ expect(Logger.prototype.error).toHaveBeenCalledWith(
+ expect.stringContaining('workspace-1'),
+ );
+ expect(Logger.prototype.error).toHaveBeenCalledWith(
+ `Migration validation report for workspace workspace-1: ${JSON.stringify(error.failedWorkspaceMigrationBuildResult.report, null, 2)}`,
+ );
+ expect(Logger.prototype.error).toHaveBeenCalledWith(
+ 'Error in workspace workspace-1: Failed to sync the message record page',
+ error.stack,
+ );
+ },
+ );
});
diff --git a/packages/twenty-server/src/database/commands/command-runners/workspace-iterator.service.ts b/packages/twenty-server/src/database/commands/command-runners/workspace-iterator.service.ts
index 6c8c7264c66..8f5951dc2e1 100644
--- a/packages/twenty-server/src/database/commands/command-runners/workspace-iterator.service.ts
+++ b/packages/twenty-server/src/database/commands/command-runners/workspace-iterator.service.ts
@@ -16,6 +16,7 @@ import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.ent
import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
+import { WorkspaceMigrationBuilderException } from 'src/engine/workspace-manager/workspace-migration/exceptions/workspace-migration-builder-exception';
import { WorkspaceMigrationRunnerException } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/exceptions/workspace-migration-runner.exception';
export type WorkspaceIteratorShard = {
@@ -153,6 +154,12 @@ export class WorkspaceIteratorService {
error.stack,
);
+ if (error instanceof WorkspaceMigrationBuilderException) {
+ this.logger.error(
+ `Migration validation report for workspace ${workspaceId}: ${JSON.stringify(error.failedWorkspaceMigrationBuildResult.report, null, 2)}`,
+ );
+ }
+
if (error instanceof WorkspaceMigrationRunnerException && error.errors) {
for (const [label, innerError] of Object.entries(error.errors)) {
if (!isDefined(innerError)) continue;
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789757500000-sync-message-record-page.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789757500000-sync-message-record-page.command.ts
index e6bc078f7cc..1e38973be49 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789757500000-sync-message-record-page.command.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789757500000-sync-message-record-page.command.ts
@@ -77,6 +77,7 @@ export class SyncMessageRecordPageCommand extends ProvisionedWorkspaceCommandRun
const {
flatObjectMetadataMaps,
+ flatFieldMetadataMaps,
flatViewMaps,
flatViewFieldMaps,
flatViewFieldGroupMaps,
@@ -85,6 +86,7 @@ export class SyncMessageRecordPageCommand extends ProvisionedWorkspaceCommandRun
flatPageLayoutWidgetMaps,
} = await this.workspaceCacheService.getOrRecompute(workspaceId, [
'flatObjectMetadataMaps',
+ 'flatFieldMetadataMaps',
'flatViewMaps',
'flatViewFieldMaps',
'flatViewFieldGroupMaps',
@@ -181,6 +183,22 @@ export class SyncMessageRecordPageCommand extends ProvisionedWorkspaceCommandRun
standardFlatEntityMaps: standardAllFlatEntityMaps.flatViewFieldMaps,
existingFlatEntityMaps: flatViewFieldMaps,
universalIdentifiers: FIELDS_VIEW_FIELD_UNIVERSAL_IDENTIFIERS,
+ }).filter((viewField) => {
+ if (
+ isDefined(
+ flatFieldMetadataMaps.byUniversalIdentifier[
+ viewField.fieldMetadataUniversalIdentifier
+ ],
+ )
+ ) {
+ return true;
+ }
+
+ this.logger.warn(
+ `Skipping message record page view field ${viewField.universalIdentifier} for workspace ${workspaceId}: field metadata ${viewField.fieldMetadataUniversalIdentifier} does not exist`,
+ );
+
+ return false;
});
const pageLayoutsToCreate =
@@ -231,14 +249,11 @@ export class SyncMessageRecordPageCommand extends ProvisionedWorkspaceCommandRun
`${isDryRun ? '[DRY RUN] ' : ''}Workspace ${workspaceId}: ${viewsToCreate.length} view(s), ${viewFieldGroupsToCreate.length} field group(s), ${viewFieldsToCreate.length} view field(s), ${pageLayoutsToCreate.length} layout(s), ${pageLayoutTabsToCreate.length} tab(s), ${pageLayoutWidgetsToCreate.length} widget(s)`,
);
- if (isDryRun) {
- return;
- }
-
const result =
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunLegacyWorkspaceMigration(
{
isSystemBuild: true,
+ dryRun: isDryRun,
workspaceId,
applicationUniversalIdentifier:
twentyStandardFlatApplication.universalIdentifier,
@@ -285,7 +300,7 @@ export class SyncMessageRecordPageCommand extends ProvisionedWorkspaceCommandRun
}
this.logger.log(
- `Synced the message record page for workspace ${workspaceId}`,
+ `${isDryRun ? '[DRY RUN] Validated' : 'Synced'} the message record page for workspace ${workspaceId}`,
);
}
}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/__tests__/2-42-workspace-command-1789757500000-sync-message-record-page.command.spec.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/__tests__/2-42-workspace-command-1789757500000-sync-message-record-page.command.spec.ts
index c60968248aa..ceb6bba8765 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/__tests__/2-42-workspace-command-1789757500000-sync-message-record-page.command.spec.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/__tests__/2-42-workspace-command-1789757500000-sync-message-record-page.command.spec.ts
@@ -2,14 +2,18 @@ import {
STANDARD_OBJECTS,
STANDARD_PAGE_LAYOUT_UNIVERSAL_IDENTIFIERS,
} from 'twenty-shared/metadata';
+import { type FeatureFlagKey } from 'twenty-shared/types';
import { type WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';
import { SyncMessageRecordPageCommand } from 'src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789757500000-sync-message-record-page.command';
import { type ApplicationService } from 'src/engine/core-modules/application/application.service';
+import { createEmptyFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/constant/create-empty-flat-entity-maps.constant';
+import { type FlatViewField } from 'src/engine/metadata-modules/flat-view-field/types/flat-view-field.type';
import { WidgetConfigurationType } from 'src/engine/metadata-modules/page-layout-widget/enums/widget-configuration-type.type';
import { type 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 { type WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service';
+import { FlatViewFieldValidatorService } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/services/flat-view-field-validator.service';
jest.mock(
'src/engine/workspace-manager/twenty-standard-application/utils/twenty-standard-application-all-flat-entity-maps.constant',
@@ -45,6 +49,18 @@ const HOME_TAB_UNIVERSAL_IDENTIFIER =
const FIELDS_WIDGET_UNIVERSAL_IDENTIFIER =
MESSAGE_RECORD_PAGE.tabs.home.widgets.fields.universalIdentifier;
+const STANDARD_MAPS = jest
+ .requireActual<
+ typeof import('src/engine/workspace-manager/twenty-standard-application/utils/twenty-standard-application-all-flat-entity-maps.constant')
+ >(
+ 'src/engine/workspace-manager/twenty-standard-application/utils/twenty-standard-application-all-flat-entity-maps.constant',
+ )
+ .computeTwentyStandardApplicationAllFlatEntityMaps({
+ now: '2026-09-23T00:00:00.000Z',
+ workspaceId: WORKSPACE_ID,
+ twentyStandardApplicationId: STANDARD_APPLICATION.id,
+ }).allFlatEntityMaps;
+
const STANDARD_FIELDS_VIEW_ID = '20202020-0000-0000-0000-000000000010';
const EXISTING_FIELDS_VIEW_ID = '20202020-0000-0000-0000-000000000011';
@@ -101,7 +117,10 @@ describe('SyncMessageRecordPageCommand', () => {
),
flatViewFieldMaps: buildMaps(
FIELDS_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.map(
- (universalIdentifier) => ({ universalIdentifier }),
+ (universalIdentifier) =>
+ STANDARD_MAPS.flatViewFieldMaps.byUniversalIdentifier[
+ universalIdentifier
+ ]!,
),
),
flatPageLayoutMaps: buildMaps([
@@ -148,6 +167,7 @@ describe('SyncMessageRecordPageCommand', () => {
const mockWorkspaceCache = ({
hasMessageObject = true,
+ missingFieldIdentifiers = [] as string[],
existingViews = [] as {
id: string;
universalIdentifier: string;
@@ -171,6 +191,15 @@ describe('SyncMessageRecordPageCommand', () => {
? [{ universalIdentifier: MESSAGE.universalIdentifier }]
: [],
),
+ flatFieldMetadataMaps: {
+ byUniversalIdentifier: Object.fromEntries(
+ Object.entries(
+ STANDARD_MAPS.flatFieldMetadataMaps.byUniversalIdentifier,
+ ).filter(
+ ([identifier]) => !missingFieldIdentifiers.includes(identifier),
+ ),
+ ),
+ },
flatViewMaps: buildMaps(existingViews),
flatViewFieldGroupMaps: buildMaps(
existingViewFieldGroups.map((universalIdentifier) => ({
@@ -250,6 +279,169 @@ describe('SyncMessageRecordPageCommand', () => {
}
});
+ it.each([
+ {
+ description: 'createdBy',
+ missingFieldIdentifiers: [MESSAGE.fields.createdBy.universalIdentifier],
+ omittedViewFields: 1,
+ },
+ {
+ description: 'the historical system fields',
+ missingFieldIdentifiers: [
+ MESSAGE.fields.createdBy.universalIdentifier,
+ MESSAGE.fields.updatedBy.universalIdentifier,
+ MESSAGE.fields.position.universalIdentifier,
+ MESSAGE.fields.searchVector.universalIdentifier,
+ ],
+ omittedViewFields: 1,
+ },
+ {
+ description: 'other referenced fields',
+ missingFieldIdentifiers: [
+ MESSAGE.fields.text.universalIdentifier,
+ MESSAGE.fields.receivedAt.universalIdentifier,
+ ],
+ omittedViewFields: 2,
+ },
+ ])(
+ 'creates a valid page without backfilling missing $description',
+ async ({ missingFieldIdentifiers, omittedViewFields }) => {
+ mockWorkspaceCache({ missingFieldIdentifiers });
+
+ const { flatFieldMetadataMaps } = await getOrRecomputeMock();
+ const viewFieldValidator = new FlatViewFieldValidatorService();
+ const standardView =
+ STANDARD_MAPS.flatViewMaps.byUniversalIdentifier[
+ FIELDS_VIEW_UNIVERSAL_IDENTIFIER
+ ]!;
+ const validateViewField = (viewField: FlatViewField) =>
+ viewFieldValidator.validateFlatViewFieldCreation({
+ flatEntityToValidate: viewField,
+ optimisticFlatEntityMapsAndRelatedFlatEntityMaps: {
+ ...STANDARD_MAPS,
+ flatFieldMetadataMaps,
+ flatViewFieldMaps: createEmptyFlatEntityMaps(),
+ flatViewMaps: buildMaps([
+ { ...standardView, viewFieldUniversalIdentifiers: [] },
+ ]),
+ },
+ remainingFlatEntityMapsToValidate: createEmptyFlatEntityMaps(),
+ additionalCacheDataMaps: {
+ featureFlagsMap: {} as Record<FeatureFlagKey, boolean>,
+ },
+ buildOptions: {
+ isSystemBuild: true,
+ applicationUniversalIdentifier:
+ STANDARD_APPLICATION.universalIdentifier,
+ },
+ workspaceId: WORKSPACE_ID,
+ });
+
+ const originalViewFields = FIELDS_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.map(
+ (identifier) =>
+ STANDARD_MAPS.flatViewFieldMaps.byUniversalIdentifier[identifier]!,
+ );
+
+ expect(
+ originalViewFields.flatMap(
+ (viewField) => validateViewField(viewField).errors,
+ ),
+ ).toHaveLength(omittedViewFields);
+
+ await runOnWorkspace();
+
+ const payload = getMigrationPayload();
+ const viewFields: FlatViewField[] = payload.viewField.flatEntityToCreate;
+
+ expect(viewFields).toHaveLength(
+ FIELDS_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.length - omittedViewFields,
+ );
+ expect(
+ viewFields.flatMap((viewField) => validateViewField(viewField).errors),
+ ).toEqual([]);
+ expect(payload.fieldMetadata).toBeUndefined();
+ expect(payload.pageLayout.flatEntityToCreate).toHaveLength(1);
+ expect(payload.pageLayoutTab.flatEntityToCreate).toHaveLength(1);
+ expect(payload.pageLayoutWidget.flatEntityToCreate).toHaveLength(1);
+ expect(loggerWarnMock).toHaveBeenCalledTimes(omittedViewFields);
+
+ mockWorkspaceCache({
+ missingFieldIdentifiers,
+ existingViews: [
+ {
+ id: EXISTING_FIELDS_VIEW_ID,
+ universalIdentifier: FIELDS_VIEW_UNIVERSAL_IDENTIFIER,
+ },
+ ],
+ existingViewFieldGroups: FIELDS_VIEW_FIELD_GROUP_UNIVERSAL_IDENTIFIERS,
+ existingViewFields: viewFields.map(
+ (viewField) => viewField.universalIdentifier,
+ ),
+ existingPageLayouts: [
+ { universalIdentifier: PAGE_LAYOUT_UNIVERSAL_IDENTIFIER },
+ ],
+ existingPageLayoutTabs: [
+ { universalIdentifier: HOME_TAB_UNIVERSAL_IDENTIFIER },
+ ],
+ existingPageLayoutWidgets: [FIELDS_WIDGET_UNIVERSAL_IDENTIFIER],
+ });
+
+ await runOnWorkspace();
+
+ expect(
+ validateBuildAndRunLegacyWorkspaceMigrationMock,
+ ).toHaveBeenCalledTimes(1);
+ },
+ );
+
+ it('adds a previously omitted view field on an explicit rerun after its metadata is restored', async () => {
+ const createdByViewFieldIdentifier =
+ MESSAGE.views.messageRecordPageFields.viewFields.createdBy
+ .universalIdentifier;
+
+ mockWorkspaceCache({
+ existingViews: [
+ {
+ id: EXISTING_FIELDS_VIEW_ID,
+ universalIdentifier: FIELDS_VIEW_UNIVERSAL_IDENTIFIER,
+ },
+ ],
+ existingViewFieldGroups: FIELDS_VIEW_FIELD_GROUP_UNIVERSAL_IDENTIFIERS,
+ existingViewFields: FIELDS_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.filter(
+ (identifier) => identifier !== createdByViewFieldIdentifier,
+ ),
+ existingPageLayouts: [
+ { universalIdentifier: PAGE_LAYOUT_UNIVERSAL_IDENTIFIER },
+ ],
+ existingPageLayoutTabs: [
+ { universalIdentifier: HOME_TAB_UNIVERSAL_IDENTIFIER },
+ ],
+ existingPageLayoutWidgets: [FIELDS_WIDGET_UNIVERSAL_IDENTIFIER],
+ });
+
+ await runOnWorkspace();
+
+ const payload = getMigrationPayload();
+
+ expect(payload.viewField.flatEntityToCreate).toEqual([
+ expect.objectContaining({
+ universalIdentifier: createdByViewFieldIdentifier,
+ fieldMetadataUniversalIdentifier:
+ MESSAGE.fields.createdBy.universalIdentifier,
+ }),
+ ]);
+ expect(payload.fieldMetadata).toBeUndefined();
+ for (const metadataName of [
+ 'view',
+ 'viewFieldGroup',
+ 'pageLayout',
+ 'pageLayoutTab',
+ 'pageLayoutWidget',
+ ]) {
+ expect(payload[metadataName].flatEntityToCreate).toEqual([]);
+ }
+ });
+
it('binds the fields widget to the fields view the workspace already holds', async () => {
mockWorkspaceCache({
existingViews: [
@@ -419,7 +611,7 @@ describe('SyncMessageRecordPageCommand', () => {
).not.toHaveBeenCalled();
});
- it('reports what it would do on a dry run without running the migration', async () => {
+ it('validates the migration on a dry run without executing it', async () => {
mockWorkspaceCache();
await runOnWorkspace(true);
@@ -429,24 +621,27 @@ describe('SyncMessageRecordPageCommand', () => {
);
expect(
validateBuildAndRunLegacyWorkspaceMigrationMock,
- ).not.toHaveBeenCalled();
+ ).toHaveBeenCalledWith(expect.objectContaining({ dryRun: true }));
});
- it('throws when the migration fails, so the upgrade does not record success', async () => {
- const failedResult = { status: 'fail' };
-
- mockWorkspaceCache();
- validateBuildAndRunLegacyWorkspaceMigrationMock.mockResolvedValue(
- failedResult,
- );
-
- await expect(runOnWorkspace()).rejects.toThrow(
- `Failed to sync the message record page for workspace ${WORKSPACE_ID}`,
- );
- // the typed exception is what carries the failed build result to callers
- await expect(runOnWorkspace()).rejects.toMatchObject({
- name: 'WorkspaceMigrationBuilderException',
- failedWorkspaceMigrationBuildResult: failedResult,
- });
- });
+ it.each([false, true])(
+ 'throws when validation fails (dryRun=%s)',
+ async (dryRun) => {
+ const failedResult = { status: 'fail' };
+
+ mockWorkspaceCache();
+ validateBuildAndRunLegacyWorkspaceMigrationMock.mockResolvedValue(
+ failedResult,
+ );
+
+ await expect(runOnWorkspace(dryRun)).rejects.toThrow(
+ `Failed to sync the message record page for workspace ${WORKSPACE_ID}`,
+ );
+ // the typed exception is what carries the failed build result to callers
+ await expect(runOnWorkspace(dryRun)).rejects.toMatchObject({
+ name: 'WorkspaceMigrationBuilderException',
+ failedWorkspaceMigrationBuildResult: failedResult,
+ });
+ },
+ );
});