-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data migration for adding recrtuitment cycles to database
- Loading branch information
Showing
5 changed files
with
70 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
23 changes: 23 additions & 0 deletions
23
app/services/data_migrations/add_all_recruitment_cycle_timetables_to_database.rb
This file contains 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,23 @@ | ||
module DataMigrations | ||
class AddAllRecruitmentCycleTimetablesToDatabase | ||
TIMESTAMP = 20250129093844 | ||
MANUAL_RUN = false | ||
|
||
def change | ||
CYCLE_DATES.each do |recruitment_cycle_year, dates| | ||
RecruitmentCycleTimetable.find_or_create_by(recruitment_cycle_year:).tap do |timetable| | ||
timetable.update( | ||
find_opens_at: dates[:find_opens], | ||
apply_opens_at: dates[:apply_opens], | ||
apply_deadline_at: dates[:apply_deadline], | ||
reject_by_default_at: dates[:reject_by_default], | ||
decline_by_default_at: dates[:find_closes] - 1.day, | ||
find_closes_at: dates[:find_closes], | ||
christmas_holiday_range: dates.dig(:holidays, :christmas), | ||
easter_holiday_range: dates.dig(:holidays, :easter), | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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
41 changes: 41 additions & 0 deletions
41
spec/services/data_migrations/add_all_recruitment_cycle_timetables_to_database_spec.rb
This file contains 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,41 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe DataMigrations::AddAllRecruitmentCycleTimetablesToDatabase do | ||
it 'adds all recruitment cycles to database' do | ||
described_class.new.change | ||
expect(RecruitmentCycleTimetable.pluck(:recruitment_cycle_year)) | ||
.to contain_exactly(2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027) | ||
end | ||
|
||
it 'handles holidays as expected' do | ||
described_class.new.change | ||
year_without_holidays = RecruitmentCycleTimetable.find_by(recruitment_cycle_year: 2020) | ||
year_with_holidays = RecruitmentCycleTimetable.find_by(recruitment_cycle_year: 2021) | ||
|
||
expect(year_without_holidays.easter_holiday_range).to be_nil | ||
expect(year_without_holidays.christmas_holiday_range).to be_nil | ||
|
||
expect(year_with_holidays.easter_holiday_range.begin).to eq Date.new(2021, 4, 2) | ||
expect(year_with_holidays.easter_holiday_range.end).to eq Date.new(2021, 4, 17) | ||
|
||
expect(year_with_holidays.christmas_holiday_range.begin).to eq Date.new(2020, 12, 20) | ||
expect(year_with_holidays.christmas_holiday_range.end).to eq Date.new(2021, 1, 2) | ||
end | ||
|
||
it 'does not create duplicates if run twice' do | ||
described_class.new.change | ||
expect(RecruitmentCycleTimetable.count).to eq 9 | ||
described_class.new.change | ||
expect(RecruitmentCycleTimetable.count).to eq 9 | ||
end | ||
|
||
it 'reverts any changes to the original data' do | ||
described_class.new.change | ||
timetable = RecruitmentCycleTimetable.last | ||
original_find_opens_at = timetable.find_opens_at | ||
|
||
timetable.update(find_opens_at: original_find_opens_at - 1.day) | ||
described_class.new.change | ||
expect(timetable.reload.find_opens_at).to eq original_find_opens_at | ||
end | ||
end |