Skip to content

Commit 8e79a69

Browse files
Explicitly handle ImportantNotices in PatientMerger
This used to be handled using a `dependent: :destroy` relationship on the `Patient` model. However, after the introduction of `PatientDeleter` (which explicitly deletes) `ImportantNotice`s, this relationship should be removed, replaced by explicit handling in `PatientMerger` Jira-Issue: MAV-7067
1 parent 4b5f5f5 commit 8e79a69

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

app/lib/patient_merger.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def call
2525

2626
patient_to_destroy.archive_reasons.destroy_all
2727

28+
patient_to_destroy.important_notices.destroy_all
29+
2830
patient_to_destroy.attendance_records.find_each do |attendance_record|
2931
if patient_to_keep.attendance_records.exists?(
3032
location_id: attendance_record.location_id,

spec/lib/patient_merger_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
let(:gillick_assessment) do
5858
create(:gillick_assessment, :competent, patient: patient_to_destroy)
5959
end
60+
let(:important_notice) do
61+
create(:important_notice, :deceased, patient: patient_to_destroy, team:)
62+
end
6063
let(:note) { create(:note, patient: patient_to_destroy) }
6164
let(:notify_log_entry) do
6265
create(:notify_log_entry, :email, patient: patient_to_destroy)
@@ -168,6 +171,11 @@
168171
)
169172
end
170173

174+
it "deletes important notices" do
175+
important_notice
176+
expect { call }.to change(ImportantNotice, :count).by(-1)
177+
end
178+
171179
it "moves notes" do
172180
expect { call }.to change { note.reload.patient }.to(patient_to_keep)
173181
end
@@ -492,4 +500,52 @@
492500
end
493501
end
494502
end
503+
504+
# This test ensures that if a new table is added with a non-cascading FK
505+
# to patients, it will prompt the developer to handle it in PatientMerger.
506+
describe "non-cascading patient FK coverage" do
507+
it "covers all non-cascading FK relationships to the patients table" do
508+
non_cascading_fk_tables =
509+
ActiveRecord::Base.connection.tables.flat_map do |table|
510+
foreign_key =
511+
ActiveRecord::Base
512+
.connection
513+
.foreign_keys(table)
514+
.select do |fk|
515+
fk.to_table == "patients" && fk.options[:on_delete] != :cascade
516+
end
517+
foreign_key.map(&:from_table)
518+
end
519+
non_cascading_fk_tables = non_cascading_fk_tables.to_set
520+
521+
# Tables explicitly handled by PatientMerger
522+
explicitly_handled = %w[
523+
archive_reasons
524+
attendance_records
525+
clinic_notifications
526+
consent_notifications
527+
consents
528+
gillick_assessments
529+
important_notices
530+
notes
531+
parent_relationships
532+
patient_changesets
533+
patient_locations
534+
patient_specific_directions
535+
pre_screenings
536+
school_moves
537+
session_notifications
538+
triages
539+
vaccination_records
540+
].to_set
541+
542+
unhandled = non_cascading_fk_tables - explicitly_handled
543+
544+
expect(unhandled).to(
545+
be_empty,
546+
"The following tables have non-cascading FKs to patients but are " \
547+
"not handled in PatientMerger: #{unhandled.to_a.sort.join(", ")}"
548+
)
549+
end
550+
end
495551
end

0 commit comments

Comments
 (0)