Skip to content

Commit 9dccdf2

Browse files
authored
Merge pull request #6456 from NHSDigital/revert-remove-parents-keep-existing-flow
2 parents 16bc91c + e374800 commit 9dccdf2

4 files changed

Lines changed: 59 additions & 21 deletions

File tree

app/forms/import_duplicate_form.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ def apply_pending_changes!
5858
end
5959

6060
def discard_pending_changes!
61-
remove_imported_parent_relationships_if_needed!
62-
6361
object.patient.discard_pending_changes! if object.respond_to?(:patient)
6462

6563
object.discard_pending_changes!
@@ -92,18 +90,4 @@ def changeset_for_keep_both
9290
def reset_count!
9391
TeamCachedCounts.new(current_team).reset_import_issues!
9492
end
95-
96-
def remove_imported_parent_relationships_if_needed!
97-
return unless object.is_a?(Patient)
98-
99-
changeset = object.changesets.includes(:import).order(:created_at).last
100-
return if changeset.nil?
101-
102-
changeset
103-
.import
104-
.parent_relationships
105-
.includes(:patient)
106-
.where(patient: object)
107-
.find_each(&:destroy!)
108-
end
10993
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
class ReinstateRemovedParentRelationships < ActiveRecord::Migration[8.1]
4+
def up
5+
destroyed_audits =
6+
Audited::Audit.where(
7+
auditable_type: "ParentRelationship",
8+
action: "destroy"
9+
).where("created_at > ?", Date.new(2026, 2, 19))
10+
11+
reinstated = 0
12+
13+
destroyed_audits.in_batches(of: 1000) do |batch|
14+
batch.each do |audit|
15+
changes = audit.audited_changes
16+
patient_id = changes["patient_id"]
17+
parent_id = changes["parent_id"]
18+
19+
next unless Patient.exists?(patient_id)
20+
next unless Parent.exists?(parent_id)
21+
22+
# Only reinstate relationships that were removed as part of discarding
23+
# an import duplicate — identified by a pending_changes clear on the
24+
# same patient within the same minute.
25+
contemporaneous_discard =
26+
Audited::Audit
27+
.where(
28+
auditable_type: "Patient",
29+
auditable_id: patient_id,
30+
action: "update",
31+
created_at:
32+
(audit.created_at - 1.minute)..(audit.created_at + 1.minute)
33+
)
34+
.any? do |patient_audit|
35+
patient_audit.audited_changes.key?("pending_changes") &&
36+
patient_audit.audited_changes["pending_changes"].second == {}
37+
end
38+
39+
next unless contemporaneous_discard
40+
41+
ParentRelationship.find_or_create_by!(patient_id:, parent_id:) do |pr|
42+
pr.type = changes["type"]
43+
pr.other_name = changes["other_name"]
44+
end
45+
46+
reinstated += 1
47+
end
48+
end
49+
50+
say "Reinstated #{reinstated} parent relationship(s)"
51+
end
52+
53+
def down
54+
raise ActiveRecord::IrreversibleMigration
55+
end
56+
end

db/data_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DataMigrate::Data.define(version: 2026_03_26_110653)
1+
DataMigrate::Data.define(version: 2026_03_27_000001)

spec/forms/import_duplicate_form_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
expect(class_import.patients.reload).to contain_exactly(new_patient)
9494
end
9595

96-
it "removes imported parent relationships when discarding changes" do
96+
it "keeps imported parent relationships when discarding changes" do
9797
form =
9898
described_class.new(
9999
apply_changes: "discard",
@@ -103,9 +103,7 @@
103103

104104
expect(form.save).to be(true)
105105

106-
expect { import_relationship.reload }.to raise_error(
107-
ActiveRecord::RecordNotFound
108-
)
106+
expect(import_relationship.reload.patient).to eq(existing_patient)
109107
expect(existing_relationship.reload.patient).to eq(existing_patient)
110108
expect(changeset.reload.patient).to eq(existing_patient)
111109
end

0 commit comments

Comments
 (0)