-
Notifications
You must be signed in to change notification settings - Fork 7.5k
feat(metadata): deterministic universalIdentifiers for server-generated side-effects #21949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Weiko
wants to merge
10
commits into
main
Choose a base branch
from
c--deterministic-universal-identifiers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d04d2f5
feat(metadata): deterministic universalIdentifiers for server-generat…
Weiko a10f47e
review
Weiko 17d08bd
address review
Weiko 6c3b580
fix
Weiko c70b0a3
self review
Weiko 5fe00c8
review
Weiko 5e0b51e
fix after review
Weiko 372b1ce
Fix fixture
Weiko 06944d4
rename after Charles review
Weiko e9dc017
Merge branch 'main' into c--deterministic-universal-identifiers
Weiko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...src/engine/metadata-modules/index-metadata/utils/compute-flat-index-name-or-throw.util.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { compositeTypeDefinitions, RelationType } from 'twenty-shared/types'; | ||
| import { isDefined } from 'twenty-shared/utils'; | ||
|
|
||
| import { computeCompositeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util'; | ||
| import { computeMorphOrRelationFieldJoinColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-morph-or-relation-field-join-column-name.util'; | ||
| import { isCompositeFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-composite-field-metadata-type.util'; | ||
| import { | ||
| FlatEntityMapsException, | ||
| FlatEntityMapsExceptionCode, | ||
| } from 'src/engine/metadata-modules/flat-entity/exceptions/flat-entity-maps.exception'; | ||
| import { isMorphOrRelationUniversalFlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/utils/is-morph-or-relation-flat-field-metadata.util'; | ||
| import { generateDeterministicIndexNameV2 } from 'src/engine/metadata-modules/index-metadata/utils/generate-deterministic-index-name-v2'; | ||
| import { type UniversalFlatFieldMetadata } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-field-metadata.type'; | ||
| import { type UniversalFlatObjectMetadata } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-object-metadata.type'; | ||
|
|
||
| type FlatIndexFieldForName = { | ||
| fieldMetadataUniversalIdentifier: string; | ||
| order: number; | ||
| subFieldName: string | null; | ||
| }; | ||
|
|
||
| export const computeFlatIndexNameOrThrow = ({ | ||
|
Weiko marked this conversation as resolved.
|
||
| flatObjectMetadata, | ||
| objectFlatFieldMetadatas, | ||
| universalFlatIndexFieldMetadatas, | ||
| isUnique, | ||
| indexWhereClause, | ||
| }: { | ||
| flatObjectMetadata: UniversalFlatObjectMetadata; | ||
| objectFlatFieldMetadatas: UniversalFlatFieldMetadata[]; | ||
| universalFlatIndexFieldMetadatas: FlatIndexFieldForName[]; | ||
| isUnique: boolean; | ||
| indexWhereClause: string | null; | ||
| }): string => { | ||
| const orderedIndexColumnNames = [...universalFlatIndexFieldMetadatas] | ||
| .sort((a, b) => a.order - b.order) | ||
| .map((flatIndexField) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Duplicate index-field column resolution logic introduces drift risk between name generation and schema/index execution paths. A future rule change in one place can silently desynchronize deterministic names from actual indexed columns. Prompt for AI agents |
||
| const relatedFlatFieldMetadata = objectFlatFieldMetadatas.find( | ||
| (flatFieldMetadata) => | ||
| flatFieldMetadata.universalIdentifier === | ||
| flatIndexField.fieldMetadataUniversalIdentifier, | ||
| ); | ||
|
|
||
| if (!isDefined(relatedFlatFieldMetadata)) { | ||
| throw new FlatEntityMapsException( | ||
| 'Could not find flat index field related field in cache', | ||
| FlatEntityMapsExceptionCode.ENTITY_NOT_FOUND, | ||
| ); | ||
| } | ||
|
|
||
| // Composite parent with an explicit sub-field → single sub-column. | ||
| // Composite parent without sub-field falls through to the legacy | ||
| // scalar branch below, which produces a deterministic name based on | ||
| // the parent name (the runner handles the multi-column SQL expansion | ||
| // via isIncludedInUniqueConstraint). | ||
| if ( | ||
| isCompositeFieldMetadataType(relatedFlatFieldMetadata.type) && | ||
| isDefined(flatIndexField.subFieldName) | ||
| ) { | ||
| const property = compositeTypeDefinitions | ||
| .get(relatedFlatFieldMetadata.type) | ||
| ?.properties.find( | ||
| (compositeProperty) => | ||
| compositeProperty.name === flatIndexField.subFieldName, | ||
| ); | ||
|
|
||
| if (!isDefined(property)) { | ||
| throw new FlatEntityMapsException( | ||
| `Composite sub-field "${flatIndexField.subFieldName}" not found on ${relatedFlatFieldMetadata.name}`, | ||
| FlatEntityMapsExceptionCode.ENTITY_NOT_FOUND, | ||
| ); | ||
| } | ||
|
|
||
| return computeCompositeColumnName( | ||
| { | ||
| name: relatedFlatFieldMetadata.name, | ||
| type: relatedFlatFieldMetadata.type, | ||
| }, | ||
| property, | ||
| ); | ||
| } | ||
|
|
||
| const isManyToOneRelation = | ||
| isMorphOrRelationUniversalFlatFieldMetadata(relatedFlatFieldMetadata) && | ||
| relatedFlatFieldMetadata.universalSettings?.relationType === | ||
| RelationType.MANY_TO_ONE; | ||
|
|
||
| return isManyToOneRelation | ||
| ? computeMorphOrRelationFieldJoinColumnName({ | ||
| name: relatedFlatFieldMetadata.name, | ||
| }) | ||
| : relatedFlatFieldMetadata.name; | ||
| }); | ||
|
|
||
| return generateDeterministicIndexNameV2({ | ||
| flatObjectMetadata, | ||
| orderedIndexColumnNames, | ||
| isUnique, | ||
| indexWhereClause, | ||
| }); | ||
| }; | ||
63 changes: 63 additions & 0 deletions
63
...tadata/utils/generate-flat-index-metadata-with-deterministic-universal-identifier.util.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { getIndexUniversalIdentifier } from 'twenty-shared/application'; | ||
|
|
||
| import { computeFlatIndexNameOrThrow } from 'src/engine/metadata-modules/index-metadata/utils/compute-flat-index-name-or-throw.util'; | ||
| import { type UniversalFlatFieldMetadata } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-field-metadata.type'; | ||
| import { type UniversalFlatIndexMetadata } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-index-metadata.type'; | ||
| import { type UniversalFlatObjectMetadata } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-object-metadata.type'; | ||
|
|
||
| type FlatIndexWithoutDeterministicIdentifiers = Omit< | ||
| UniversalFlatIndexMetadata, | ||
| | 'name' | ||
| | 'universalIdentifier' | ||
| | 'objectMetadataUniversalIdentifier' | ||
| | 'applicationUniversalIdentifier' | ||
| | 'universalFlatIndexFieldMetadatas' | ||
| > & { | ||
| universalFlatIndexFieldMetadatas: Array< | ||
| Omit< | ||
| UniversalFlatIndexMetadata['universalFlatIndexFieldMetadatas'][number], | ||
| 'indexMetadataUniversalIdentifier' | ||
| > | ||
| >; | ||
| }; | ||
|
|
||
| export const generateFlatIndexMetadataWithDeterministicUniversalIdentifierOrThrow = | ||
| ({ | ||
| flatObjectMetadata, | ||
| objectFlatFieldMetadatas, | ||
| flatIndex, | ||
| }: { | ||
| flatObjectMetadata: UniversalFlatObjectMetadata; | ||
| objectFlatFieldMetadatas: UniversalFlatFieldMetadata[]; | ||
| flatIndex: FlatIndexWithoutDeterministicIdentifiers; | ||
| }): UniversalFlatIndexMetadata => { | ||
| const name = computeFlatIndexNameOrThrow({ | ||
| flatObjectMetadata, | ||
| objectFlatFieldMetadatas, | ||
| universalFlatIndexFieldMetadatas: | ||
| flatIndex.universalFlatIndexFieldMetadatas, | ||
| isUnique: flatIndex.isUnique, | ||
| indexWhereClause: flatIndex.indexWhereClause, | ||
| }); | ||
|
|
||
| const universalIdentifier = getIndexUniversalIdentifier({ | ||
| applicationUniversalIdentifier: | ||
| flatObjectMetadata.applicationUniversalIdentifier, | ||
| objectUniversalIdentifier: flatObjectMetadata.universalIdentifier, | ||
| name, | ||
| }); | ||
|
|
||
| return { | ||
| ...flatIndex, | ||
| name, | ||
| universalIdentifier, | ||
| objectMetadataUniversalIdentifier: flatObjectMetadata.universalIdentifier, | ||
| applicationUniversalIdentifier: | ||
| flatObjectMetadata.applicationUniversalIdentifier, | ||
| universalFlatIndexFieldMetadatas: | ||
| flatIndex.universalFlatIndexFieldMetadatas.map((indexField) => ({ | ||
| ...indexField, | ||
| indexMetadataUniversalIdentifier: universalIdentifier, | ||
| })), | ||
| }; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Navigation-command universalIdentifier uses the old ad-hoc v5 formula instead of the new shared
getNavigationCommandUniversalIdentifierhelper, producing UIDs inconsistent with the PR's deterministic-ID standard and omitting the owner application UID from the computation.Prompt for AI agents