Skip to content

The open alternative to Salesforce, designed for AI.

fix(upgrade): skip 2.20 campaign view columns whose field does not exist yet (#24003)

neo773b5826b2d4e9e
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-20/2-20-workspace-command-1783525261000-add-message-campaign-stat-fields.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-20/2-20-workspace-command-1783525261000-add-message-campaign-stat-fields.command.ts
index 5e6e8830dfc..be8fbf5a1db 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-20/2-20-workspace-command-1783525261000-add-message-campaign-stat-fields.command.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-20/2-20-workspace-command-1783525261000-add-message-campaign-stat-fields.command.ts
@@ -28,8 +28,8 @@ const STAT_FIELD_UNIVERSAL_IDENTIFIERS = [
];
// The allMessageCampaigns view is introduced by this feature, so existing
-// workspaces have no messageCampaign view at all. Create the view and every one
-// of its columns (not just the stat columns) so it materializes end to end.
+// workspaces have no messageCampaign view at all. Create the view and its
+// columns (not just the stat columns) so it materializes end to end.
const CAMPAIGN_VIEW_UNIVERSAL_IDENTIFIER =
CAMPAIGN.views.allMessageCampaigns.universalIdentifier;
@@ -126,13 +126,12 @@ export class AddMessageCampaignStatFieldsCommand extends ProvisionedWorkspaceCom
const viewFieldsToCreate = CAMPAIGN_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.filter(
(universalIdentifier) =>
!isDefined(flatViewFieldMaps.byUniversalIdentifier[universalIdentifier]),
- ).map((universalIdentifier) => {
- const standardViewField = findFlatEntityByUniversalIdentifier<FlatViewField>(
- {
+ ).flatMap((universalIdentifier) => {
+ const standardViewField =
+ findFlatEntityByUniversalIdentifier<FlatViewField>({
flatEntityMaps: standardAllFlatEntityMaps.flatViewFieldMaps,
universalIdentifier,
- },
- );
+ });
if (!isDefined(standardViewField)) {
throw new Error(
@@ -140,7 +139,27 @@ export class AddMessageCampaignStatFieldsCommand extends ProvisionedWorkspaceCom
);
}
- return standardViewField;
+ const fieldUniversalIdentifier =
+ standardAllFlatEntityMaps.flatFieldMetadataMaps.universalIdentifierById[
+ standardViewField.fieldMetadataId
+ ];
+
+ if (!isDefined(fieldUniversalIdentifier)) {
+ return [];
+ }
+
+ // The standard column list keeps growing after 2.20, so skip columns whose
+ // field a later command introduces and backfills.
+ const isFieldAvailable =
+ isDefined(
+ flatFieldMetadataMaps.byUniversalIdentifier[fieldUniversalIdentifier],
+ ) ||
+ fieldsToCreate.some(
+ (fieldToCreate) =>
+ fieldToCreate.universalIdentifier === fieldUniversalIdentifier,
+ );
+
+ return isFieldAvailable ? [standardViewField] : [];
});
const hasMetadataChanges =
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-20/__tests__/2-20-workspace-command-1783525261000-add-message-campaign-stat-fields.command.spec.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-20/__tests__/2-20-workspace-command-1783525261000-add-message-campaign-stat-fields.command.spec.ts
index 0f66772be02..fd873c0fd32 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-20/__tests__/2-20-workspace-command-1783525261000-add-message-campaign-stat-fields.command.spec.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-20/__tests__/2-20-workspace-command-1783525261000-add-message-campaign-stat-fields.command.spec.ts
@@ -29,9 +29,46 @@ const STAT_FIELD_UNIVERSAL_IDENTIFIERS = [
];
const CAMPAIGN_VIEW_UNIVERSAL_IDENTIFIER =
CAMPAIGN.views.allMessageCampaigns.universalIdentifier;
-const CAMPAIGN_VIEW_FIELD_UNIVERSAL_IDENTIFIERS = Object.values(
+
+const CAMPAIGN_FIELD_UNIVERSAL_IDENTIFIER_BY_NAME: Record<string, string> =
+ Object.fromEntries(
+ Object.entries(CAMPAIGN.fields).map(
+ ([fieldName, { universalIdentifier }]) => [fieldName, universalIdentifier],
+ ),
+ );
+
+// The command reads the view columns from the standard application of the
+// running build, and resolves each column's field through fieldMetadataId, so
+// the mocks mirror that indirection. Field ids double as their universal
+// identifier here.
+const CAMPAIGN_VIEW_FIELDS = Object.entries(
CAMPAIGN.views.allMessageCampaigns.viewFields,
-).map((viewField) => viewField.universalIdentifier);
+).map(([fieldName, { universalIdentifier }]) => ({
+ universalIdentifier,
+ fieldUniversalIdentifier: CAMPAIGN_FIELD_UNIVERSAL_IDENTIFIER_BY_NAME[
+ fieldName
+ ],
+}));
+
+const CAMPAIGN_VIEW_FIELD_UNIVERSAL_IDENTIFIERS = CAMPAIGN_VIEW_FIELDS.map(
+ ({ universalIdentifier }) => universalIdentifier,
+);
+
+const NAME_FIELD_UNIVERSAL_IDENTIFIER = CAMPAIGN.fields.name.universalIdentifier;
+const NAME_VIEW_FIELD_UNIVERSAL_IDENTIFIER =
+ CAMPAIGN.views.allMessageCampaigns.viewFields.name.universalIdentifier;
+
+// Everything the allMessageCampaigns view references that a 2.19 workspace
+// already has: neither the stat fields (added by this command) nor name (added
+// by the 2.25 command).
+const PRE_EXISTING_CAMPAIGN_FIELD_UNIVERSAL_IDENTIFIERS =
+ CAMPAIGN_VIEW_FIELDS.map(
+ ({ fieldUniversalIdentifier }) => fieldUniversalIdentifier,
+ ).filter(
+ (universalIdentifier) =>
+ universalIdentifier !== NAME_FIELD_UNIVERSAL_IDENTIFIER &&
+ !STAT_FIELD_UNIVERSAL_IDENTIFIERS.includes(universalIdentifier),
+ );
const buildByUniversalIdentifierMap = (universalIdentifiers: string[]) => ({
byUniversalIdentifier: Object.fromEntries(
@@ -42,6 +79,27 @@ const buildByUniversalIdentifierMap = (universalIdentifiers: string[]) => ({
),
});
+const buildStandardFieldMetadataMaps = (universalIdentifiers: string[]) => ({
+ ...buildByUniversalIdentifierMap(universalIdentifiers),
+ universalIdentifierById: Object.fromEntries(
+ universalIdentifiers.map((universalIdentifier) => [
+ universalIdentifier,
+ universalIdentifier,
+ ]),
+ ),
+});
+
+const buildStandardViewFieldMaps = () => ({
+ byUniversalIdentifier: Object.fromEntries(
+ CAMPAIGN_VIEW_FIELDS.map(
+ ({ universalIdentifier, fieldUniversalIdentifier }) => [
+ universalIdentifier,
+ { universalIdentifier, fieldMetadataId: fieldUniversalIdentifier },
+ ],
+ ),
+ ),
+});
+
describe('AddMessageCampaignStatFieldsCommand', () => {
let command: AddMessageCampaignStatFieldsCommand;
let findApplicationMock: jest.Mock;
@@ -61,15 +119,15 @@ describe('AddMessageCampaignStatFieldsCommand', () => {
computeTwentyStandardApplicationAllFlatEntityMapsMock.mockReturnValue({
allFlatEntityMaps: {
- flatFieldMetadataMaps: buildByUniversalIdentifierMap(
- STAT_FIELD_UNIVERSAL_IDENTIFIERS,
+ flatFieldMetadataMaps: buildStandardFieldMetadataMaps(
+ CAMPAIGN_VIEW_FIELDS.map(
+ ({ fieldUniversalIdentifier }) => fieldUniversalIdentifier,
+ ),
),
flatViewMaps: buildByUniversalIdentifierMap([
CAMPAIGN_VIEW_UNIVERSAL_IDENTIFIER,
]),
- flatViewFieldMaps: buildByUniversalIdentifierMap(
- CAMPAIGN_VIEW_FIELD_UNIVERSAL_IDENTIFIERS,
- ),
+ flatViewFieldMaps: buildStandardViewFieldMaps(),
},
});
@@ -116,8 +174,13 @@ describe('AddMessageCampaignStatFieldsCommand', () => {
});
};
- it('creates missing stat fields, the campaign view, and all campaign view fields', async () => {
- mockWorkspaceCache({});
+ it('creates missing stat fields, the campaign view, and every campaign view field whose field is available', async () => {
+ mockWorkspaceCache({
+ existingFields: [
+ ...PRE_EXISTING_CAMPAIGN_FIELD_UNIVERSAL_IDENTIFIERS,
+ NAME_FIELD_UNIVERSAL_IDENTIFIER,
+ ],
+ });
await runOnWorkspace();
@@ -166,9 +229,34 @@ describe('AddMessageCampaignStatFieldsCommand', () => {
).toHaveLength(CAMPAIGN_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.length);
});
+ it('leaves out view columns whose field does not exist in the workspace yet', async () => {
+ mockWorkspaceCache({
+ existingFields: PRE_EXISTING_CAMPAIGN_FIELD_UNIVERSAL_IDENTIFIERS,
+ });
+
+ await runOnWorkspace();
+
+ const payload =
+ validateBuildAndRunWorkspaceMigrationMock.mock.calls[0][0]
+ .allFlatEntityOperationByMetadataName;
+
+ expect(payload.viewField.flatEntityToCreate).not.toContainEqual(
+ expect.objectContaining({
+ universalIdentifier: NAME_VIEW_FIELD_UNIVERSAL_IDENTIFIER,
+ }),
+ );
+ expect(payload.viewField.flatEntityToCreate).toHaveLength(
+ CAMPAIGN_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.length - 1,
+ );
+ });
+
it('creates only missing pieces when rerun against a partially migrated workspace', async () => {
mockWorkspaceCache({
- existingFields: [CAMPAIGN.fields.sentCount.universalIdentifier],
+ existingFields: [
+ ...PRE_EXISTING_CAMPAIGN_FIELD_UNIVERSAL_IDENTIFIERS,
+ NAME_FIELD_UNIVERSAL_IDENTIFIER,
+ CAMPAIGN.fields.sentCount.universalIdentifier,
+ ],
existingViews: [CAMPAIGN_VIEW_UNIVERSAL_IDENTIFIER],
existingViewFields: [
CAMPAIGN.views.allMessageCampaigns.viewFields.subject