Skip to content

Commit

Permalink
Address comments in PR - use save! & report ID's of records updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Sutton committed Dec 23, 2020
1 parent 5c46a1b commit 4f88e28
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/tasks/historic_data_contacts_whitespace_strip.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@ namespace :strip_whitespace do
task data_cleanup: :environment do
BATCH_SIZE = 1000
# Process in batches set by the batch size 1000 is default.
Contact.find_in_batches(batch_size: BATCH_SIZE) do |contact|
puts contact.size
contact.each do |contact_to_update|
contact_to_update.save(touch: false)
targeted_attributes = ["first_name", "middle_names", "surname"]
AR_where = []
updated_contacts = []

# These are the fields/attributes of Contact that to 'clean'
targeted_attributes.each do |field|
AR_where << "LENGTH(RTRIM(LTRIM(#{field}))) <> LENGTH(#{field})"
end

Contact.where(AR_where.join(' OR ')).find_in_batches(batch_size: BATCH_SIZE) do |contacts|
temp_updated_contacts = []

contacts.each do |contact|
contact.save!(touch: false)
temp_updated_contacts << contact.id
rescue ActiveRecord::RecordInvalid => e
puts "Records updated - incomplete (#{temp_updated_contacts.size} of #{BATCH_SIZE}):\n#{temp_updated_contacts}"
updated_contacts << temp_updated_contacts
raise ActiveRecord::RecordInvalid
end
puts "Records updated (#{BATCH_SIZE}):\n#{temp_updated_contacts}"
end
puts "Completed (#{updated_contacts.size}):\n#{updated_contacts}"
end
end

0 comments on commit 4f88e28

Please sign in to comment.