Skip to content

Commit

Permalink
fix: config change update trigger (#1103)
Browse files Browse the repository at this point in the history
* fix: config change update trigger

* fix: failing tests
  • Loading branch information
adityathebe authored Sep 27, 2024
1 parent 85635d9 commit 747afc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions tests/config_type_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"context"
"encoding/json"
"fmt"

"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
Expand Down Expand Up @@ -156,7 +157,11 @@ var _ = ginkgo.Describe("Check config_class_summary view", ginkgo.Ordered, func(

// We are making a change and an analysis older than 7 days, it should reflect in the summary
change0 := gen.Generated.Changes[0]
DefaultContext.DB().Model(&models.ConfigChange{}).Where("id = ?", change0.ID).UpdateColumn("created_at", gorm.Expr("NOW() - '15 days'::interval"))
err = DefaultContext.DB().Model(&models.ConfigChange{}).Where("id = ?", change0.ID).UpdateColumns(map[string]any{
"created_at": gorm.Expr("NOW() - '15 days'::interval"),
"details": `{"reason": "test reason"}`,
}).Error
Expect(err).To(BeNil())

analysis0 := gen.Generated.Analysis[0]
DefaultContext.DB().Model(&models.ConfigAnalysis{}).Where("id = ?", analysis0.ID).UpdateColumn("last_observed", gorm.Expr("NOW() - '15 days'::interval"))
Expand All @@ -183,10 +188,10 @@ var _ = ginkgo.Describe("Check config_class_summary view", ginkgo.Ordered, func(
for _, expected := range expectedTypeSummary7d {
i, found := lo.Find(rows7d, func(i summaryRow) bool { return i.Type == expected.Type })
Expect(found).To(BeTrue())
Expect(i.Count).To(Equal(expected.Count))
Expect(i.Changes).To(Equal(expected.Changes))
Expect(i.Health).To(Equal(expected.Health))
Expect(i.Analysis).To(Equal(expected.Analysis))
Expect(i.Count).To(Equal(expected.Count), fmt.Sprintf("count mismatched for type %s", expected.Type))
Expect(i.Changes).To(Equal(expected.Changes), fmt.Sprintf("changes count mismatched for type %s", expected.Type))
Expect(i.Health).To(Equal(expected.Health), fmt.Sprintf("health mismatched for type %s", expected.Type))
Expect(i.Analysis).To(Equal(expected.Analysis), fmt.Sprintf("analysis count mismatched for type %s", expected.Type))
}

// We have separate test for 10 days since 30day and 7day summary is served from materialized views
Expand Down
4 changes: 2 additions & 2 deletions views/030_config_changes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ BEGIN
ELSE count
END,
created_at = CASE
WHEN NEW.details IS DISTINCT FROM OLD.details OR NEW.diff IS DISTINCT FROM OLD.diff THEN NOW()
ELSE COALESCE(NEW.created_at, OLD.created_at)
WHEN NEW.details IS DISTINCT FROM OLD.details OR NEW.diff IS DISTINCT FROM OLD.diff THEN NEW.created_at
ELSE OLD.created_at
END,
created_by = NEW.created_by,
details = NEW.details,
Expand Down

0 comments on commit 747afc2

Please sign in to comment.