Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: config_items cleanup #591

Merged
merged 2 commits into from
May 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions jobs/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
adityathebe marked this conversation as resolved.
Show resolved Hide resolved
)
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 {
Expand Down
Loading