From 3ab57c624a3b476a83b935c4f505e25fa9385db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Sun, 24 Nov 2024 15:57:58 +0100 Subject: [PATCH] support delta update to location key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/spotty-shoes-exercise.md | 5 +++++ .../src/database/DefaultProviderDatabase.test.ts | 2 +- .../src/database/DefaultProviderDatabase.ts | 11 ++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 .changeset/spotty-shoes-exercise.md 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