From 3cf22d0a92542dfa81526cdb0482d271201cb359 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 30 Jun 2021 14:29:31 +0200 Subject: [PATCH] catalog-backend: review fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Johan Haals Co-authored-by: Fredrik Adelöw Signed-off-by: Patrik Oldsberg --- .../next/database/DefaultProcessingDatabase.test.ts | 8 ++++++-- .../src/next/database/DefaultProcessingDatabase.ts | 13 +++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.test.ts b/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.test.ts index 9a9c09daaf..e449e5580c 100644 --- a/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.test.ts +++ b/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.test.ts @@ -210,7 +210,9 @@ describe('Default Processing Database', () => { relations: [], deferredEntities: [], }), - ).rejects.toThrow(`Processing state not found for ${id}`); + ).rejects.toThrow( + `Conflicting write of processing result for ${id} with location key 'undefined'`, + ); }); }, 60_000, @@ -249,7 +251,9 @@ describe('Default Processing Database', () => { await db.transaction(tx => expect( db.updateProcessedEntity(tx, { ...options, locationKey: 'fail' }), - ).rejects.toThrow(`Processing state not found for ${id}`), + ).rejects.toThrow( + `Conflicting write of processing result for ${id} with location key 'fail'`, + ), ); }, 60_000, diff --git a/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.ts b/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.ts index 53025682ac..1f1ea4bf84 100644 --- a/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.ts +++ b/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.ts @@ -16,7 +16,7 @@ import { Entity, stringifyEntityRef } from '@backstage/catalog-model'; import { JsonObject } from '@backstage/config'; -import { ConflictError, NotFoundError } from '@backstage/errors'; +import { ConflictError } from '@backstage/errors'; import { Knex } from 'knex'; import lodash from 'lodash'; import { v4 as uuid } from 'uuid'; @@ -83,7 +83,9 @@ export class DefaultProcessingDatabase implements ProcessingDatabase { .orWhereNull('location_key'); }); if (refreshResult === 0) { - throw new NotFoundError(`Processing state not found for ${id}`); + throw new ConflictError( + `Conflicting write of processing result for ${id} with location key '${locationKey}'`, + ); } // Schedule all deferred entities for future processing. @@ -372,7 +374,7 @@ export class DefaultProcessingDatabase implements ProcessingDatabase { query = query.onConflict('entity_ref').ignore(); } - const result = await query; + const result: { /* postgres */ rowCount?: number } = await query; if (result.rowCount === 0) { throw new ConflictError( 'Insert failed due to conflicting entity_ref', @@ -395,7 +397,10 @@ export class DefaultProcessingDatabase implements ProcessingDatabase { .select(); // If the location key matches it means we just had a race trigger, which we can safely ignore - if (conflictingEntity.location_key !== locationKey) { + if ( + !conflictingEntity || + conflictingEntity.location_key !== locationKey + ) { this.options.logger.warn( `Detected conflicting entityRef ${entityRef} already referenced by ${conflictingEntity.location_key} and now also ${locationKey}`, );