app/models/concerns/counter/verifyable.rb in counterwise-0.1.3 vs app/models/concerns/counter/verifyable.rb in counterwise-0.1.4
- old
+ new
@@ -33,15 +33,16 @@
# on_error: raise, log, correct
# Returns the number of incorrect counters
def sample_and_verify scope: -> { all }, samples: 1000, verbose: true, on_error: :raise
incorrect_counters = 0
- counter_range = Counter::Value.minimum(:id)..Counter::Value.maximum(:id)
+ counters = Counter::Value.merge(scope)
+ counter_range = counters.minimum(:id)..counters.maximum(:id)
samples.times do
random_id = rand(counter_range)
- counter = Counter::Value.merge(scope).where("id >= ?", random_id).limit(1).first
+ counter = counters.where("id >= ?", random_id).limit(1).first
next if counter.nil?
if counter.definition.global? || counter.definition.calculated?
puts "➡️ Skipping counter #{counter.name} (#{counter.id})" if verbose
next
@@ -54,10 +55,12 @@
message = "❌ counter #{counter.name} (#{counter.id}) for #{counter.parent_type}##{counter.parent_id} has incorrect counter value. Expected #{counter.value} but got #{counter.count_by_sql}"
case on_error
when :raise then raise Counter::Error.new(message)
when :log then Rails.logger.error message
- when :correct then counter.correct!
+ when :correct
+ counter.correct!
+ puts "🔧 Corrected counter #{counter.id}" if verbose
end
end
sleep 0.1
end
incorrect_counters