Files
twenty/packages/twenty-server/test/integration/metadata/suites/object-metadata/create-one-object-metadata-deterministic-field-universal-identifiers.integration-spec.ts
T
Paul Rastoin 1be5a0e54a System side effect relations (#22882)
Closes twentyhq/core-team-issues#2667

## What

Default relations to the standard relation objects
(`timelineActivities`, `attachments`, `noteTargets`, `taskTargets`) are
now fully owned by the **metadata side-effect engine**. Neither the API
transpilers nor the SDK manifest builder provision them anymore: any
object creation, rename or deletion — regardless of the caller — goes
through the same engine handlers.

## Why

- Provisioning was duplicated across the API path and the SDK manifest
builder, with diverging behavior.
- Universal identifiers of relation fields were derived from object
**names**, so renaming an object mutated them and forced lossy
delete+create cycles on manifest sync.

## How

### Engine-owned lifecycle (side-effect handlers)

- `objectSystemRelationsOnCreate`: provisions the 8 forward/reverse
relation fields (+ join column indexes) when an object is created.
- `objectSystemRelationsOnUpdate`: renames the reverse morph fields
(`target<ObjectName>`) when their host object is renamed — a lossless
`fieldMetadata.update`.
- `objectSystemSideEffectsOnDelete`: cascades deletion of engine-owned
fields/indexes when the object is deleted.
- The API transpilers and the SDK `buildManifest` no longer inject these
fields; `isSystemSideEffect: true` marks engine-owned entities, guarded
by a granular property allowlist (only `isActive` is user-editable) and
excluded from manifest deletion inference.

### Name-free deterministic universal identifiers

New `getSystemRelationFieldUniversalIdentifier({
applicationUniversalIdentifier, objectUniversalIdentifier,
relationTargetObjectUniversalIdentifier })` in `twenty-shared`, exported
from `twenty-sdk/define`. The identifier is keyed on the two **object**
identifiers instead of field names (direction encoded by argument
order), so object renames never mutate relation field identifiers. It
cannot collide with the name-based `getFieldUniversalIdentifier`
derivation (field names cannot contain `:`).

### twenty-standard re-owned

All 48 forward/reverse system relation field declarations in
`STANDARD_OBJECTS` now pin the derived name-free identifiers (computed
inline via the shared util) and carry `isSystemSideEffect: true`, with
labels/icons declared explicitly (translated via `msg`).
`twenty-standard` is projected as if the engine had generated these
fields itself.

### 2.23 upgrade commands

- `reconcile-system-relation-field-universal-identifier`: structurally
matches existing default relation fields per workspace and backfills the
derived universal identifiers, `isSystemSideEffect` flags, and standard
labels/icons.
- `upgrade-people-data-labs-application`: upgrades installed PDL apps to
`1.0.7` right after the backfill to close the desync window (its views
reference the re-derived identifiers).

### Misc

- `people-data-labs` `1.0.7`: views temporarily pin the new derived
identifiers (TODO: import from the next released `twenty-sdk`).
- `UpgradeStatusModule` split out of `UpgradeModule` so the application
module cluster can consume upgrade status/migration services without
importing the versioned command bundles (fixes a require cycle that
crashed boot).
- Docs: `system-fields.mdx` documents the system relation fields and
their resolver; `sync-and-recovery.mdx` plan example no longer shows
auto-injected relations.

## Known red CI

`people-data-labs (dockerhub-latest)` fails by design until the 2.23
server image is published: the app pins the new identifiers which only
exist on a 2.23 server. The `local` leg (server built from this branch)
is green.

## System fields are no longer manifest-authorable (accepted regression)

The manifest converter no longer derives `isSystem` /
`isSystemSideEffect` from field names. Reserved-system-named manifest
fields (`id`, `createdAt`, `updatedAt`, `deletedAt`, `createdBy`,
`updatedBy`, `position`, `searchVector`) are now skipped at conversion
time when they carry the exact derived universal identifier (keeps
manifests built with older SDKs installable), and rejected with
`INVALID_INPUT` when they pin any other identifier. System fields are
therefore fully engine-canonical: nothing a manifest carries can produce
a system-flagged entity anymore.

**Accepted regression**: a manifest can no longer influence system field
properties at all. Previously a (legacy) re-declaration could shape them
at creation — which actually produced broken system fields, e.g. a
nullable, non-unique `id` — and could still toggle the allowlisted
`isActive` / `universalSettings` afterwards. We consider this acceptable
for now: per-app granularity over system fields will be reintroduced
later through the **override framework**, which will also settle update
semantics by forbidding direct updates over `isSystemSideEffect: true`
entities and expressing divergence as overrides.

`isSystemSideEffect`-only entities (the default relation fields
provisioned by this PR) still have no engine-level update guard (see
Follow-up below); that part is unchanged and also lands with the
overrides refactor.

## Follow-up

`isSystemSideEffect` field update/delete guards intentionally live at
the API layer (`sanitize-raw-update-field-input.ts`,
`from-delete-field-input-...util.ts`) rather than in the engine-level
`FlatFieldMetadataValidatorService`. Moving them into the validator
requires threading operation-origin (direct field mutation vs engine
cascade) through the migration matrix, otherwise legitimate object
rename/delete cascades (which carry `isSystemBuild=false`) would be
rejected. Tracked in twentyhq/core-team-issues#2671.

<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/22882?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
2026-07-20 18:53:24 +02:00

195 lines
6.0 KiB
TypeScript

import { findManyApplications } from 'test/integration/graphql/utils/find-many-applications.util';
import { findManyFieldsMetadata } from 'test/integration/metadata/suites/field-metadata/utils/find-many-fields-metadata.util';
import { createOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/create-one-object-metadata.util';
import { deleteOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/delete-one-object-metadata.util';
import {
getFieldUniversalIdentifier,
getSystemRelationFieldUniversalIdentifier,
} from 'twenty-shared/application';
import { capitalize, isDefined } from 'twenty-shared/utils';
const SYSTEM_FIELD_NAMES = [
'id',
'name',
'createdAt',
'updatedAt',
'deletedAt',
'createdBy',
'updatedBy',
'position',
'searchVector',
];
const DEFAULT_RELATION_FORWARD_FIELD_NAMES = [
'timelineActivities',
'attachments',
'noteTargets',
'taskTargets',
];
const OBJECT_NAME_SINGULAR = 'rocketForUniversalIdentifier';
type FetchedField = {
id: string;
name: string;
universalIdentifier: string;
relation: {
targetFieldMetadata: {
id: string;
name: string;
universalIdentifier: string;
};
targetObjectMetadata: {
id: string;
nameSingular: string;
universalIdentifier: string;
};
} | null;
};
describe('Custom object creation deterministic field universal identifiers', () => {
let createdObjectMetadataId: string;
let objectUniversalIdentifier: string;
let applicationUniversalIdentifier: string;
let fetchedFields: FetchedField[];
beforeAll(async () => {
const { data: createObjectData } = await createOneObjectMetadata({
expectToFail: false,
input: {
nameSingular: OBJECT_NAME_SINGULAR,
namePlural: `${OBJECT_NAME_SINGULAR}s`,
labelSingular: 'Rocket For Universal Identifier',
labelPlural: 'Rockets For Universal Identifier',
icon: 'IconRocket',
},
gqlFields: 'id universalIdentifier applicationId',
});
createdObjectMetadataId = createObjectData.createOneObject.id;
objectUniversalIdentifier =
createObjectData.createOneObject.universalIdentifier;
const { data: findManyApplicationsData } = await findManyApplications({
expectToFail: false,
});
const workspaceCustomApplication =
findManyApplicationsData.findManyApplications.find(
(application) =>
application.id === createObjectData.createOneObject.applicationId,
);
if (!isDefined(workspaceCustomApplication)) {
throw new Error(
'Could not resolve the application of the created custom object',
);
}
applicationUniversalIdentifier =
workspaceCustomApplication.universalIdentifier;
const { fields } = await findManyFieldsMetadata({
expectToFail: false,
input: {
filter: { objectMetadataId: { eq: createdObjectMetadataId } },
paging: { first: 100 },
},
gqlFields: `
id
name
universalIdentifier
relation {
targetFieldMetadata { id name universalIdentifier }
targetObjectMetadata { id nameSingular universalIdentifier }
}
`,
});
fetchedFields = fields.map((edge: { node: FetchedField }) => edge.node);
});
afterAll(async () => {
await deleteOneObjectMetadata({
expectToFail: false,
input: { idToDelete: createdObjectMetadataId },
});
});
it.each(SYSTEM_FIELD_NAMES)(
'should derive the %s system field universal identifier deterministically',
(systemFieldName) => {
const systemField = fetchedFields.find(
(field) => field.name === systemFieldName,
);
expect(systemField).toBeDefined();
expect(systemField?.universalIdentifier).toBe(
getFieldUniversalIdentifier({
applicationUniversalIdentifier,
objectUniversalIdentifier,
name: systemFieldName,
}),
);
},
);
it.each(DEFAULT_RELATION_FORWARD_FIELD_NAMES)(
'should derive the %s default relation field universal identifiers deterministically',
async (forwardFieldName) => {
const forwardField = fetchedFields.find(
(field) => field.name === forwardFieldName,
);
expect(forwardField).toBeDefined();
const standardRelationObject =
forwardField?.relation?.targetObjectMetadata;
expect(standardRelationObject).toBeDefined();
if (!isDefined(standardRelationObject)) {
return;
}
const { fields: standardObjectFieldEdges } = await findManyFieldsMetadata(
{
expectToFail: false,
input: {
filter: { objectMetadataId: { eq: standardRelationObject.id } },
paging: { first: 1000 },
},
gqlFields: 'id name universalIdentifier',
},
);
const storedReverseFieldName = `target${capitalize(OBJECT_NAME_SINGULAR)}`;
const reverseField = standardObjectFieldEdges
.map((edge: { node: FetchedField }) => edge.node)
.find((field: FetchedField) => field.name === storedReverseFieldName);
expect(reverseField).toBeDefined();
// Both sides of a system relation use the name-free deterministic
// identifier (host + relation target object, direction encoded by
// swapping them) so an object rename stays a lossless update.
expect(forwardField?.universalIdentifier).toBe(
getSystemRelationFieldUniversalIdentifier({
applicationUniversalIdentifier,
objectUniversalIdentifier: objectUniversalIdentifier,
relationTargetObjectUniversalIdentifier:
standardRelationObject.universalIdentifier,
}),
);
expect(reverseField?.universalIdentifier).toBe(
getSystemRelationFieldUniversalIdentifier({
applicationUniversalIdentifier,
objectUniversalIdentifier:
standardRelationObject.universalIdentifier,
relationTargetObjectUniversalIdentifier: objectUniversalIdentifier,
}),
);
},
);
});