catalog-backend: reference cleanup fix

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-12-09 17:27:56 +01:00
parent 71147d5c16
commit 24ff18621c
3 changed files with 16 additions and 9 deletions
@@ -462,12 +462,17 @@ describe('DefaultProcessingDatabase', () => {
knexTx<DbRefreshStateReferencesRow>(
'refresh_state_references',
).select(),
).resolves.toEqual([
expect.objectContaining({
source_entity_ref: 'location:default/fakelocation',
target_entity_ref: 'component:default/1',
}),
]);
).resolves.toEqual(
step.expectConflict
? []
: [
// eslint-disable-next-line jest/no-conditional-expect
expect.objectContaining({
source_entity_ref: 'location:default/fakelocation',
target_entity_ref: 'component:default/1',
}),
],
);
expect(mockLogger.error).not.toHaveBeenCalled();
}
@@ -314,7 +314,6 @@ export class DefaultProcessingDatabase implements ProcessingDatabase {
// Keeps track of the entities that we end up inserting to update refresh_state_references afterwards
const stateReferences = new Array<string>();
const conflictingStateReferences = new Array<string>();
// Upsert all of the unprocessed entities into the refresh_state table, by
// their entity ref.
@@ -357,13 +356,11 @@ export class DefaultProcessingDatabase implements ProcessingDatabase {
this.options.logger.warn(
`Detected conflicting entityRef ${entityRef} already referenced by ${conflictingKey} and now also ${locationKey}`,
);
conflictingStateReferences.push(entityRef);
}
}
// Replace all references for the originating entity or source and then create new ones
await tx<DbRefreshStateReferencesRow>('refresh_state_references')
.whereNotIn('target_entity_ref', conflictingStateReferences)
.andWhere({ source_entity_ref: options.sourceEntityRef })
.delete();
await tx.batchInsert(
@@ -161,6 +161,11 @@ export class DefaultProviderDatabase implements ProviderDatabase {
});
}
await tx<DbRefreshStateReferencesRow>('refresh_state_references')
.where('target_entity_ref', entityRef)
.andWhere({ source_key: options.sourceKey })
.delete();
if (ok) {
await tx<DbRefreshStateReferencesRow>(
'refresh_state_references',