Skip to content

The open alternative to Salesforce, designed for AI.

fix(server): add subFieldName column early in upgrade sequence (#20584)

Charles Bochet09daccc3f9d2
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-3/2-3-instance-command-fast-1747234200000-add-sub-field-name-to-view-sort.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-3/2-3-instance-command-fast-1747234200000-add-sub-field-name-to-view-sort.ts
new file mode 100644
index 00000000000..667ee728c02
--- /dev/null
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-3/2-3-instance-command-fast-1747234200000-add-sub-field-name-to-view-sort.ts
@@ -0,0 +1,37 @@
+import { QueryRunner } from 'typeorm';
+
+import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';
+import { FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';
+
+// The `subFieldName` column is officially introduced in 2.5
+// (see `2-5-instance-command-fast-1778502963794-add-sub-field-name-to-view-sort.ts`),
+// but cross-version upgrades from pre-2.3 fail before they ever reach a 2.5
+// instance command: the 2.3 `DropMessageDirectionFieldCommand` builds a
+// migration that deletes a `fieldMetadata`, and the workspace-migration runner
+// pulls in `flatViewSortMaps` via the metadata cascade graph (`viewSort` →
+// `fieldMetadata` is a many-to-one, so `viewSort` is in `fieldMetadata`'s
+// inverse one-to-many set). That recomputes
+// `WorkspaceFlatViewSortMapCacheService`, which does a `viewSortRepository.find()`
+// — TypeORM emits a SELECT that includes `subFieldName`, the column doesn't
+// exist in DB yet, and the upgrade aborts.
+//
+// Adding the column here ensures it exists before any 2.3 workspace command
+// can trigger that cascade. The 2.5 instance command is idempotent (uses
+// `IF NOT EXISTS`), so cross-upgrade callers see it as a no-op while
+// fresh-from-2.5 install paths still create the column there as before.
+@RegisteredInstanceCommand('2.3.0', 1747234200000)
+export class AddSubFieldNameToViewSortEarlyFastInstanceCommand
+ implements FastInstanceCommand
+{
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(
+ `ALTER TABLE "core"."viewSort" ADD COLUMN IF NOT EXISTS "subFieldName" character varying`,
+ );
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(
+ `ALTER TABLE "core"."viewSort" DROP COLUMN IF EXISTS "subFieldName"`,
+ );
+ }
+}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-5/2-5-instance-command-fast-1778502963794-add-sub-field-name-to-view-sort.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-5/2-5-instance-command-fast-1778502963794-add-sub-field-name-to-view-sort.ts
index 7de59f0051d..760a9d0905f 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-5/2-5-instance-command-fast-1778502963794-add-sub-field-name-to-view-sort.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-5/2-5-instance-command-fast-1778502963794-add-sub-field-name-to-view-sort.ts
@@ -7,15 +7,17 @@ import { FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/
export class AddSubFieldNameToViewSortFastInstanceCommand
implements FastInstanceCommand
{
+ // Idempotent so it can coexist with the early 2.3 instance command
+ // `1747234200000-add-sub-field-name-to-view-sort` (see that file for context).
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
- `ALTER TABLE "core"."viewSort" ADD "subFieldName" character varying`,
+ `ALTER TABLE "core"."viewSort" ADD COLUMN IF NOT EXISTS "subFieldName" character varying`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
- `ALTER TABLE "core"."viewSort" DROP COLUMN "subFieldName"`,
+ `ALTER TABLE "core"."viewSort" DROP COLUMN IF EXISTS "subFieldName"`,
);
}
}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
index e52e98e7eb8..ea774cace4f 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
@@ -20,6 +20,7 @@ import { AddProviderExecutedToAgentMessagePartFastInstanceCommand } from 'src/da
import { BackfillPageLayoutWidgetPositionSlowInstanceCommand } from 'src/database/commands/upgrade-version-command/2-1/2-1-instance-command-slow-1795000002000-backfill-page-layout-widget-position';
import { AddCacheTokensToAgentChatThreadFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-2/2-2-instance-command-fast-1777455269302-add-cache-tokens-to-agent-chat-thread';
import { AddLogoToApplicationFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-2/2-2-instance-command-fast-1777539664664-add-logo-to-application';
+import { AddSubFieldNameToViewSortEarlyFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-3/2-3-instance-command-fast-1747234200000-add-sub-field-name-to-view-sort';
import { AddUpgradeMigrationWorkspaceIdIndexFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-3/2-3-instance-command-fast-1777308014234-add-upgrade-migration-workspace-id-index';
import { AddDeletedAtToAgentChatThreadFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-3/2-3-instance-command-fast-1777682000000-add-deleted-at-to-agent-chat-thread';
import { ConnectionProviderSyncableEntityFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-3/2-3-instance-command-fast-1777896012579-connection-provider-syncable-entity';
@@ -58,6 +59,7 @@ export const INSTANCE_COMMANDS = [
AddGlobalObjectContextToCommandMenuItemAvailabilityTypeFastInstanceCommand,
AddPageLayoutIdToCommandMenuItemFastInstanceCommand,
AddConditionalAvailabilityExpressionToPageLayoutWidgetFastInstanceCommand,
+ AddSubFieldNameToViewSortEarlyFastInstanceCommand,
AddUpgradeMigrationWorkspaceIdIndexFastInstanceCommand,
AddIsPreInstalledToApplicationRegistrationFastInstanceCommand,
AddProviderExecutedToAgentMessagePartFastInstanceCommand,