Skip to content
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
4 changes: 2 additions & 2 deletions app/jobs/commit_patient_changesets_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def import_school_moves(changesets, import)
school_move.confirm! if school_move.patient.persisted?
end

SchoolMove.import(
SchoolMove.import!(
importable_school_moves.to_a,
on_duplicate_key_ignore: :all
on_duplicate_key_update: :all
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/class_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def postprocess_rows!
)
end

SchoolMove.import(school_moves, on_duplicate_key_ignore: true)
SchoolMove.import!(school_moves, on_duplicate_key_ignore: true)

PatientsAgedOutOfSchoolJob.perform_later
end
Expand Down
9 changes: 2 additions & 7 deletions app/models/consent_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,8 @@ def match_with_patient!(patient, current_user:)
patient.school != school || patient.home_educated != home_educated

if school_changed && !patient.deceased? && !patient.invalidated?
school_move =
if school
SchoolMove.find_or_initialize_by(patient:, school:)
else
SchoolMove.find_or_initialize_by(patient:, home_educated:, team:)
end

school_move = SchoolMove.find_or_initialize_by(patient:)
school_move.assign_from(school:, home_educated:, team:)
school_move.update!(academic_year:, source: :parental_consent_form)
end

Expand Down
18 changes: 7 additions & 11 deletions app/models/patient_changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,13 @@ def school_move
patient.home_educated != home_educated ||
patient.not_in_team?(team:, academic_year:) ||
patient.archived?(team:)
school_move =
if school
SchoolMove.find_or_initialize_by(patient:, school:)
else
SchoolMove.find_or_initialize_by(patient:, home_educated:, team:)
end

school_move.tap do
it.academic_year = academic_year
it.source = school_move_source
end
school_move = SchoolMove.find_or_initialize_by(patient:)
school_move.assign_from(school:, home_educated:, team:)
school_move.assign_attributes(
academic_year:,
source: school_move_source
)
school_move
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion app/models/patient_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def bulk_import(rows: 100)
end
@school_moves_to_confirm.clear

SchoolMove.import(@school_moves_to_save.to_a, on_duplicate_key_ignore: :all)
SchoolMove.import!(
@school_moves_to_save.to_a,
on_duplicate_key_update: :all
)
@school_moves_to_save.clear
end
end
15 changes: 4 additions & 11 deletions app/models/patient_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,10 @@ def to_school_move(patient)
if patient.new_record? || patient.school != school ||
patient.home_educated != home_educated ||
patient.not_in_team?(team:, academic_year:) || patient.archived?(team:)
school_move =
if school
SchoolMove.find_or_initialize_by(patient:, school:)
else
SchoolMove.find_or_initialize_by(patient:, home_educated:, team:)
end

school_move.tap do
it.academic_year = academic_year
it.source = school_move_source
end
school_move = SchoolMove.find_or_initialize_by(patient:)
school_move.assign_from(school:, home_educated:, team:)
school_move.assign_attributes(academic_year:, source: school_move_source)
school_move
end
end

Expand Down
17 changes: 12 additions & 5 deletions app/models/school_move.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
#
# Indexes
#
# index_school_moves_on_patient_id_and_home_educated_and_team_id (patient_id,home_educated,team_id) UNIQUE
# index_school_moves_on_patient_id_and_school_id (patient_id,school_id) UNIQUE
# index_school_moves_on_school_id (school_id)
# index_school_moves_on_team_id (team_id)
# index_school_moves_on_patient_id (patient_id) UNIQUE
# index_school_moves_on_school_id (school_id)
# index_school_moves_on_team_id (team_id)
#
# Foreign Keys
#
Expand Down Expand Up @@ -49,13 +48,21 @@ class SchoolMove < ApplicationRecord
unless: -> { school.nil? }
}

def assign_from(school:, home_educated:, team:)
if school
assign_attributes(school:, home_educated: nil, team: nil)
else
assign_attributes(school: nil, home_educated:, team:)
end
end

