catalog-backend: review fixes

Co-authored-by: Johan Haals <johan.haals@gmail.com>
Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-06-30 14:29:31 +02:00
parent 27e90f969d
commit 3cf22d0a92
2 changed files with 15 additions and 6 deletions
@@ -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,
@@ -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}`,
);