Skip to content

A platform for community discussion. Free, open, simple.

FIX: prevent cleanup_trackers from unintentionally deleting all trackers (#37619)

Renato Atilio4ecf3a0d1e35
diff --git a/app/models/problem_check.rb b/app/models/problem_check.rb
index 9023fe8fe86..4e9c42908bd 100644
--- a/app/models/problem_check.rb
+++ b/app/models/problem_check.rb
@@ -155,7 +155,10 @@ class ProblemCheck
end
def self.cleanup_trackers
- ProblemCheckTracker.where(identifier:).where.not(target: targets.call).destroy_all
+ current_targets = targets.call
+ return if current_targets.empty?
+
+ ProblemCheckTracker.where(identifier:).where.not(target: current_targets).destroy_all
end
def initialize(target = NO_TARGET)
diff --git a/spec/services/problem_check_spec.rb b/spec/services/problem_check_spec.rb
index b7b57580b9d..711eb56df8f 100644
--- a/spec/services/problem_check_spec.rb
+++ b/spec/services/problem_check_spec.rb
@@ -186,5 +186,14 @@ RSpec.describe ProblemCheck do
ProblemCheckTracker.pluck(:target)
}.from(contain_exactly("foo", "bar", "baz")).to(contain_exactly("foo", "bar"))
end
+
+ context "when targets returns an empty array" do
+ before { MultiTargetCheck.targets = -> { [] } }
+ after { MultiTargetCheck.targets = -> { %w[foo bar] } }
+
+ it "does not delete any trackers" do
+ expect { multi_target_check.cleanup_trackers }.not_to change { ProblemCheckTracker.count }
+ end
+ end
end
end