Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/tasks/backfill_sponsor_id.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

namespace :in_person_enrollments do
desc 'Backfill the sponsor_id column.'

##
# Usage:
#
# bundle exec rake in_person_enrollments:backfill_sponsor_id
#
task backfill_sponsor_id: :environment do |_task, _args|
with_timeout do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we able to use the Reports::BaseReport.transaction_with_timeout here?

Suggested change
with_timeout do
Reports::BaseReport.transaction_with_timeout do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reports::BaseReport.transaction_with_timeout is new to me - I'm curious what it adds here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not having to re-implement a nearly-identical method below in this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the config for dev, int, and staging and found that report_timeout was not set for any of those environments. When I checked in application.yml.default, the only environment that has a value set for report_timeout is production. This makes me think that in order to use the Reports::BaseReport.transaction_with_timeout method, whoever runs this task would first have to set the report_timeout config variable in dev, int, and staging. Is this correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the value is nil, it's an unlimited timeout

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!
(Do we want an unlimited timeout?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes we do! but we could also update the method to accept an explicit timeout if we wanted to hardcode that. I guess what I was aiming to get at was it might be nice to DRY some timeout code, but if this rake task is going away, maybe not worth the effort

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think the reason I’m balking at this proposed change is because it feels weird to have a piece of code that isn’t related to reports rely on the report_timeout variable.

ipp_sponsor_id = IdentityConfig.store.usps_ipp_sponsor_id
enrollments_without_sponsor_id = InPersonEnrollment.where(sponsor_id: nil)
enrollments_without_sponsor_id_count = enrollments_without_sponsor_id.count

warn("Found #{enrollments_without_sponsor_id_count} in_person_enrollments needing backfill")

tally = 0
enrollments_without_sponsor_id.in_batches(of: batch_size) do |batch|
tally += batch.update_all(sponsor_id: ipp_sponsor_id) # rubocop:disable Rails/SkipsModelValidations
warn("set sponsor_id for #{tally} in_person_enrollments")
end
warn("COMPLETE: Updated #{tally} in_person_enrollments")

enrollments_without_sponsor_id = InPersonEnrollment.where(sponsor_id: nil)
enrollments_without_sponsor_id_count = enrollments_without_sponsor_id.count
warn("#{enrollments_without_sponsor_id_count} enrollments without a sponsor id")
end
end

def batch_size
ENV['BATCH_SIZE'] ? ENV['BATCH_SIZE'].to_i : 1000
end

def with_timeout
timeout_in_seconds ||= if ENV['STATEMENT_TIMEOUT_IN_SECONDS']
ENV['STATEMENT_TIMEOUT_IN_SECONDS'].to_i.seconds
else
60.seconds
end
ActiveRecord::Base.transaction do
quoted_timeout = ActiveRecord::Base.connection.quote(timeout_in_seconds.in_milliseconds)
ActiveRecord::Base.connection.execute("SET statement_timeout = #{quoted_timeout}")
yield
end
end
end
4 changes: 4 additions & 0 deletions spec/factories/in_person_enrollments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@
trait :enhanced_ipp do
sponsor_id { IdentityConfig.store.usps_eipp_sponsor_id }
end

trait :with_nil_sponsor_id do
sponsor_id { nil }
end
end
end
73 changes: 73 additions & 0 deletions spec/lib/tasks/backfill_sponsor_id_rake_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'rails_helper'
require 'rake'

RSpec.describe 'in_person_enrollments:backfill_sponsor_id rake task' do
let!(:task) do
Rake.application.rake_require 'tasks/backfill_sponsor_id'
Rake::Task.define_task(:environment)
Rake::Task['in_person_enrollments:backfill_sponsor_id']
end

subject(:invoke_task) do
actual_stderr = $stderr
proxy_stderr = StringIO.new
begin
$stderr = proxy_stderr
task.reenable
task.invoke
proxy_stderr.string
ensure
$stderr = actual_stderr
end
end

let(:pending_enrollment) { create(:in_person_enrollment, :pending, :with_nil_sponsor_id) }
let(:expired_enrollment) { create(:in_person_enrollment, :expired, :with_nil_sponsor_id) }
let(:failed_enrollment) { create(:in_person_enrollment, :failed, :with_nil_sponsor_id) }
let(:enrollment_with_service_provider) do
create(:in_person_enrollment, :with_service_provider, :with_nil_sponsor_id)
end
let(:enrollment_with_sponsor_id) { create(:in_person_enrollment) }

before do
allow(IdentityConfig.store).to receive(:usps_ipp_sponsor_id).and_return('31459')
expect(pending_enrollment.sponsor_id).to be_nil
expect(expired_enrollment.sponsor_id).to be_nil
expect(failed_enrollment.sponsor_id).to be_nil
expect(enrollment_with_service_provider.sponsor_id).to be_nil
expect(enrollment_with_sponsor_id.sponsor_id).not_to be_nil
end

it 'does not change the value of an existing sponsor id' do
original_sponsor_id = enrollment_with_sponsor_id.sponsor_id
subject
expect(enrollment_with_sponsor_id.sponsor_id).to eq(original_sponsor_id)
end

it 'sets a sponsor id for every enrollment with a nil sponsor id' do
enrollments_with_nil_sponsor_id_count = InPersonEnrollment.where(sponsor_id: nil).count
expect(enrollments_with_nil_sponsor_id_count).to eq(4)
subject
enrollments_with_nil_sponsor_id_count = InPersonEnrollment.where(sponsor_id: nil).count
expect(enrollments_with_nil_sponsor_id_count).to eq(0)
end

it 'sets a sponsor id that is a string' do
subject
enrollments = InPersonEnrollment.all
enrollments.each do |enrollment|
expect(enrollment.sponsor_id).to be_a String
end
end

it 'outputs what it did' do
expect(invoke_task.to_s).to eql(
<<~END,
Found 4 in_person_enrollments needing backfill
set sponsor_id for 4 in_person_enrollments
COMPLETE: Updated 4 in_person_enrollments
0 enrollments without a sponsor id
END
)
end
end