From 6ba4e2b4c89ec2147ed2dc34d9eef05df4c92c98 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Tue, 9 Jul 2024 20:13:23 +0545 Subject: [PATCH] fix: updating creation & deletion timestamps * for configs with no creation timestamp (example: Kubernetes cluster), we were always updating the created_at field. More so, the field was set to zero value. * for configs that weren't deleted, we were always updating the deleted_at field to a NULL value. Now, we only set to NULL if the existing config was actually deleted before. This fixes the accuracy of the updated rows counter. --- db/update.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/db/update.go b/db/update.go index 6d9dc6b4..46c82f59 100644 --- a/db/update.go +++ b/db/update.go @@ -106,10 +106,11 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult, ci, existing *model updates := make(map[string]interface{}) changes := make([]*models.ConfigChange, 0) - if ci.DeletedAt != existing.DeletedAt { + if lo.FromPtr(ci.DeletedAt) != lo.FromPtr(existing.DeletedAt) { updates["deleted_at"] = ci.DeletedAt updates["delete_reason"] = ci.DeleteReason - } else { + } else if existing.DeletedAt != nil && ci.DeletedAt == nil { + // item was previously deleted but is now being restored updates["deleted_at"] = gorm.Expr("NULL") updates["delete_reason"] = gorm.Expr("NULL") } @@ -163,7 +164,9 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult, ci, existing *model updates["path"] = ci.Path } - if ci.CreatedAt != existing.CreatedAt { + if ci.CreatedAt.IsZero() && existing.CreatedAt.IsZero() { + updates["created_at"] = gorm.Expr("NOW()") + } else if ci.CreatedAt != existing.CreatedAt && !ci.CreatedAt.IsZero() { updates["created_at"] = ci.CreatedAt }