Skip to content

Commit

Permalink
remove last_id tracking, fix save transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo committed Oct 25, 2023
1 parent 4db46de commit 1641e3f
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions lib/tasks/accounts/update_adopter_status.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ namespace :accounts do
desc 'Update newly created field for adopter status from Salesforce'
# rake accounts:update_adopter_status
task update_adopter_status: [:environment] do
last_id = nil
loop do
users = User.where(adopter_status: nil).order(:id).limit(250)
users = users.where("id > #{last_id}") unless last_id.nil?
last_id = users.last&.id
users = User.where(adopter_status: nil).where.not(salesforce_contact_id: nil ).limit(250)

begin
contacts = OpenStax::Salesforce::Remote::Contact.select(
Expand All @@ -16,21 +13,18 @@ namespace :accounts do
)
.where(id: users.map(&:salesforce_contact_id))
.index_by(&:id)
.to_a

updated_users = users.map do |user|
contact = contacts[user.salesforce_contact_id]
user.adopter_status = contact.adoption_status
end

updated_users.transaction do
updated_users.save!
updated_users.each(&:save!)
end
rescue StandardError => se
Sentry.capture_exception se
end

break if users.length < 250
end
end
end

0 comments on commit 1641e3f

Please sign in to comment.