1be5a0e54a
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. -->
294 lines
10 KiB
TypeScript
294 lines
10 KiB
TypeScript
import { deleteOneFieldMetadata } from 'test/integration/metadata/suites/field-metadata/utils/delete-one-field-metadata.util';
|
|
import { findManyFieldsMetadataQueryFactory } from 'test/integration/metadata/suites/field-metadata/utils/find-many-fields-metadata-query-factory.util';
|
|
import { updateOneFieldMetadata } from 'test/integration/metadata/suites/field-metadata/utils/update-one-field-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 { findManyObjectMetadataQueryFactory } from 'test/integration/metadata/suites/object-metadata/utils/find-many-object-metadata-query-factory.util';
|
|
import { updateOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/update-one-object-metadata.util';
|
|
import { makeMetadataAPIRequest } from 'test/integration/metadata/suites/utils/make-metadata-api-request.util';
|
|
import { FieldMetadataType } from 'twenty-shared/types';
|
|
import { capitalize } from 'twenty-shared/utils';
|
|
|
|
describe('Custom object renaming', () => {
|
|
let listingObjectId = '';
|
|
const uniqueSuffix = Date.now().toString().slice(-8);
|
|
|
|
const STANDARD_OBJECT_RELATIONS = [
|
|
'noteTarget',
|
|
'attachment',
|
|
'taskTarget',
|
|
'timelineActivity',
|
|
];
|
|
|
|
const standardObjectRelationsMap = STANDARD_OBJECT_RELATIONS.reduce(
|
|
(acc, relation) => ({
|
|
...acc,
|
|
[relation]: {
|
|
objectMetadataId: '',
|
|
foreignKeyFieldMetadataId: '',
|
|
relationFieldMetadataId: '',
|
|
relationFieldMetadataUniversalIdentifier: '',
|
|
},
|
|
}),
|
|
{},
|
|
);
|
|
|
|
const standardObjectsGraphqlOperation = findManyObjectMetadataQueryFactory({
|
|
gqlFields: `
|
|
id
|
|
nameSingular
|
|
`,
|
|
input: {
|
|
filter: {},
|
|
paging: { first: 1000 },
|
|
},
|
|
});
|
|
|
|
const fieldsGraphqlOperation = findManyFieldsMetadataQueryFactory({
|
|
gqlFields: `
|
|
id
|
|
name
|
|
label
|
|
type
|
|
universalIdentifier
|
|
object {
|
|
id
|
|
}
|
|
`,
|
|
input: {
|
|
filter: {},
|
|
paging: { first: 1000 },
|
|
},
|
|
});
|
|
|
|
// @ts-expect-error legacy noImplicitAny
|
|
const fillStandardObjectRelationsMapObjectMetadataId = (standardObjects) => {
|
|
STANDARD_OBJECT_RELATIONS.forEach((relation) => {
|
|
// @ts-expect-error legacy noImplicitAny
|
|
standardObjectRelationsMap[relation].objectMetadataId =
|
|
standardObjects.body.data.objects.edges.find(
|
|
// @ts-expect-error legacy noImplicitAny
|
|
(object) =>
|
|
object.node.nameSingular === relation ||
|
|
object.node.nameSingular === `target${relation}`,
|
|
).node.id;
|
|
});
|
|
};
|
|
|
|
it('1. should create one custom object with standard relations', async () => {
|
|
// Arrange
|
|
const standardObjects = await makeMetadataAPIRequest(
|
|
standardObjectsGraphqlOperation,
|
|
);
|
|
|
|
fillStandardObjectRelationsMapObjectMetadataId(standardObjects);
|
|
|
|
const CUSTOM_OBJECT = {
|
|
namePlural: `customObjects${uniqueSuffix}`,
|
|
nameSingular: `customObject${uniqueSuffix}`,
|
|
labelPlural: `Custom Objects ${uniqueSuffix}`,
|
|
labelSingular: `Custom Object ${uniqueSuffix}`,
|
|
description: 'Custom object description',
|
|
icon: 'IconListNumbers',
|
|
isLabelSyncedWithName: false,
|
|
};
|
|
|
|
// Act
|
|
const { data } = await createOneObjectMetadata({
|
|
expectToFail: false,
|
|
input: CUSTOM_OBJECT,
|
|
gqlFields: `
|
|
id
|
|
nameSingular
|
|
`,
|
|
});
|
|
|
|
// Assert
|
|
expect(data.createOneObject.nameSingular).toBe(CUSTOM_OBJECT.nameSingular);
|
|
|
|
listingObjectId = data.createOneObject.id;
|
|
|
|
const fields = await makeMetadataAPIRequest(fieldsGraphqlOperation);
|
|
|
|
const relationFieldsMetadataForListing = fields.body.data.fields.edges
|
|
.filter(
|
|
// @ts-expect-error legacy noImplicitAny
|
|
(field) =>
|
|
(field.node.name === `${CUSTOM_OBJECT.nameSingular}` &&
|
|
FieldMetadataType.RELATION) ||
|
|
(field.node.name ===
|
|
`target${capitalize(CUSTOM_OBJECT.nameSingular)}` &&
|
|
FieldMetadataType.MORPH_RELATION),
|
|
)
|
|
// @ts-expect-error legacy noImplicitAny
|
|
.map((field) => field.node);
|
|
|
|
STANDARD_OBJECT_RELATIONS.forEach((relation) => {
|
|
// relation field
|
|
const relationFieldMetadata = relationFieldsMetadataForListing.find(
|
|
// @ts-expect-error legacy noImplicitAny
|
|
(field) =>
|
|
field.object.id ===
|
|
// @ts-expect-error legacy noImplicitAny
|
|
standardObjectRelationsMap[relation].objectMetadataId,
|
|
);
|
|
|
|
const relationFieldMetadataId = relationFieldMetadata?.id;
|
|
|
|
expect(relationFieldMetadataId).not.toBeUndefined();
|
|
// Reverse system relation fields carry the engine-derived label
|
|
// (capitalized source object nameSingular)
|
|
expect(relationFieldMetadata?.label).toBe(
|
|
capitalize(CUSTOM_OBJECT.nameSingular),
|
|
);
|
|
|
|
// @ts-expect-error legacy noImplicitAny
|
|
standardObjectRelationsMap[relation].relationFieldMetadataId =
|
|
relationFieldMetadataId;
|
|
// @ts-expect-error legacy noImplicitAny
|
|
standardObjectRelationsMap[relation].relationFieldMetadataUniversalIdentifier =
|
|
relationFieldMetadata?.universalIdentifier;
|
|
});
|
|
});
|
|
|
|
it('2. should rename custom object', async () => {
|
|
// Arrange
|
|
const HOUSE_NAME_SINGULAR = `house${uniqueSuffix}`;
|
|
const HOUSE_NAME_PLURAL = `houses${uniqueSuffix}`;
|
|
const HOUSE_LABEL_SINGULAR = `House ${uniqueSuffix}`;
|
|
const HOUSE_LABEL_PLURAL = `Houses ${uniqueSuffix}`;
|
|
|
|
// Act
|
|
const { data } = await updateOneObjectMetadata({
|
|
expectToFail: false,
|
|
gqlFields: `
|
|
nameSingular
|
|
labelSingular
|
|
namePlural
|
|
labelPlural
|
|
`,
|
|
input: {
|
|
idToUpdate: listingObjectId,
|
|
updatePayload: {
|
|
nameSingular: HOUSE_NAME_SINGULAR,
|
|
namePlural: HOUSE_NAME_PLURAL,
|
|
labelSingular: HOUSE_LABEL_SINGULAR,
|
|
labelPlural: HOUSE_LABEL_PLURAL,
|
|
},
|
|
},
|
|
});
|
|
|
|
// Assert
|
|
expect(data.updateOneObject.nameSingular).toBe(HOUSE_NAME_SINGULAR);
|
|
expect(data.updateOneObject.namePlural).toBe(HOUSE_NAME_PLURAL);
|
|
expect(data.updateOneObject.labelSingular).toBe(HOUSE_LABEL_SINGULAR);
|
|
expect(data.updateOneObject.labelPlural).toBe(HOUSE_LABEL_PLURAL);
|
|
|
|
// The reverse morph fields on the standard objects must be renamed in place
|
|
// (name and engine-derived label follow the new object name) while keeping
|
|
// their universal identifier stable, so the rename stays lossless.
|
|
const expectedReverseFieldName = `target${capitalize(HOUSE_NAME_SINGULAR)}`;
|
|
const expectedReverseFieldLabel = capitalize(HOUSE_NAME_SINGULAR);
|
|
const fields = await makeMetadataAPIRequest(fieldsGraphqlOperation);
|
|
|
|
STANDARD_OBJECT_RELATIONS.forEach((relation) => {
|
|
// @ts-expect-error legacy noImplicitAny
|
|
const relationEntry = standardObjectRelationsMap[relation];
|
|
const relationFieldMetadataId = relationEntry.relationFieldMetadataId;
|
|
const relationFieldMetadataUniversalIdentifier =
|
|
relationEntry.relationFieldMetadataUniversalIdentifier;
|
|
|
|
const renamedReverseField = fields.body.data.fields.edges
|
|
// @ts-expect-error legacy noImplicitAny
|
|
.map((field) => field.node)
|
|
// @ts-expect-error legacy noImplicitAny
|
|
.find((field) => field.id === relationFieldMetadataId);
|
|
|
|
expect(renamedReverseField).toBeDefined();
|
|
expect(renamedReverseField.name).toBe(expectedReverseFieldName);
|
|
expect(renamedReverseField.label).toBe(expectedReverseFieldLabel);
|
|
expect(renamedReverseField.universalIdentifier).toBe(
|
|
relationFieldMetadataUniversalIdentifier,
|
|
);
|
|
});
|
|
});
|
|
|
|
it('3. should reject direct deletion of a system side-effect relation field', async () => {
|
|
// @ts-expect-error legacy noImplicitAny
|
|
const timelineActivityRelation = standardObjectRelationsMap['timelineActivity'];
|
|
const relationFieldMetadataId =
|
|
timelineActivityRelation.relationFieldMetadataId;
|
|
|
|
const { errors } = await deleteOneFieldMetadata({
|
|
expectToFail: true,
|
|
input: { idToDelete: relationFieldMetadataId },
|
|
});
|
|
|
|
expect(errors).toBeDefined();
|
|
expect(errors.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('4. should reject direct edition of a system side-effect relation field', async () => {
|
|
// @ts-expect-error legacy noImplicitAny
|
|
const timelineActivityRelation = standardObjectRelationsMap['timelineActivity'];
|
|
const relationFieldMetadataId =
|
|
timelineActivityRelation.relationFieldMetadataId;
|
|
|
|
const { errors } = await updateOneFieldMetadata({
|
|
expectToFail: true,
|
|
input: {
|
|
idToUpdate: relationFieldMetadataId,
|
|
updatePayload: { label: 'Should not be editable' },
|
|
},
|
|
});
|
|
|
|
expect(errors).toBeDefined();
|
|
expect(errors.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('5. should reject a morph relations update payload on a system side-effect relation field', async () => {
|
|
// @ts-expect-error legacy noImplicitAny
|
|
const timelineActivityRelation = standardObjectRelationsMap['timelineActivity'];
|
|
const relationFieldMetadataId =
|
|
timelineActivityRelation.relationFieldMetadataId;
|
|
|
|
// morphRelationsUpdatePayload is not an editable property, so it must be
|
|
// rejected explicitly for engine-owned fields instead of silently creating
|
|
// relation fields and indexes on them
|
|
const { errors } = await updateOneFieldMetadata({
|
|
expectToFail: true,
|
|
input: {
|
|
idToUpdate: relationFieldMetadataId,
|
|
updatePayload: {
|
|
morphRelationsUpdatePayload: [
|
|
{ targetObjectMetadataId: listingObjectId },
|
|
],
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(errors).toBeDefined();
|
|
expect(errors.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('6. should delete custom object', async () => {
|
|
await updateOneObjectMetadata({
|
|
expectToFail: false,
|
|
input: {
|
|
idToUpdate: listingObjectId,
|
|
updatePayload: {
|
|
isActive: false,
|
|
},
|
|
},
|
|
});
|
|
|
|
const { data } = await deleteOneObjectMetadata({
|
|
input: {
|
|
idToDelete: listingObjectId,
|
|
},
|
|
});
|
|
|
|
expect(data.deleteOneObject.id).toBe(listingObjectId);
|
|
});
|
|
});
|