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 Jan 12, 2021
1 parent 41484cc commit 8078af3
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']
where_statement = []
updated_contacts = []

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

Contact.where(where_statement.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
puts "Records updated - incomplete (#{temp_updated_contacts.size} of #{BATCH_SIZE}):\n#{temp_updated_contacts}"
raise ActiveRecord::RecordInvalid
end
puts "Records updated (#{BATCH_SIZE}):\n#{temp_updated_contacts}"
updated_contacts << temp_updated_contacts
end
puts 'Completed'
end
end

0 comments on commit 8078af3

Please sign in to comment.