Merge pull request #26996 from drodil/catalog_search_trx

fix(catalog): update search table in transaction
This commit is contained in:
Fredrik Adelöw
2024-10-08 11:17:31 +02:00
committed by GitHub
2 changed files with 16 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Update catalog search table in transaction
@@ -218,28 +218,28 @@ export async function performStitching(options: {
.onConflict('entity_id')
.merge(['final_entity', 'hash', 'last_updated_at']);
if (options.strategy.mode === 'deferred') {
const markDeferred = async () => {
if (options.strategy.mode !== 'deferred') {
return;
}
await markDeferredStitchCompleted({
knex: knex,
entityRef,
stitchTicket,
});
}
};
if (amountOfRowsChanged === 0) {
logger.debug(`Entity ${entityRef} is already stitched, skipping write.`);
await markDeferred();
return 'abandoned';
}
// TODO(freben): Search will probably need a similar safeguard against
// race conditions like the final_entities ticket handling above.
// Otherwise, it can be the case that:
// A writes the entity ->
// B writes the entity ->
// B writes search ->
// A writes search
await knex<DbSearchRow>('search').where({ entity_id: entityId }).delete();
await knex.batchInsert('search', searchEntries, BATCH_SIZE);
await knex.transaction(async trx => {
await trx<DbSearchRow>('search').where({ entity_id: entityId }).delete();
await trx.batchInsert('search', searchEntries, BATCH_SIZE);
});
await markDeferred();
return 'changed';
}