From 9b8095545b28f053b3ce2e9c4052b4bc67eeb978 Mon Sep 17 00:00:00 2001 From: Lori Bailey <44073106+elceebee@users.noreply.github.com> Date: Wed, 25 Sep 2024 14:51:31 +0100 Subject: [PATCH 1/3] Data migration to remove the decline_by_default data from 2024 application choices --- ...t_at_and_decline_by_default_days_to_nil.rb | 28 +++++++++++ lib/tasks/data.rake | 1 + ...and_decline_by_default_days_to_nil_spec.rb | 49 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 app/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil.rb create mode 100644 spec/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil_spec.rb diff --git a/app/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil.rb b/app/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil.rb new file mode 100644 index 00000000000..118bfe1f10b --- /dev/null +++ b/app/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil.rb @@ -0,0 +1,28 @@ +module DataMigrations + class SetDeclineByDefaultAtAndDeclineByDefaultDaysToNil + TIMESTAMP = 20240925092609 + MANUAL_RUN = false + + def change + application_choices.in_batches(of: 4000) do |batch| + batch.update_all(decline_by_default_at: nil, decline_by_default_days: nil) + end + end + + private + + def application_choices + choices_from_2024 + .where.not(decline_by_default_at: nil) + .or(choices_from_2024.where.not(decline_by_default_days: nil)) + .distinct + end + + def choices_from_2024 + @choices_from_2024 ||= ApplicationChoice + .joins(:application_form) + .where('application_form.recruitment_cycle_year': 2024) + .distinct + end + end +end diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 5f46a1049fb..965c4d79c03 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -1,5 +1,6 @@ DATA_MIGRATION_SERVICES = [ # do not delete or edit this line - services added below by generator + 'DataMigrations::SetDeclineByDefaultAtAndDeclineByDefaultDaysToNil', 'DataMigrations::BackfillApplicationChoicesWithWorkExperiences', 'DataMigrations::MarkUnsubmittedApplicationsWithoutEnglishProficiencyAsElfIncomplete', 'DataMigrations::BackfillEnglishProficiencyRecordsForCarriedOverApplications', diff --git a/spec/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil_spec.rb b/spec/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil_spec.rb new file mode 100644 index 00000000000..624d3120ce0 --- /dev/null +++ b/spec/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' + +RSpec.describe DataMigrations::SetDeclineByDefaultAtAndDeclineByDefaultDaysToNil do + it 'updates choices where decline_by_default_at is not null' do + application_choice = create(:application_choice, + decline_by_default_at: DateTime.now, + decline_by_default_days: nil, + application_form: build(:application_form, recruitment_cycle_year: 2024)) + + described_class.new.change + expect(application_choice.reload.decline_by_default_at).to be_nil + end + + it 'updates choices where decline_by_default_days is not null' do + application_choice = create(:application_choice, + decline_by_default_at: nil, + decline_by_default_days: 10, + application_form: build(:application_form, recruitment_cycle_year: 2024)) + + described_class.new.change + + expect(application_choice.reload.decline_by_default_days).to be_nil + end + + it 'updates choices where both days and at are not null' do + application_choice = create(:application_choice, + decline_by_default_at: DateTime.now, + decline_by_default_days: 10, + application_form: build(:application_form, recruitment_cycle_year: 2024)) + + described_class.new.change + + application_choice.reload + expect(application_choice.decline_by_default_days).to be_nil + expect(application_choice.decline_by_default_at).to be_nil + end + + it 'updates only 2024 application choices' do + application_2024 = create(:application_choice, decline_by_default_at: Time.zone.now, + application_form: build(:application_form, recruitment_cycle_year: 2024)) + application_2023 = create(:application_choice, decline_by_default_at: Time.zone.now, + application_form: build(:application_form, recruitment_cycle_year: 2023)) + + described_class.new.change + + expect(application_2024.reload.decline_by_default_at).to be_nil + expect(application_2023.reload.decline_by_default_at).not_to be_nil + end +end From b1995ce0612f45d4951517a5aba1bc8c0a247eb5 Mon Sep 17 00:00:00 2001 From: Lori Bailey <44073106+elceebee@users.noreply.github.com> Date: Thu, 26 Sep 2024 09:54:47 +0100 Subject: [PATCH 2/3] Remove data migrations from array --- lib/tasks/data.rake | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 965c4d79c03..b3c1e733fc6 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -20,8 +20,6 @@ DATA_MIGRATION_SERVICES = [ 'DataMigrations::RemoveFeedbackHelpfulFeatureFlag', 'DataMigrations::RemoveOnePersonalStatementFeatureFlag', 'DataMigrations::SetMissingWorkHistoryStatusValues', - 'DataMigrations::UpdateDeclineByDefaultAtFromCurrentCycle', - 'DataMigrations::RemoveDbdFromCurrentCycle', 'DataMigrations::RemoveRecruitWithPendingConditionsFeatureFlag', 'DataMigrations::BackfillFeedbackFormComplete', 'DataMigrations::RemoveMidCycleReportFeatureFlag', From 969351fa52e5dfff14e996c0ab5659adbdab2070 Mon Sep 17 00:00:00 2001 From: Lori Bailey <44073106+elceebee@users.noreply.github.com> Date: Thu, 26 Sep 2024 10:48:38 +0100 Subject: [PATCH 3/3] run migration manually --- ..._decline_by_default_at_and_decline_by_default_days_to_nil.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil.rb b/app/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil.rb index 118bfe1f10b..319a98ef49e 100644 --- a/app/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil.rb +++ b/app/services/data_migrations/set_decline_by_default_at_and_decline_by_default_days_to_nil.rb @@ -1,7 +1,7 @@ module DataMigrations class SetDeclineByDefaultAtAndDeclineByDefaultDaysToNil TIMESTAMP = 20240925092609 - MANUAL_RUN = false + MANUAL_RUN = true def change application_choices.in_batches(of: 4000) do |batch|