From d06b803ae8ed9e1470009555f1e773b372e0e6a4 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Tue, 28 May 2024 10:00:27 +0545 Subject: [PATCH 1/2] feat: config_items cleanup --- jobs/cleanup.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/jobs/cleanup.go b/jobs/cleanup.go index 66ead341..a57cbca0 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) + ) 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 { From bcd46f8e6c9308afbcdd6051ee1e1d7544df21e0 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Tue, 28 May 2024 18:45:59 +0545 Subject: [PATCH 2/2] Update jobs/cleanup.go Co-authored-by: Moshe Immerman --- jobs/cleanup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jobs/cleanup.go b/jobs/cleanup.go index a57cbca0..3d450b3a 100644 --- a/jobs/cleanup.go +++ b/jobs/cleanup.go @@ -125,7 +125,7 @@ var CleanupConfigItems = &job.Job{ WHERE deleted_at < NOW() - interval '1 SECONDS' * ? AND id NOT IN (%s) - ORDER BY length(path) + ORDER BY length(path) DESC ) DELETE FROM config_items WHERE id IN (SELECT id FROM ordered_rows)`, linkedConfigsQuery)