mirror of
https://github.com/System-End/hackatime.git
synced 2026-04-20 00:35:22 +00:00
Only calculate hash on heartbeats missing hashes
This commit is contained in:
parent
34dc749344
commit
b303861397
1 changed files with 11 additions and 9 deletions
|
|
@ -2,19 +2,21 @@ class OneTime::GenerateUniqueHeartbeatHashesJob < ApplicationJob
|
|||
queue_as :default
|
||||
|
||||
def perform
|
||||
Heartbeat.find_each do |heartbeat|
|
||||
heartbeat.send(:set_fields_hash!)
|
||||
heartbeat.save!
|
||||
ActiveRecord::Base.transaction do
|
||||
# batch update the fields_hash
|
||||
Heartbeat.where(fields_hash: nil).find_each_batch(of: 1000) do |batch|
|
||||
batch.each do |heartbeat|
|
||||
heartbeat.send(:set_fields_hash!)
|
||||
heartbeat.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# error if any two heartbeats have the same fields_hash
|
||||
duplicates = false
|
||||
|
||||
# delete duplicates
|
||||
Heartbeat.group(:fields_hash).having("count(*) > 1").count.each do |fields_hash, count|
|
||||
puts "Duplicate fields_hash: #{fields_hash} (count: #{count})"
|
||||
duplicates = true
|
||||
Heartbeat.where(fields_hash: fields_hash).order(:created_at).offset(1).delete_all
|
||||
puts "Deleted #{count - 1} heartbeat(s)"
|
||||
end
|
||||
|
||||
raise "Duplicate in fields_hash" if duplicates
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue