Skip to content

Commit

Permalink
Merge pull request #1690 from flanksource/fix/cleanup-soft-deleted-co…
Browse files Browse the repository at this point in the history
…mponents

fix: CleanupSoftDeletedComponents job
  • Loading branch information
moshloop authored Feb 29, 2024
2 parents 4faa86d + 33c78ef commit 6d456b3
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pkg/topology/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ var CleanupSoftDeletedComponents = &job.Job{

seconds := int64(retention.Seconds())

linkedComponents := `
SELECT component_id FROM evidences WHERE component_id IS NOT NULL
UNION
SELECT component_id FROM playbook_runs WHERE component_id IS NOT NULL
`

tx := ctx.Context.DB().Exec(
`DELETE FROM component_relationships WHERE deleted_at < NOW() - interval '1 SECONDS' * ? OR
component_id in (SELECT id FROM components WHERE id NOT IN (SELECT component_id FROM evidences WHERE component_id IS NOT NULL) AND deleted_at < NOW() - interval '1 SECONDS' * ?) OR
relationship_id in (SELECT id FROM components WHERE id NOT IN (SELECT component_id FROM evidences WHERE component_id IS NOT NULL) AND deleted_at < NOW() - interval '1 SECONDS' * ?)
`, seconds, seconds, seconds)
fmt.Sprintf(`
DELETE FROM component_relationships
WHERE deleted_at < NOW() - interval '1 SECONDS' * ? OR
component_id in (SELECT id FROM components WHERE id NOT IN (%s) AND deleted_at < NOW() - interval '1 SECONDS' * ?) OR
relationship_id in (SELECT id FROM components WHERE id NOT IN (%s) AND deleted_at < NOW() - interval '1 SECONDS' * ?)
`, linkedComponents, linkedComponents),
seconds, seconds, seconds)
if tx.Error != nil {
return tx.Error
}
Expand All @@ -40,9 +49,12 @@ var CleanupSoftDeletedComponents = &job.Job{
}

for {
tx = ctx.Context.DB().Exec(`DELETE FROM components WHERE id in (SELECT id FROM components WHERE
id NOT IN (SELECT component_id FROM evidences WHERE component_id IS NOT NULL)
AND deleted_at < NOW() - interval '1 SECONDS' * ? ORDER BY length(path) DESC LIMIT 1000)`, seconds)
tx = ctx.Context.DB().Exec(fmt.Sprintf(`
DELETE FROM components WHERE id in (
SELECT id FROM components WHERE id NOT IN (%s)
AND deleted_at < NOW() - interval '1 SECONDS' * ?
ORDER BY length(path) DESC LIMIT 1000
)`, linkedComponents), seconds)
if tx.Error != nil {
return tx.Error
}
Expand All @@ -52,6 +64,7 @@ var CleanupSoftDeletedComponents = &job.Job{
break
}
}

return nil
},
}
Expand Down

0 comments on commit 6d456b3

Please sign in to comment.