Skip to content
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

Change work_histories migration to insert via SQL #9853

Merged
merged 1 commit into from
Sep 26, 2024
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
63 changes: 41 additions & 22 deletions app/workers/migrate_application_choices_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,55 @@ class MigrateApplicationChoicesWorker
sidekiq_options queue: :low_priority

def perform(choice_ids)
errors = []
return if HostingEnvironment.production?

ApplicationChoice.where(id: choice_ids).find_each(batch_size: 100) do |choice|
ActiveRecord::Base.transaction do
application_form = choice.application_form
application_experiences = []
CatalinVoineag marked this conversation as resolved.
Show resolved Hide resolved
work_history_breaks = []

if choice.work_experiences.blank? && application_form.application_work_experiences.any?
choice.work_experiences = application_form.application_work_experiences.map(&:dup)
end
ApplicationChoice.where(id: choice_ids).find_each(batch_size: 100) do |choice|
application_form = choice.application_form

if choice.work_experiences.blank? && application_form.application_work_experiences.any?
application_experiences += valid_attributes(
application_form.application_work_experiences.map(&:dup),
choice,
'experienceable',
)
end

if choice.volunteering_experiences.blank? && application_form.application_volunteering_experiences.any?
choice.volunteering_experiences = application_form.application_volunteering_experiences.map(&:dup)
end
if choice.volunteering_experiences.blank? && application_form.application_volunteering_experiences.any?
application_experiences += valid_attributes(
application_form.application_volunteering_experiences.map(&:dup),
choice,
'experienceable',
)
end

if choice.work_history_breaks.blank? && application_form.application_work_history_breaks.any?
choice.work_history_breaks = application_form.application_work_history_breaks.map(&:dup)
end
if choice.work_history_breaks.blank? && application_form.application_work_history_breaks.any?
work_history_breaks += valid_attributes(
application_form.application_work_history_breaks.map(&:dup),
choice,
'breakable',
)
end
rescue ActiveRecord::RecordInvalid => e
errors << "Error choice id #{choice.id}: #{e.message}"
end

if errors
errors.each do |error|
Rails.logger.info error
end
ApplicationExperience.insert_all(application_experiences) if application_experiences.any?
ApplicationWorkHistoryBreak.insert_all(work_history_breaks) if work_history_breaks.any?
end

Rails.logger.info "#{errors.count} errors"
end
private

Rails.logger.info 'No errors' if errors.blank?
def valid_attributes(records, choice, polymorphic_column)
records.map do |record|
attributes = record.attributes
attributes["#{polymorphic_column}_type"] = 'ApplicationChoice'
attributes["#{polymorphic_column}_id"] = choice.id
attributes['created_at'] = Time.zone.now
attributes['updated_at'] = Time.zone.now
attributes.delete('id')

attributes
end
end
end
34 changes: 21 additions & 13 deletions spec/workers/migrate_application_choices_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

RSpec.describe MigrateApplicationChoicesWorker do
describe '#perform' do
it 'dups the working experiences and histories from application_form to choice' do
before do
TestSuiteTimeMachine.unfreeze!
end

it 'dups the working experiences and histories from application_form to choice', :with_audited do
application_form = create(
:completed_application_form,
volunteering_experiences_count: 1,
Expand Down Expand Up @@ -31,7 +35,6 @@
breakable: choice_with_data_migrated,
)
choice_ids = [choice.id, choice_with_data_migrated.id]
allow(Rails.logger).to receive(:info)

expect {
described_class.new.perform(choice_ids)
Expand All @@ -41,27 +44,32 @@
.and not_change(choice_with_data_migrated.work_experiences, :count)
.and not_change(choice_with_data_migrated.volunteering_experiences, :count)
.and not_change(choice_with_data_migrated.work_history_breaks, :count)
expect(Rails.logger).to have_received(:info).with('No errors')
.and not_change(choice.own_and_associated_audits, :count)
CatalinVoineag marked this conversation as resolved.
Show resolved Hide resolved
.and not_change(choice.reload, :updated_at)
.and not_change(application_form.reload, :updated_at)
.and not_change(application_form.candidate, :updated_at)
end

context 'with errors' do
it 'rescues ActiveRecord::RecordInvalid exceptions and logs the errors' do
context 'when env is production' do
it 'does not create work histories' do
application_form = create(
:completed_application_form,
volunteering_experiences_count: 1,
full_work_history: true,
)
choice = create(:application_choice, application_form:)
choice_ids = [choice.id]

# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(ApplicationChoice).to receive(:work_experiences=)
.and_raise(ActiveRecord::RecordInvalid)
# rubocop:enable RSpec/AnyInstance
allow(Rails.logger).to receive(:info)
allow(HostingEnvironment).to receive(:production?).and_return(true)

described_class.new.perform(choice_ids)
expect(Rails.logger).to have_received(:info).with("Error choice id #{choice.id}: Record invalid")
expect {
described_class.new.perform([choice.id])
}.to not_change(choice.work_experiences, :count)
.and not_change(choice.volunteering_experiences, :count)
.and not_change(choice.work_history_breaks, :count)
.and not_change(choice.own_and_associated_audits, :count)
.and not_change(choice.reload, :updated_at)
.and not_change(application_form.reload, :updated_at)
.and not_change(application_form.candidate, :updated_at)
end
end
end
Expand Down
Loading