Merge pull request #17766 from backstage/freben/better-orphan-control

improve the query for orphan pruning
This commit is contained in:
Fredrik Adelöw
2023-05-12 09:55:27 +02:00
committed by GitHub
2 changed files with 14 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Improve the query for orphan pruning
@@ -33,13 +33,16 @@ export async function deleteOrphanedEntities(options: {
// Limit iterations for sanity
for (let i = 0; i < 100; ++i) {
const candidates = await tx
.with('orphans', orphans =>
.with('orphans', ['entity_id', 'entity_ref'], orphans =>
orphans
.from<DbRefreshStateRow>('refresh_state')
.select('entity_id', 'entity_ref')
.whereNotIn('entity_ref', keep =>
keep.distinct('target_entity_ref').from('refresh_state_references'),
),
.from('refresh_state')
.select('refresh_state.entity_id', 'refresh_state.entity_ref')
.leftOuterJoin(
'refresh_state_references',
'refresh_state_references.target_entity_ref',
'refresh_state.entity_ref',
)
.whereNull('refresh_state_references.target_entity_ref'),
)
.select({
entityId: 'orphans.entity_id',