Fix workspace relation sync (#11963)

## Context

While deploying the IS_NEW_RELATION_ENABLED (we don't compute relation
based on relationMetadata anymore) to existing workspace, I've tested to
run a sync-metadata post feature flag activation. This has raised two
issues:
- the workspaceMigration generator (which is over-complex and should be
refactored later) for fieldMetadata of type RELATION was not handling
settings update properly ;
- we need to delete existing fieldMetadata corresponding to the UUID
foreignKey as they are not needed anymore. This is handled as a 0.53
upgrade command as 0.53 will also come with the full removal of the old
relation system

---------

Co-authored-by: Etienne <45695613+etiennejouan@users.noreply.github.com>
Co-authored-by: prastoin <paul@twenty.com>
This commit is contained in:
Charles Bochet
2025-05-09 19:03:39 +02:00
committed by GitHub
parent 3308ba56b2
commit 8216800a4a
7 changed files with 166 additions and 63 deletions

View File

@ -130,10 +130,6 @@ export class WorkspaceFieldRelationComparator {
throw new Error(`Field ${fieldId} not found in originalObjectMetadata`);
}
if (!standardFieldMetadata) {
throw new Error(`Field ${fieldId} not found in standardObjectMetadata`);
}
for (const difference of differences) {
const property = difference.path[difference.path.length - 1];
@ -175,11 +171,11 @@ export class WorkspaceFieldRelationComparator {
(fieldPropertiesToStringify as readonly string[]).includes(property)
) {
const newValue = this.parseJSONOrString(difference.value);
const oldValue = this.parseJSONOrString(difference.oldValue);
if (property === 'settings' && difference.oldValue) {
const newSettings = newValue as FieldMetadataRelationSettings;
const oldSettings =
difference.oldValue as FieldMetadataRelationSettings;
const oldSettings = oldValue as FieldMetadataRelationSettings;
// Check if the relation type has changed
if (oldSettings.relationType !== newSettings.relationType) {
@ -193,6 +189,10 @@ export class WorkspaceFieldRelationComparator {
}
}
if (!standardFieldMetadata) {
throw new Error(`Field ${fieldId} not found in standardObjectMetadata`);
}
if (relationTypeChange) {
result.push({
action: ComparatorAction.DELETE,

View File

@ -221,8 +221,6 @@ export class WorkspaceMetadataUpdaterService {
[];
let updatedFieldRelationMetadataCollection: FieldMetadataUpdate<FieldMetadataType.RELATION>[] =
[];
let deletedFieldRelationMetadataCollection: FieldMetadataUpdate<FieldMetadataType.RELATION>[] =
[];
/**
* Create field relation metadata
@ -253,20 +251,26 @@ export class WorkspaceMetadataUpdaterService {
}
if (!options || options.actions.includes('delete')) {
deletedFieldRelationMetadataCollection = await this.updateEntities<
FieldMetadataEntity<FieldMetadataType.RELATION>
>(
manager,
FieldMetadataEntity,
storage.fieldRelationMetadataDeleteCollection,
['objectMetadataId', 'workspaceId'],
);
const fieldMetadataRepository =
manager.getRepository(FieldMetadataEntity);
if (storage.fieldRelationMetadataDeleteCollection.length > 0) {
await fieldMetadataRepository.delete(
storage.fieldRelationMetadataDeleteCollection.map(
(field) => field.id,
),
);
}
}
return {
createdFieldRelationMetadataCollection,
updatedFieldRelationMetadataCollection,
deletedFieldRelationMetadataCollection,
deletedFieldRelationMetadataCollection:
storage.fieldRelationMetadataDeleteCollection.map((field) => ({
current: field,
altered: field,
})),
};
}