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. -->
411 lines
14 KiB
TypeScript
411 lines
14 KiB
TypeScript
import { buildBaseManifest } from 'test/integration/metadata/suites/application/utils/build-base-manifest.util';
|
|
import { buildDefaultObjectManifest } from 'test/integration/metadata/suites/application/utils/build-default-object-manifest.util';
|
|
import { cleanupApplicationAndAppRegistration } from 'test/integration/metadata/suites/application/utils/cleanup-application-and-app-registration.util';
|
|
import { setupApplicationForSync } from 'test/integration/metadata/suites/application/utils/setup-application-for-sync.util';
|
|
import { syncApplication } from 'test/integration/metadata/suites/application/utils/sync-application.util';
|
|
import { findManyObjectMetadataWithIndexes } from 'test/integration/metadata/suites/object-metadata/utils/find-many-object-metadata-with-indexes.util';
|
|
import { findManyObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/find-many-object-metadata.util';
|
|
import { findRoles } from 'test/integration/metadata/suites/role/utils/find-roles.util';
|
|
import { findSkills } from 'test/integration/metadata/suites/skill/utils/find-skills.util';
|
|
import { extractRecordIdsAndDatesAsExpectAny } from 'test/utils/extract-record-ids-and-dates-as-expect-any';
|
|
import {
|
|
type FieldManifest,
|
|
getSystemRelationFieldUniversalIdentifier,
|
|
type Manifest,
|
|
} from 'twenty-shared/application';
|
|
import {
|
|
DEFAULT_RELATIONS_OBJECTS_STANDARD_IDS,
|
|
STANDARD_OBJECTS,
|
|
} from 'twenty-shared/metadata';
|
|
import { FieldMetadataType } from 'twenty-shared/types';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
const TEST_APP_ID = uuidv4();
|
|
const TEST_ROLE_ID = uuidv4();
|
|
const TEST_FIELD_ID = uuidv4();
|
|
const TEST_SKILL_ID = uuidv4();
|
|
|
|
const TEST_OBJECT = buildDefaultObjectManifest({
|
|
applicationUniversalIdentifier: TEST_APP_ID,
|
|
nameSingular: 'ticket',
|
|
namePlural: 'tickets',
|
|
labelSingular: 'Ticket',
|
|
labelPlural: 'Tickets',
|
|
description: 'A support ticket',
|
|
icon: 'IconTicket',
|
|
});
|
|
|
|
const TEST_SKILL = {
|
|
universalIdentifier: TEST_SKILL_ID,
|
|
name: 'test-skill',
|
|
label: 'Test Skill',
|
|
description: 'A skill for testing',
|
|
icon: 'IconBrain',
|
|
content: '# Test Skill\n\nThis is a test skill.',
|
|
};
|
|
|
|
const TEST_FIELD: FieldManifest = {
|
|
universalIdentifier: TEST_FIELD_ID,
|
|
type: FieldMetadataType.TEXT,
|
|
name: 'description',
|
|
label: 'Description',
|
|
description: 'Ticket description',
|
|
icon: 'IconFileDescription',
|
|
objectUniversalIdentifier: TEST_OBJECT.universalIdentifier,
|
|
};
|
|
|
|
const buildManifest = (
|
|
overrides?: Partial<Pick<Manifest, 'fields' | 'skills' | 'objects'>>,
|
|
) =>
|
|
buildBaseManifest({
|
|
appId: TEST_APP_ID,
|
|
roleId: TEST_ROLE_ID,
|
|
overrides: {
|
|
objects: [TEST_OBJECT],
|
|
skills: [TEST_SKILL],
|
|
fields: [TEST_FIELD],
|
|
...overrides,
|
|
},
|
|
});
|
|
|
|
describe('syncApplication', () => {
|
|
beforeEach(async () => {
|
|
await setupApplicationForSync({
|
|
applicationUniversalIdentifier: TEST_APP_ID,
|
|
name: 'Test Application',
|
|
description: 'A test application',
|
|
sourcePath: 'test-sync',
|
|
});
|
|
}, 60000);
|
|
|
|
afterEach(async () => {
|
|
await cleanupApplicationAndAppRegistration({
|
|
applicationUniversalIdentifier: TEST_APP_ID,
|
|
});
|
|
});
|
|
|
|
it('should return workspace migration actions on initial sync then on second sync with field rename and new role', async () => {
|
|
const { data: firstSyncData } = await syncApplication({
|
|
manifest: buildManifest(),
|
|
expectToFail: false,
|
|
});
|
|
|
|
expect(firstSyncData).toMatchSnapshot(
|
|
extractRecordIdsAndDatesAsExpectAny(firstSyncData),
|
|
);
|
|
|
|
// Verify database state after first sync
|
|
const { objects: objectsAfterSync } = await findManyObjectMetadata({
|
|
input: {
|
|
filter: {},
|
|
paging: { first: 100 },
|
|
},
|
|
gqlFields:
|
|
'id nameSingular namePlural labelSingular labelPlural description icon',
|
|
expectToFail: false,
|
|
});
|
|
|
|
const ticketObject = objectsAfterSync.find(
|
|
(obj) => obj.nameSingular === 'ticket',
|
|
);
|
|
|
|
expect(ticketObject).toBeDefined();
|
|
expect(ticketObject).toMatchObject({
|
|
nameSingular: 'ticket',
|
|
namePlural: 'tickets',
|
|
labelSingular: 'Ticket',
|
|
labelPlural: 'Tickets',
|
|
description: 'A support ticket',
|
|
icon: 'IconTicket',
|
|
});
|
|
|
|
const objects = await findManyObjectMetadataWithIndexes({
|
|
expectToFail: false,
|
|
});
|
|
|
|
const fieldsAfterSync = objects.find(
|
|
(o) => o.universalIdentifier === TEST_OBJECT.universalIdentifier,
|
|
)?.fieldsList;
|
|
|
|
const descriptionField = fieldsAfterSync?.find(
|
|
(f) => f.universalIdentifier === TEST_FIELD_ID,
|
|
);
|
|
|
|
expect(descriptionField).toBeDefined();
|
|
expect(descriptionField).toMatchObject({
|
|
name: 'description',
|
|
label: 'Description',
|
|
type: FieldMetadataType.TEXT,
|
|
description: 'Ticket description',
|
|
icon: 'IconFileDescription',
|
|
});
|
|
|
|
const { data: rolesAfterSync } = await findRoles({
|
|
gqlFields: 'id label description universalIdentifier',
|
|
expectToFail: false,
|
|
});
|
|
|
|
const testRole = rolesAfterSync.getRoles.find(
|
|
(role) => role.universalIdentifier === TEST_ROLE_ID,
|
|
);
|
|
|
|
const { data: skillsAfterSync } = await findSkills({
|
|
gqlFields: 'id name label description content icon',
|
|
expectToFail: false,
|
|
input: undefined,
|
|
});
|
|
|
|
const testSkill = skillsAfterSync.skills.find(
|
|
(skill) => skill.name === 'test-skill',
|
|
);
|
|
|
|
expect(testRole).toBeDefined();
|
|
expect(testRole).toMatchObject({
|
|
label: 'Test Role',
|
|
description: 'A test role',
|
|
});
|
|
|
|
expect(testSkill).toBeDefined();
|
|
expect(testSkill).toMatchObject({
|
|
name: 'test-skill',
|
|
label: 'Test Skill',
|
|
description: 'A skill for testing',
|
|
icon: 'IconBrain',
|
|
content: '# Test Skill\n\nThis is a test skill.',
|
|
});
|
|
}, 60000);
|
|
|
|
// The snapshot masks every identifier as Any<String>, so it cannot catch a
|
|
// regression in the deterministic universal identifiers of the default
|
|
// relations. This test recomputes them and asserts the emitted migration
|
|
// actions carry them exactly, on both sides of each relation.
|
|
it('should provision the four engine-owned default relations on initial sync', async () => {
|
|
const NAME_PLURAL_BY_STANDARD_OBJECT_NAME_SINGULAR = {
|
|
timelineActivity: 'timelineActivities',
|
|
attachment: 'attachments',
|
|
noteTarget: 'noteTargets',
|
|
taskTarget: 'taskTargets',
|
|
} as const;
|
|
|
|
const { data: syncData } = await syncApplication({
|
|
manifest: buildManifest(),
|
|
expectToFail: false,
|
|
});
|
|
|
|
type FlatFieldEntity = {
|
|
universalIdentifier: string;
|
|
name?: string;
|
|
type?: string;
|
|
isSystem?: boolean;
|
|
isSystemSideEffect?: boolean;
|
|
morphId?: string | null;
|
|
objectMetadataUniversalIdentifier?: string;
|
|
relationTargetObjectMetadataUniversalIdentifier?: string | null;
|
|
relationTargetFieldMetadataUniversalIdentifier?: string | null;
|
|
universalSettings?: { joinColumnName?: string } | null;
|
|
universalFlatIndexFieldMetadatas?: {
|
|
fieldMetadataUniversalIdentifier: string;
|
|
}[];
|
|
};
|
|
type FlatEntityAction = {
|
|
type: string;
|
|
metadataName: string;
|
|
flatEntity: FlatFieldEntity;
|
|
// A relation field create action carries its reverse field inline.
|
|
relatedUniversalFlatFieldMetadata?: FlatFieldEntity;
|
|
};
|
|
|
|
const actions = (
|
|
syncData as {
|
|
syncApplication: { actions: FlatEntityAction[] };
|
|
}
|
|
).syncApplication.actions;
|
|
|
|
const fieldCreateActionByUniversalIdentifier = Object.fromEntries(
|
|
actions
|
|
.filter(
|
|
(action) =>
|
|
action.metadataName === 'fieldMetadata' && action.type === 'create',
|
|
)
|
|
.map((action) => [action.flatEntity.universalIdentifier, action]),
|
|
);
|
|
const indexCreateActions = actions.filter(
|
|
(action) => action.metadataName === 'index' && action.type === 'create',
|
|
);
|
|
|
|
for (const standardObjectNameSingular of DEFAULT_RELATIONS_OBJECTS_STANDARD_IDS) {
|
|
const standardObjectUniversalIdentifier =
|
|
STANDARD_OBJECTS[standardObjectNameSingular].universalIdentifier;
|
|
const standardObjectNamePlural =
|
|
NAME_PLURAL_BY_STANDARD_OBJECT_NAME_SINGULAR[
|
|
standardObjectNameSingular
|
|
];
|
|
|
|
const forwardUniversalIdentifier =
|
|
getSystemRelationFieldUniversalIdentifier({
|
|
applicationUniversalIdentifier: TEST_APP_ID,
|
|
objectUniversalIdentifier: TEST_OBJECT.universalIdentifier,
|
|
relationTargetObjectUniversalIdentifier:
|
|
standardObjectUniversalIdentifier,
|
|
});
|
|
const reverseUniversalIdentifier =
|
|
getSystemRelationFieldUniversalIdentifier({
|
|
applicationUniversalIdentifier: TEST_APP_ID,
|
|
objectUniversalIdentifier: standardObjectUniversalIdentifier,
|
|
relationTargetObjectUniversalIdentifier:
|
|
TEST_OBJECT.universalIdentifier,
|
|
});
|
|
|
|
// Forward RELATION field on the ticket, named after the standard
|
|
// object's namePlural, under the name-free universal identifier.
|
|
const forwardCreateAction =
|
|
fieldCreateActionByUniversalIdentifier[forwardUniversalIdentifier];
|
|
|
|
expect(forwardCreateAction?.flatEntity).toMatchObject({
|
|
name: standardObjectNamePlural,
|
|
type: FieldMetadataType.RELATION,
|
|
// Engine-owned (isSystemSideEffect) but not isSystem: the engine
|
|
// rename/delete cascades must pass the validator system-field gates.
|
|
isSystem: false,
|
|
isSystemSideEffect: true,
|
|
objectMetadataUniversalIdentifier: TEST_OBJECT.universalIdentifier,
|
|
relationTargetObjectMetadataUniversalIdentifier:
|
|
standardObjectUniversalIdentifier,
|
|
relationTargetFieldMetadataUniversalIdentifier:
|
|
reverseUniversalIdentifier,
|
|
});
|
|
|
|
// Reverse MORPH_RELATION field on the standard object, carried inline on
|
|
// the forward create action, under the name-free deterministic universal
|
|
// identifier, with the engine-pinned targetMorphId and the join column.
|
|
expect(
|
|
forwardCreateAction?.relatedUniversalFlatFieldMetadata,
|
|
).toMatchObject({
|
|
name: 'targetTicket',
|
|
type: FieldMetadataType.MORPH_RELATION,
|
|
isSystem: false,
|
|
isSystemSideEffect: true,
|
|
universalIdentifier: reverseUniversalIdentifier,
|
|
morphId:
|
|
STANDARD_OBJECTS[standardObjectNameSingular].morphIds.targetMorphId
|
|
.morphId,
|
|
objectMetadataUniversalIdentifier: standardObjectUniversalIdentifier,
|
|
relationTargetObjectMetadataUniversalIdentifier:
|
|
TEST_OBJECT.universalIdentifier,
|
|
relationTargetFieldMetadataUniversalIdentifier:
|
|
forwardUniversalIdentifier,
|
|
universalSettings: expect.objectContaining({
|
|
joinColumnName: 'targetTicketId',
|
|
}),
|
|
});
|
|
|
|
// Engine-owned join-column index backing the reverse field.
|
|
const reverseJoinColumnIndexAction = indexCreateActions.find((action) =>
|
|
action.flatEntity.universalFlatIndexFieldMetadatas?.some(
|
|
(indexFieldMetadata) =>
|
|
indexFieldMetadata.fieldMetadataUniversalIdentifier ===
|
|
reverseUniversalIdentifier,
|
|
),
|
|
);
|
|
|
|
expect(reverseJoinColumnIndexAction).toBeDefined();
|
|
expect(reverseJoinColumnIndexAction?.flatEntity).toMatchObject({
|
|
isSystemSideEffect: true,
|
|
objectMetadataUniversalIdentifier: standardObjectUniversalIdentifier,
|
|
});
|
|
}
|
|
}, 60000);
|
|
|
|
it('should delete old field and create equivalent one when field universalIdentifier changes', async () => {
|
|
const originalFieldId = uuidv4();
|
|
const updatedFieldId = uuidv4();
|
|
|
|
const testObject = buildDefaultObjectManifest({
|
|
applicationUniversalIdentifier: TEST_APP_ID,
|
|
nameSingular: 'ticket',
|
|
namePlural: 'tickets',
|
|
labelSingular: 'Ticket',
|
|
labelPlural: 'Tickets',
|
|
description: 'A support ticket',
|
|
icon: 'IconTicket',
|
|
});
|
|
|
|
const baseField: FieldManifest = {
|
|
universalIdentifier: originalFieldId,
|
|
type: FieldMetadataType.TEXT,
|
|
name: 'description',
|
|
label: 'Description',
|
|
description: 'Ticket description',
|
|
icon: 'IconFileDescription',
|
|
objectUniversalIdentifier: testObject.universalIdentifier,
|
|
};
|
|
|
|
const { data: firstSyncData } = await syncApplication({
|
|
manifest: buildBaseManifest({
|
|
appId: TEST_APP_ID,
|
|
roleId: TEST_ROLE_ID,
|
|
overrides: {
|
|
objects: [testObject],
|
|
fields: [baseField],
|
|
},
|
|
}),
|
|
expectToFail: false,
|
|
});
|
|
|
|
expect(firstSyncData).toMatchSnapshot(
|
|
extractRecordIdsAndDatesAsExpectAny(firstSyncData),
|
|
);
|
|
|
|
const { data: secondSyncData } = await syncApplication({
|
|
manifest: buildBaseManifest({
|
|
appId: TEST_APP_ID,
|
|
roleId: TEST_ROLE_ID,
|
|
overrides: {
|
|
objects: [testObject],
|
|
fields: [
|
|
{
|
|
...baseField,
|
|
universalIdentifier: updatedFieldId,
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
expectToFail: false,
|
|
});
|
|
|
|
expect(secondSyncData).toMatchSnapshot(
|
|
extractRecordIdsAndDatesAsExpectAny(secondSyncData),
|
|
);
|
|
}, 60000);
|
|
|
|
it('should create a TEXT field on the standard Company object', async () => {
|
|
const companyFieldId = uuidv4();
|
|
|
|
const manifest = buildManifest({
|
|
skills: [],
|
|
objects: [],
|
|
fields: [
|
|
{
|
|
universalIdentifier: companyFieldId,
|
|
type: FieldMetadataType.TEXT,
|
|
name: 'industry',
|
|
label: 'Industry',
|
|
description: 'The industry of the company',
|
|
icon: 'IconBuildingFactory2',
|
|
objectUniversalIdentifier:
|
|
STANDARD_OBJECTS.company.universalIdentifier,
|
|
},
|
|
],
|
|
});
|
|
|
|
const { data: syncData } = await syncApplication({
|
|
manifest,
|
|
expectToFail: false,
|
|
});
|
|
|
|
expect(syncData).toMatchSnapshot(
|
|
extractRecordIdsAndDatesAsExpectAny(syncData),
|
|
);
|
|
}, 60000);
|
|
});
|