Files
twenty/packages/twenty-server/src/engine/metadata-modules/field-metadata/field-metadata.entity.ts
T
Félix Malfait 5a4ebca226 refactor(server): unify the two metadata override mechanisms into one (#22417)
## Unify the two metadata override mechanisms into one

Twenty had **two** override mechanisms:

- **`standardOverrides`** — a bespoke JSONB column on
`objectMetadata`/`fieldMetadata` with typed DTOs and a per-locale
`translations` map, resolved by two i18n-aware resolvers.
- **`OverridableEntity.overrides`** — a flat, registry-driven JSONB blob
on view / view-field / view-field-group / command-menu-item /
page-layout-tab / page-layout-widget, resolved by a plain spread.

This PR collapses them into **one** concept: a single `overrides` blob,
one registry-driven overridable set, one i18n-aware read path, and one
write path (`computeMetadataOverridesBlob`, extracted in #22404).

Object/field **stay on `SyncableEntity`** (not reparented to
`OverridableEntity`) so their `isActive` default stays **FALSE** — this
sidesteps the `isActive` default conflict entirely.

### GraphQL breaking change (accepted)

The `standardOverrides` field is **removed** with no deprecation alias —
`overrides` (a `JSON` scalar) is exposed instead on `Object` and
`Field`. Product confirmed negligible external usage; the front-end has
no hand-written consumer (only generated types), which are regenerated
here.

### Commit structure (reviewable commit-by-commit)

1. **Unified resolver + parity harness** —
`resolveEffectiveEntityProperty` is a strict superset of the three
legacy resolvers; a corpus parity spec compares it against a *frozen
reference* of the old logic across every locale, `isStandardApp` branch
and override shape.
2. **Registry-driven** — object/field presentation props tagged
`isOverridable` + `translatable`; the overridable/translatable sets are
derived from the registry (a test asserts they equal the legacy
hardcoded lists).
3. **Rename + swap + delete** — `standardOverrides` → `overrides` across
entities, DTOs, flat/universal types, producers, the ~12
resolve/write/create/sync call sites, mocks and specs; the reconciler's
two compare entries collapse to one; the three legacy resolvers, both
DTOs and the hardcoded constants/types are deleted.
4. **Migration (zero-downtime, two-phase)** — split across two releases
so a rolling deploy never drops a column a previous-release pod still
`SELECT`s:
   - **2.19 fast** — add the `overrides` column (schema only).
- **2.19 slow** — backfill `overrides` from `standardOverrides` in
`runDataMigration` (kept out of the schema transaction so the bulk write
doesn't hold the ACCESS EXCLUSIVE lock; skipped on fresh installs, which
have no data to copy).
- **2.20 fast** — drop the legacy `standardOverrides` column (gated by
`TWENTY_NEXT_VERSIONS`, so it stays dormant until the instance reaches
2.20).
5. **Front/client-SDK regen** — regenerated metadata GraphQL types.
6. **Integration specs + i18n** — updated the standard object/field
update integration specs + snapshots, and the reworded validator message
catalog entry.

### Rolling-deploy safety

`standardOverrides` is retained through 2.19 and only dropped in 2.20,
mirroring the codebase's deferred-drop convention
(`isUIReadOnly`/`isCustom`). During the 2.19 rollout both columns exist,
so old and new pods coexist without "column does not exist" errors. The
backfill lives in a slow `runDataMigration` (per the
`no-data-mutation-in-fast-instance-command` rule) so it doesn't stall
reads.

### `isActive` guard

The migration never reads or writes `isActive`; the backfill asserts the
active-row count is unchanged and aborts otherwise. Verified on a real
DB: apply + revert preserves the blob **and** the nested `translations`
map, with `isActive` counts identical before/after.

### Verification (local)

- `nx typecheck twenty-server` + `nx typecheck twenty-front` — green
- `nx lint:diff-with-main twenty-server` (oxlint `--type-aware` + oxfmt)
— green
- `nx test twenty-server` — green (unit + parity + registry + migration
tests)
- `nx run twenty-server:test:integration:with-db-reset` — green
- `database:reset` applies the 2.19 phases and leaves **both** columns
present (2.20 drop stays dormant); backfill + revert round-trip verified
on a real DB
- Metadata integration suites (standard object/field update, application
sync) pass end-to-end against the two-column schema
- Metadata GraphQL types regenerated against a booted server; zero
`standardOverrides` references remain in application code (only the
migration commands + the legacy schema baseline)

---------

Co-authored-by: prastoin <paul@twenty.com>
2026-07-02 12:01:15 +02:00

260 lines
9.4 KiB
TypeScript

import {
FieldMetadataDefaultValue,
FieldMetadataOptions,
FieldMetadataSettings,
FieldMetadataType,
} from 'twenty-shared/types';
import {
Check,
Column,
CreateDateColumn,
Entity,
Index,
JoinColumn,
ManyToOne,
OneToMany,
OneToOne,
PrimaryGeneratedColumn,
Relation,
Unique,
UpdateDateColumn,
} from 'typeorm';
import { WasIntroducedInUpgrade } from 'src/engine/core-modules/upgrade/decorators/was-introduced-in-upgrade.decorator';
import { WasRemovedInUpgrade } from 'src/engine/core-modules/upgrade/decorators/was-removed-in-upgrade.decorator';
import { ADD_IS_SYSTEM_SIDE_EFFECT_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-15/is-system-side-effect-upgrade-command-name.constant';
import { ADD_METADATA_OVERRIDES_COLUMN_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-19/add-metadata-overrides-column-upgrade-command-name.constant';
import { RENAME_IS_UI_READ_ONLY_TO_IS_UI_EDITABLE_UPGRADE_COMMAND_NAME } from 'src/engine/metadata-modules/object-metadata/constants/rename-is-ui-read-only-to-is-ui-editable-upgrade-command-name.constant';
import { type FieldMetadataOverrides } from 'src/engine/metadata-modules/field-metadata/types/field-metadata-overrides.type';
import { AssignIfIsGivenFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/types/assign-if-is-given-field-metadata-type.type';
import { AssignTypeIfIsMorphOrRelationFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/types/assign-type-if-is-morph-or-relation-field-metadata-type.type';
import { IndexFieldMetadataEntity } from 'src/engine/metadata-modules/index-metadata/index-field-metadata.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { FieldPermissionEntity } from 'src/engine/metadata-modules/object-permission/field-permission/field-permission.entity';
import { SearchFieldMetadataEntity } from 'src/engine/metadata-modules/search-field-metadata/search-field-metadata.entity';
import { ViewFieldEntity } from 'src/engine/metadata-modules/view-field/entities/view-field.entity';
import { ViewFilterEntity } from 'src/engine/metadata-modules/view-filter/entities/view-filter.entity';
import { ViewEntity } from 'src/engine/metadata-modules/view/entities/view.entity';
import { SyncableEntity } from 'src/engine/workspace-manager/types/syncable-entity.interface';
import { JsonbProperty } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/jsonb-property.type';
import { ViewSortEntity } from 'src/engine/metadata-modules/view-sort/entities/view-sort.entity';
// This entity is used as a reference test case for type utilities in:
// Modifying relations or properties may require updating type test expectations for Typecheck to pass.
@Entity('fieldMetadata')
@Check(
'CHK_FIELD_METADATA_MORPH_RELATION_REQUIRES_MORPH_ID',
`("type" != 'MORPH_RELATION') OR ("type" = 'MORPH_RELATION' AND "morphId" IS NOT NULL)`,
)
@Index('IDX_FIELD_METADATA_RELATION_TARGET_FIELD_METADATA_ID', [
'relationTargetFieldMetadataId',
])
@Index('IDX_FIELD_METADATA_RELATION_TARGET_OBJECT_METADATA_ID', [
'relationTargetObjectMetadataId',
])
@Unique('IDX_FIELD_METADATA_NAME_OBJECT_METADATA_ID_WORKSPACE_ID_UNIQUE', [
'name',
'objectMetadataId',
'workspaceId',
])
@Index('IDX_FIELD_METADATA_OBJECT_METADATA_ID_WORKSPACE_ID', [
'objectMetadataId',
'workspaceId',
])
@Index('IDX_FIELD_METADATA_WORKSPACE_ID', ['workspaceId'])
export class FieldMetadataEntity<
TFieldMetadataType extends FieldMetadataType = FieldMetadataType,
>
extends SyncableEntity
implements Required<FieldMetadataEntity>
{
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ nullable: false, type: 'uuid' })
objectMetadataId: string;
@ManyToOne(() => ObjectMetadataEntity, (object) => object.fields, {
onDelete: 'CASCADE',
nullable: false,
})
@JoinColumn({ name: 'objectMetadataId' })
@Index('IDX_FIELD_METADATA_OBJECT_METADATA_ID', ['objectMetadataId'])
object: Relation<ObjectMetadataEntity>;
@Column({
nullable: false,
type: 'varchar',
})
type: TFieldMetadataType;
@Column({ nullable: false })
name: string;
@Column({ nullable: false })
label: string;
@Column({ nullable: true, type: 'jsonb' })
defaultValue: JsonbProperty<FieldMetadataDefaultValue<TFieldMetadataType>>;
@Column({ nullable: true, type: 'text' })
description: string | null;
@Column({ nullable: true, type: 'varchar' })
icon: string | null;
@WasIntroducedInUpgrade({
upgradeCommandName: ADD_METADATA_OVERRIDES_COLUMN_UPGRADE_COMMAND_NAME,
})
@Column({ type: 'jsonb', nullable: true })
overrides: JsonbProperty<FieldMetadataOverrides> | null;
/**
* @deprecated Superseded by `overrides`; kept readable for pods on the
* previous release during a rolling deploy. Drop deferred to 2-20/README.md.
*/
@Column({ type: 'jsonb', nullable: true })
standardOverrides: WasRemovedInUpgrade<JsonbProperty<FieldMetadataOverrides> | null>;
@Column('jsonb', { nullable: true })
options: JsonbProperty<FieldMetadataOptions<TFieldMetadataType>>;
@Column('jsonb', { nullable: true })
settings: JsonbProperty<FieldMetadataSettings<TFieldMetadataType>>;
@WasRemovedInUpgrade({
upgradeCommandName:
'2.12.0_DropIsCustomFromObjectAndFieldMetadataFastInstanceCommand_1780579070012',
})
@Column({ type: 'boolean', default: false })
isCustom: WasRemovedInUpgrade<boolean>;
@Column({ default: false })
isActive: boolean;
@Column({ default: false })
isSystem: boolean;
@WasIntroducedInUpgrade({
upgradeCommandName: ADD_IS_SYSTEM_SIDE_EFFECT_UPGRADE_COMMAND_NAME,
})
@Column({ nullable: false, default: false, type: 'boolean' })
isSystemSideEffect: boolean;
@WasIntroducedInUpgrade({
upgradeCommandName:
RENAME_IS_UI_READ_ONLY_TO_IS_UI_EDITABLE_UPGRADE_COMMAND_NAME,
})
@Column({ default: true })
isUIEditable: boolean;
// Superseded by isUIEditable. Intentionally NOT @WasRemovedInUpgrade: dropping
// it in 2.13 would break the previous release's pods mid rolling-deploy, since
// they still SELECT it. The WasRemovedInUpgrade<T> type is kept so callers may
// omit it; the decorator + physical drop are deferred (core-team-issues#2542).
@Column({ type: 'boolean', default: false })
isUIReadOnly: WasRemovedInUpgrade<boolean>;
// Is this really nullable ?
@Column({ nullable: true, default: true, type: 'boolean' })
isNullable: boolean | null;
// Derived at flat-entity cache build time from the existence of a
// single-field UNIQUE IndexMetadata covering this field — never persisted
// on this entity. Kept on the type so flat-entity consumers continue to
// read field.isUnique without per-call derivation; the PG column was
// dropped by 1798300000000-drop-field-metadata-is-unique-column.ts.
isUnique: boolean | null;
@Column({ default: false })
isLabelSyncedWithName: boolean;
@Column({ nullable: true, type: 'uuid' })
relationTargetFieldMetadataId: AssignTypeIfIsMorphOrRelationFieldMetadataType<
string,
TFieldMetadataType
>;
@OneToOne(
() => FieldMetadataEntity,
(fieldMetadata) => fieldMetadata.relationTargetFieldMetadataId,
{ nullable: true },
)
@JoinColumn({ name: 'relationTargetFieldMetadataId' })
relationTargetFieldMetadata: AssignTypeIfIsMorphOrRelationFieldMetadataType<
Relation<FieldMetadataEntity>,
TFieldMetadataType
>;
@Column({ nullable: true, type: 'uuid' })
relationTargetObjectMetadataId: AssignTypeIfIsMorphOrRelationFieldMetadataType<
string,
TFieldMetadataType
>;
@ManyToOne(() => ObjectMetadataEntity, {
onDelete: 'CASCADE',
nullable: true,
})
@JoinColumn({ name: 'relationTargetObjectMetadataId' })
relationTargetObjectMetadata: AssignTypeIfIsMorphOrRelationFieldMetadataType<
Relation<ObjectMetadataEntity>,
TFieldMetadataType
>;
@Column({ nullable: true, type: 'uuid' })
morphId: AssignIfIsGivenFieldMetadataType<
string,
TFieldMetadataType,
FieldMetadataType.MORPH_RELATION
>;
@OneToMany(
() => IndexFieldMetadataEntity,
(indexFieldMetadata) => indexFieldMetadata.indexMetadata,
{
cascade: true,
},
)
indexFieldMetadatas: Relation<IndexFieldMetadataEntity[]>;
@CreateDateColumn({ type: 'timestamptz' })
createdAt: Date;
@UpdateDateColumn({ type: 'timestamptz' })
updatedAt: Date;
@OneToMany(
() => FieldPermissionEntity,
(fieldPermission) => fieldPermission.fieldMetadata,
)
fieldPermissions: Relation<FieldPermissionEntity[]>;
@OneToMany(() => ViewFieldEntity, (viewField) => viewField.fieldMetadata)
viewFields: Relation<ViewFieldEntity[]>;
@OneToMany(() => ViewFilterEntity, (viewFilter) => viewFilter.fieldMetadata)
viewFilters: Relation<ViewFilterEntity[]>;
@OneToMany(
() => ViewEntity,
(view) => view.kanbanAggregateOperationFieldMetadata,
)
kanbanAggregateOperationViews: Relation<ViewEntity[]>;
@OneToMany(() => ViewEntity, (view) => view.calendarFieldMetadata)
calendarViews: Relation<ViewEntity[]>;
@OneToMany(() => ViewEntity, (view) => view.mainGroupByFieldMetadata)
mainGroupByFieldMetadataViews: Relation<ViewEntity[]>;
@OneToMany(() => ViewSortEntity, (viewSort) => viewSort.fieldMetadata)
viewSorts: Relation<ViewSortEntity[]>;
@OneToMany(
() => SearchFieldMetadataEntity,
(searchFieldMetadata) => searchFieldMetadata.fieldMetadata,
)
searchFieldMetadatas: Relation<SearchFieldMetadataEntity[]>;
}