diff --git a/.changeset/spotty-shoes-exercise.md b/.changeset/spotty-shoes-exercise.md new file mode 100644 index 0000000000..92e0825c9f --- /dev/null +++ b/.changeset/spotty-shoes-exercise.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Support changing location keys on existing entities, in delta mutations diff --git a/plugins/catalog-backend/src/database/DefaultProviderDatabase.test.ts b/plugins/catalog-backend/src/database/DefaultProviderDatabase.test.ts index e9d1bbfcc9..ac361795e1 100644 --- a/plugins/catalog-backend/src/database/DefaultProviderDatabase.test.ts +++ b/plugins/catalog-backend/src/database/DefaultProviderDatabase.test.ts @@ -943,7 +943,7 @@ describe('DefaultProviderDatabase', () => { entity_ref: stringifyEntityRef(entity6), unprocessed_entity: JSON.stringify(entity6), unprocessed_hash: generateStableHash(entity6), - location_key: 'old', // TODO(freben): This should have been updated to "new", but we do not support that yet + location_key: 'new', // managed to update only the location key }, ]); }, diff --git a/plugins/catalog-backend/src/database/DefaultProviderDatabase.ts b/plugins/catalog-backend/src/database/DefaultProviderDatabase.ts index 11e4fd1d40..03f72e06b7 100644 --- a/plugins/catalog-backend/src/database/DefaultProviderDatabase.ts +++ b/plugins/catalog-backend/src/database/DefaultProviderDatabase.ts @@ -237,11 +237,16 @@ export class DefaultProviderDatabase implements ProviderDatabase { const newHash = generateStableHash(deferred.entity); const oldState = oldStates.get(entityRef); if (oldState === undefined) { + // Add any entity that does not exist in the database toAdd.push({ deferred, hash: newHash }); } else if ( - newHash !== oldState.unprocessed_hash || - (deferred.locationKey ?? null) !== (oldState.location_key ?? null) // normalize undefined/null + (deferred.locationKey ?? null) !== (oldState.location_key ?? null) ) { + // Remove and then re-add any entity that exists, but with a different location key + toRemove.push(entityRef); + toAdd.push({ deferred, hash: newHash }); + } else if (newHash !== oldState.unprocessed_hash) { + // Entities with modifications should be pushed through too toUpsert.push({ deferred, hash: newHash }); } }); @@ -294,7 +299,7 @@ export class DefaultProviderDatabase implements ProviderDatabase { // Add any entity that does not exist in the database toAdd.push(upsertItem); } else if ( - (oldRef?.locationKey ?? undefined) !== + (oldRef.locationKey ?? undefined) !== (item.deferred.locationKey ?? undefined) ) { // Remove and then re-add any entity that exists, but with a different location key