From 1641e3f09f7bb2feb67906821b0604b74fcc9721 Mon Sep 17 00:00:00 2001 From: Michael Harrison Date: Wed, 25 Oct 2023 10:30:45 -0500 Subject: [PATCH] remove last_id tracking, fix save transaction --- lib/tasks/accounts/update_adopter_status.rake | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/tasks/accounts/update_adopter_status.rake b/lib/tasks/accounts/update_adopter_status.rake index 07215bf07..811891834 100644 --- a/lib/tasks/accounts/update_adopter_status.rake +++ b/lib/tasks/accounts/update_adopter_status.rake @@ -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( @@ -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