Skip to content

Commit

Permalink
Merge pull request backstage#27957 from backstage/freben/query-limit-…
Browse files Browse the repository at this point in the history
…nulls

fix nulls in by-query
  • Loading branch information
freben authored Dec 2, 2024
2 parents 9d80ffc + e4aab10 commit 83e7780
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-stingrays-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---

Fix a bug where sometimes the `by-query` endpoint could return nulls for entities that were not yet stitched.
45 changes: 45 additions & 0 deletions plugins/catalog-backend/src/service/DefaultEntitiesCatalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,51 @@ describe('DefaultEntitiesCatalog', () => {
).resolves.toEqual(['BB', 'CC', 'AA']); // 'AA' has no title, ends up last
},
);

it.each(databases.eachSupportedId())(
'should silently skip over entities that are not yet stitched, %p',
async databaseId => {
await createDatabase(databaseId);

const entity1 = entityFrom('AA', { uid: 'id1' });
const entity2 = entityFrom('BB', { uid: 'id2' });
await Promise.all([
addEntityToSearch(entity1),
addEntityToSearch(entity2),
]);

const catalog = new DefaultEntitiesCatalog({
database: knex,
logger: mockServices.logger.mock(),
stitcher,
});

await expect(
catalog
.queryEntities({
orderFields: [{ field: 'metadata.uid', order: 'asc' }],
limit: 10,
credentials: mockCredentials.none(),
})
.then(r => r.items.map(e => e.metadata.name)),
).resolves.toEqual(['AA', 'BB']);

// simulate a situation where stitching is not yet complete
await knex('final_entities')
.update({ final_entity: null })
.where({ entity_ref: stringifyEntityRef(entity1) });

await expect(
catalog
.queryEntities({
orderFields: [{ field: 'metadata.uid', order: 'asc' }],
limit: 10,
credentials: mockCredentials.none(),
})
.then(r => r.items.map(e => e.metadata.name)),
).resolves.toEqual(['BB']);
},
);
});

describe('removeEntityByUid', () => {
Expand Down
12 changes: 7 additions & 5 deletions plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,13 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog {
const [prevItemOrderFieldValue, prevItemUid] =
cursor.orderFieldValues || [];

const dbQuery = db('final_entities').leftOuterJoin('search', qb =>
qb
.on('search.entity_id', 'final_entities.entity_id')
.andOnVal('search.key', sortField.field),
);
const dbQuery = db('final_entities')
.leftOuterJoin('search', qb =>
qb
.on('search.entity_id', 'final_entities.entity_id')
.andOnVal('search.key', sortField.field),
)
.whereNotNull('final_entities.final_entity');

if (cursor.filter) {
parseFilter(
Expand Down

0 comments on commit 83e7780

Please sign in to comment.