-
Notifications
You must be signed in to change notification settings - Fork 167
LG-14030: Ensure roll plan 0032's rake task is available in all environments #10988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eileen-nava
merged 5 commits into
main
from
em/14030-restore-backfill-sponsor-id-rake-task
Jul 29, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
75f94cc
restore rake task to backfill sponsor_id column in in_person_enrollme…
eileen-nava 32add90
Changelog: Upcoming Features, In-person proofing, backfill sponsor id…
eileen-nava bea450d
update in_person_enrollments factory and get tests passing
eileen-nava 715d76a
do clean up
eileen-nava 2a584ef
Merge branch 'main' into em/14030-restore-backfill-sponsor-id-rake-task
eileen-nava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 | ||
| 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 | ||
This file contains hidden or 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
This file contains hidden or 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,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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_timeouthere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reports::BaseReport.transaction_with_timeoutis new to me - I'm curious what it adds here?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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_timeoutwas not set for any of those environments. When I checked inapplication.yml.default, the only environment that has a value set forreport_timeoutis production. This makes me think that in order to use theReports::BaseReport.transaction_with_timeoutmethod, whoever runs this task would first have to set thereport_timeoutconfig variable in dev, int, and staging. Is this correct?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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_timeoutvariable.