From 39abbb9a7a0ce46b8c74ac0e56563f438aad2960 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 29 May 2024 12:08:19 +0545 Subject: [PATCH] feat: config_items cleanup (#591) * feat: config_items cleanup * Update jobs/cleanup.go Co-authored-by: Moshe Immerman --------- Co-authored-by: Moshe Immerman --- jobs/cleanup.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/jobs/cleanup.go b/jobs/cleanup.go index 66ead341..3d450b3a 100644 --- a/jobs/cleanup.go +++ b/jobs/cleanup.go @@ -110,19 +110,25 @@ var CleanupConfigItems = &job.Job{ breakParentRelationshipQuery := fmt.Sprintf(` UPDATE config_items SET parent_id = NULL - WHERE id IN (%s) AND parent_id IS NOT NULL AND deleted_at < NOW() - interval '7 days'`, + WHERE id IN (%s) AND parent_id IS NOT NULL AND deleted_at < NOW() - interval '1 SECONDS' * ?`, linkedConfigsQuery) - if tx := ctx.Context.DB().Exec(breakParentRelationshipQuery); tx.Error != nil { + if tx := ctx.Context.DB().Exec(breakParentRelationshipQuery, seconds); tx.Error != nil { return fmt.Errorf("failed to remove config parent relationships: %w", tx.Error) } else { ctx.Tracef("removed %d config parent relationships", tx.RowsAffected) } configDeleteQuery := fmt.Sprintf(` + WITH ordered_rows AS ( + SELECT id + FROM config_items + WHERE + deleted_at < NOW() - interval '1 SECONDS' * ? AND + id NOT IN (%s) + ORDER BY length(path) DESC + ) DELETE FROM config_items - WHERE - deleted_at < NOW() - interval '1 SECONDS' * ? AND - id NOT IN (%s)`, linkedConfigsQuery) + WHERE id IN (SELECT id FROM ordered_rows)`, linkedConfigsQuery) for { tx := ctx.Context.DB().Exec(configDeleteQuery, seconds) if tx.Error != nil {