-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rake task to conform person emails with lottery applicant emails
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
lib/tasks/maintenance/historical_facts_conform_applicant_emails.rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :maintenance do | ||
desc "Changes people emails to match historical facts emails for lottery applicants" | ||
task historical_facts_conform_applicant_emails: :environment do | ||
puts "Conforming people emails to match historical facts emails for lottery applicants" | ||
|
||
people = Person.joins(:historical_facts) | ||
.where(historical_facts: { kind: :lottery_application} ) | ||
.where("historical_facts.email is not null and historical_facts.email <> people.email") | ||
.distinct | ||
person_count = people.count | ||
|
||
puts "Found #{person_count} people to update" | ||
|
||
progress_bar = ::ProgressBar.new(person_count) | ||
|
||
people.find_each do |person| | ||
progress_bar.increment! | ||
|
||
if person.user_id? | ||
puts "Person #{person.id} has an associated user; skipping" | ||
next | ||
end | ||
|
||
fact = person.historical_facts.find_by(kind: :lottery_application) | ||
person.update(email: fact.email) | ||
rescue ActiveRecordError => e | ||
puts "Could not update record for Person id: #{person.id}" | ||
puts e | ||
end | ||
end | ||
end |