Skip to content

Commit

Permalink
fix: updating creation & deletion timestamps
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
adityathebe committed Jul 9, 2024
1 parent c3189b0 commit ef412aa
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ 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).Unix() != lo.FromPtr(existing.DeletedAt).Unix() {
updates["deleted_at"] = ci.DeletedAt
updates["delete_reason"] = ci.DeleteReason
} else {
} else if existing.DeletedAt != nil {
updates["deleted_at"] = gorm.Expr("NULL")
updates["delete_reason"] = gorm.Expr("NULL")
}
Expand Down Expand Up @@ -163,7 +163,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
}

Expand Down

0 comments on commit ef412aa

Please sign in to comment.