def confirm!(user: nil)
ActiveRecord::Base.transaction do
update_patient!
update_archive_reasons!(user:)
update_sessions!
create_log_entry!(user:)
SchoolMove.where(patient:).destroy_all if persisted?
destroy! if persisted?
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class MakeSchoolMovesUniqueOnPatient < ActiveRecord::Migration[8.0]
def change
change_table :school_moves, bulk: true do |t|
t.remove_index %i[patient_id home_educated team_id], unique: true
t.remove_index %i[patient_id school_id], unique: true
end

reversible do |dir|
dir.up do
patient_ids_with_multiple_school_moves =
SchoolMove
.group(:patient_id)
.having("COUNT(*) > 1")
.pluck(:patient_id)

patient_ids_with_multiple_school_moves.each do |patient_id|
newest_school_move_id =
SchoolMove.where(patient_id:).order(:created_at).last.id
SchoolMove
.where(patient_id:)
.where.not(id: newest_school_move_id)
.delete_all
end
end
end

add_index :school_moves, :patient_id, unique: true
end
end
3 changes: 1 addition & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "academic_year", null: false
t.index ["patient_id", "home_educated", "team_id"], name: "index_school_moves_on_patient_id_and_home_educated_and_team_id", unique: true
t.index ["patient_id", "school_id"], name: "index_school_moves_on_patient_id_and_school_id", unique: true
t.index ["patient_id"], name: "index_school_moves_on_patient_id", unique: true
t.index ["school_id"], name: "index_school_moves_on_school_id"
t.index ["team_id"], name: "index_school_moves_on_team_id"
end
Expand Down
7 changes: 3 additions & 4 deletions spec/factories/school_moves.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
#
# Indexes
#
# index_school_moves_on_patient_id_and_home_educated_and_team_id (patient_id,home_educated,team_id) UNIQUE
# index_school_moves_on_patient_id_and_school_id (patient_id,school_id) UNIQUE
# index_school_moves_on_school_id (school_id)
# index_school_moves_on_team_id (team_id)
# index_school_moves_on_patient_id (patient_id) UNIQUE
# index_school_moves_on_school_id (school_id)
# index_school_moves_on_team_id (team_id)
#
# Foreign Keys
#
Expand Down
35 changes: 35 additions & 0 deletions spec/models/cohort_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,41 @@
end
end

context "with an existing patient with a school move" do
let!(:existing_patient) do
create(
:patient,
given_name: "Jimmy",
family_name: "Smith",
date_of_birth: Date.new(2010, 1, 2),
nhs_number: nil,
team:,
school: create(:school)
)
end
let!(:school_move) do
create(:school_move, :to_school, patient: existing_patient)
end

before do
team.sessions.each do |session|
create(:patient_session, session:, patient: existing_patient)
end
end

it "doesn't create an additional patient" do
expect { process! }.to change(Patient, :count).by(2)
end

it "replaces the existing school move" do
expect { process! }.not_to(
change { existing_patient.reload.school_moves.count }
)

expect(school_move.reload.school).to eq(location)
end
end

context "with an unscheduled session" do
let(:session) do
create(:session, :unscheduled, team:, programmes:, location:)
Expand Down
16 changes: 5 additions & 11 deletions spec/models/school_move_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
#
# Indexes
#
# index_school_moves_on_patient_id_and_home_educated_and_team_id (patient_id,home_educated,team_id) UNIQUE
# index_school_moves_on_patient_id_and_school_id (patient_id,school_id) UNIQUE
# index_school_moves_on_school_id (school_id)
# index_school_moves_on_team_id (team_id)
# index_school_moves_on_patient_id (patient_id) UNIQUE
# index_school_moves_on_school_id (school_id)
# index_school_moves_on_team_id (team_id)
#
# Foreign Keys
#
Expand Down Expand Up @@ -155,17 +154,12 @@
end

shared_examples "destroys the school move" do
it "destroys the school move and any others" do
other_school_move = create(:school_move, :to_school, patient:)

it "destroys the school move" do
expect(school_move).to be_persisted
expect { confirm! }.to change(described_class, :count).by(-2)
expect { confirm! }.to change(described_class, :count).by(-1)
expect { school_move.reload }.to raise_error(
ActiveRecord::RecordNotFound
)
expect { other_school_move.reload }.to raise_error(
ActiveRecord::RecordNotFound
)
end
end

Expand Down