Story#119627029 - more efficient update of checksums of existing records

(cherry picked from commit 8127181)
This commit is contained in:
Vladimir Krylov 2016-06-30 11:48:18 +03:00
parent 71547d91a3
commit 2e4e07503f

View file

@ -8,17 +8,15 @@ namespace :legal_doc do
desc 'Generate hash' desc 'Generate hash'
task generate_hash: :environment do task generate_hash: :environment do
start = Time.zone.now.to_f start = Time.zone.now.to_f
puts '-----> Generating unique hash for legal documents' puts '-----> Generating unique hash for legal documents'
count = 0 count = 0
LegalDocument.find_each do |x| LegalDocument.where(checksum: [nil, ""]).find_each do |x|
if File.exist?(x.path)
if File.exist?(x.path) && x.body_hash.blank?
digest = Digest::SHA1.new digest = Digest::SHA1.new
digest.update File.binread(x.path) digest.update File.binread(x.path)
x.checksum = digest.hexdigest x.checksum = digest.to_s
x.save x.save
count += 1 count += 1
end end