Skip to content

Commit

Permalink
Add migration to change other_secondary qualifications to other quali…
Browse files Browse the repository at this point in the history
…fications
  • Loading branch information
KyleMacPherson committed Jan 15, 2025
1 parent 6b00a68 commit eefc189
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 40 deletions.
23 changes: 6 additions & 17 deletions app/services/qualifications_migration.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
class QualificationsMigration
def self.perform
ActiveRecord::Base.transaction do
qualifications = Qualification.where(category: 'other_secondary')
qualifications = Qualification.where(category: "other_secondary")

qualifications.find_each do |qualification|
# Get associated qualification results
results = qualification.qualification_results
results.each do |result|
# Create a new qualification with category 'other'
Qualification.create!(
category: 'other',
subject: result.subject,
grade: result.grade,
institution: qualification.institution,
name: qualification.name,
year: qualification.year,
job_application_id: qualification.job_application_id,
jobseeker_profile_id: qualification.jobseeker_profile_id,
finished_studying: qualification.finished_studying
)
Qualification.create!(category: "other", subject: result.subject, grade: result.grade, institution: qualification.institution, name: qualification.name,
year: qualification.year, job_application_id: qualification.job_application_id, jobseeker_profile_id: qualification.jobseeker_profile_id,
finished_studying: qualification.finished_studying)
end

# Delete the original qualification
qualification.destroy!
end
end
rescue => e
rescue StandardError => e
Rails.logger.error("Error migrating qualifications: #{e.message}")
raise
end
end
end
9 changes: 9 additions & 0 deletions lib/tasks/update_other_secondary_qualifications.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace :qualifications do
desc "Migrate other_secondary qualifications to other category"
task migrate_to_other: :environment do
QualificationsMigration.perform
puts "Migration completed successfully."
rescue StandardError => e
puts "Migration failed: #{e.message}"
end
end
80 changes: 57 additions & 23 deletions spec/services/qualifications_migration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,80 @@
require 'rails_helper'
require "rails_helper"

RSpec.describe QualificationsMigration, type: :model do
let!(:qualification) { create(:qualification, category: 'other_secondary') }

let!(:profile) { create(:jobseeker_profile) }
let!(:application) { create(:job_application) }

let!(:profile_qualification) { create(:qualification, category: "other_secondary", jobseeker_profile: profile, job_application: nil) }
let!(:application_qualification) { create(:qualification, category: "other_secondary", job_application: application, jobseeker_profile: nil) }
let!(:unrelated_qualification) { create(:qualification, category: "a_level") }

before do
qualification.qualification_results.destroy_all
QualificationResult.create(qualification: qualification, subject: 'Math', grade: 'A', awarding_body: "AXA")
QualificationResult.create(qualification: qualification, subject: 'English', grade: 'B', awarding_body: "Bee")
profile_qualification.qualification_results.destroy_all
application_qualification.qualification_results.destroy_all

QualificationResult.create!(qualification: profile_qualification, subject: "Science", grade: "A")
QualificationResult.create!(qualification: application_qualification, subject: "History", grade: "B")
end

describe '.perform' do
it 'migrates qualifications with category other_secondary to other' do
describe ".perform" do
it "migrates qualifications with category other_secondary to other" do
expect {
QualificationsMigration.perform
}.to change { Qualification.where(category: 'other').count }.by(2)
described_class.perform
}.to change { Qualification.where(category: "other").count }.by(2)
end

it 'creates new qualifications with the correct attributes' do
QualificationsMigration.perform
new_qualifications = Qualification.where(category: 'other')
it "creates new qualifications with the correct attributes" do
described_class.perform
new_qualifications = Qualification.where(category: "other")

expect(new_qualifications.count).to eq(2)
expect(new_qualifications.pluck(:subject)).to contain_exactly('Math', 'English')
expect(new_qualifications.pluck(:grade)).to contain_exactly('A', 'B')
expect(new_qualifications.pluck(:subject)).to contain_exactly("Science", "History")
expect(new_qualifications.pluck(:grade)).to contain_exactly("A", "B")
end

it "ensures new qualifications maintain their associations" do
described_class.perform

profile_other_qualification = Qualification.find_by(subject: "Science", category: "other")
application_other_qualification = Qualification.find_by(subject: "History", category: "other")

expect(profile_other_qualification.jobseeker_profile).to eq(profile)
expect(profile_other_qualification.job_application).to be_nil

expect(application_other_qualification.job_application).to eq(application)
expect(application_other_qualification.jobseeker_profile).to be_nil
end

it "deletes the original qualifications with category other_secondary" do
expect {
described_class.perform
}.to change { Qualification.where(category: "other_secondary").count }.by(-2)
end

it 'deletes the original qualification with category other_secondary' do
it "does not change other qualification types" do
expect {
QualificationsMigration.perform
}.to change { Qualification.where(category: 'other_secondary').count }.by(-1)
described_class.perform
}.not_to(change { Qualification.where(category: "a_level").count })

unaffected_qualification = Qualification.find_by(id: unrelated_qualification.id)
expect(unaffected_qualification).to be_present
expect(unaffected_qualification.category).to eq("a_level")
end

context 'when an error occurs during migration' do
context "when an error occurs during migration" do
before do
allow(Qualification).to receive(:create!).and_raise(StandardError, 'Test error')
allow(Qualification).to receive(:create!).and_raise(StandardError, "Test error")
end

it 'logs the error and does not delete original qualifications' do
it "logs the error and does not delete original qualifications" do
expect(Rails.logger).to receive(:error).with(/Error migrating qualifications: Test error/)
expect {
QualificationsMigration.perform rescue nil
}.to_not change { Qualification.where(category: 'other_secondary').count }
begin
described_class.perform
rescue StandardError
nil
end
}.not_to(change { Qualification.where(category: "other_secondary").count })
end
end
end
Expand Down

0 comments on commit eefc189

Please sign in to comment.