fix: error in CM when deleting components from DZ (#23002)

This commit is contained in:
Bassel Kanso 2025-02-26 18:04:39 +02:00 committed by GitHub
parent 1f9d3b3bd8
commit 99934b46e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -189,4 +189,38 @@ describe('removeOrphanMorphType', () => {
expect(mockDb.connection).toHaveBeenCalledWith('another_table_with_component_type');
expect(mockDb.connection).not.toHaveBeenCalledWith('table_without_component_type');
});
it('should query the database when there is no target for component and entity join table', async () => {
const mockMetadataMap = new Map([
[
'model1',
{
attributes: {
someRelation: {
type: 'relation',
relation: 'morphToMany',
joinTable: {
name: 'countries_cmps',
joinColumn: { name: 'entity_id', referencedColumn: 'id' },
pivotColumns: ['entity_id', 'cmp_id', 'field', 'component_type'],
},
},
},
},
],
]);
(mockDb.metadata.values as jest.Mock).mockReturnValue(mockMetadataMap.values());
mockDb.metadata.get = jest.fn().mockImplementation((type: string) => {
if (type === 'validType') {
return {}; // Simulate valid metadata
}
throw new Error('Metadata not found'); // Simulate missing metadata
}) as jest.MockedFunction<typeof mockDb.metadata.get>;
await removeOrphanMorphType(mockDb, { pivot: PIVOT_COLUMN });
expect(mockDb.connection).toHaveBeenCalledWith('countries_cmps');
});
});

View File

@ -16,7 +16,6 @@ const isMorphRelationWithPivot = (
attribute.type === 'relation' &&
'relation' in attribute &&
'joinTable' in attribute &&
'target' in attribute &&
'name' in attribute.joinTable &&
'pivotColumns' in attribute.joinTable &&
attribute.joinTable.pivotColumns.includes(pivot)