Skip to content

Commit dfcf020

Browse files
Remove transaction during national reporting team reset
The CLI tool should be retested after more indexes are added, to ascertain whether the locking issue is still extant. If the issue is still present, then this should help reduce the possibility of a big database lock which we witnessed last time we tested this CLI tool in data replication. Jira-Issue: MAV-7083
1 parent 6f99c90 commit dfcf020

1 file changed

Lines changed: 63 additions & 67 deletions

File tree

app/lib/mavis_cli/teams/reset_national_reporting.rb

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -82,81 +82,77 @@ def find_teams(workgroup)
8282
end
8383

8484
def reset_team(team)
85-
patient_ids_to_update = Set.new
86-
87-
ActiveRecord::Base.transaction do
88-
immunisation_imports = find_immunisation_imports_for_team(team)
89-
puts " - Found #{immunisation_imports.count} immunisation import(s) created before team's cut off date: " \
90-
"#{team.national_reporting_cut_off_date}"
91-
92-
patient_ids = find_patients_for_team(team).ids
93-
puts " - Found #{patient_ids.count} patient(s) in this team"
94-
95-
vaccination_records = find_vaccination_records_for_team(team)
96-
puts " - Found #{vaccination_records.count} vaccination record(s) in this team's imports"
97-
98-
not_synced_vaccination_records =
99-
vaccination_records.not_synced_to_nhs_immunisations_api
100-
synced_vaccination_records =
101-
vaccination_records.synced_to_nhs_immunisations_api
102-
patient_ids_of_not_synced_records =
103-
not_synced_vaccination_records.pluck(:patient_id).uniq
104-
if synced_vaccination_records.exists?
105-
puts " - #{synced_vaccination_records.count} vaccination" \
106-
" record(s) have been synced to NHS Immunisations API and" \
107-
" will NOT be deleted"
108-
puts " - #{not_synced_vaccination_records.count} vaccination" \
109-
" record(s) will be deleted"
110-
end
111-
112-
puts "Destroying #{not_synced_vaccination_records.count} vaccination records..."
113-
not_synced_vaccination_records.find_each(&:destroy)
114-
115-
puts "Refreshing immunisations imports..."
116-
if immunisation_imports.joins(:vaccination_records).any?
117-
immunisation_imports_with_records =
118-
immunisation_imports.joins(:vaccination_records).distinct
119-
immunisation_imports =
120-
immunisation_imports.where.not(
121-
id: immunisation_imports_with_records.select(:id)
122-
)
123-
puts " - #{immunisation_imports_with_records.count} immunisation" \
124-
" import(s) have associated vaccination records and will NOT" \
125-
" be deleted"
126-
puts " - #{immunisation_imports.count} immunisation import(s) will" \
127-
" be deleted"
128-
end
85+
immunisation_imports = find_immunisation_imports_for_team(team)
86+
puts " - Found #{immunisation_imports.count} immunisation import(s) created before team's cut off date: " \
87+
"#{team.national_reporting_cut_off_date}"
88+
89+
patient_ids = find_patients_for_team(team).ids
90+
puts " - Found #{patient_ids.count} patient(s) in this team"
91+
92+
vaccination_records = find_vaccination_records_for_team(team)
93+
puts " - Found #{vaccination_records.count} vaccination record(s) in this team's imports"
94+
95+
not_synced_vaccination_records =
96+
vaccination_records.not_synced_to_nhs_immunisations_api
97+
synced_vaccination_records =
98+
vaccination_records.synced_to_nhs_immunisations_api
99+
patient_ids_of_not_synced_records =
100+
not_synced_vaccination_records.pluck(:patient_id).uniq
101+
if synced_vaccination_records.exists?
102+
puts " - #{synced_vaccination_records.count} vaccination" \
103+
" record(s) have been synced to NHS Immunisations API and" \
104+
" will NOT be deleted"
105+
puts " - #{not_synced_vaccination_records.count} vaccination" \
106+
" record(s) will be deleted"
107+
end
129108

130-
puts "Destroying #{immunisation_imports.count} immunisation imports..."
131-
immunisation_imports.find_each(&:destroy)
109+
puts "Destroying #{not_synced_vaccination_records.count} vaccination records..."
110+
not_synced_vaccination_records.find_each(&:destroy)
132111

133-
archive_reasons =
134-
ArchiveReason.where(
135-
patient_id: patient_ids_of_not_synced_records,
136-
team:
112+
puts "Refreshing immunisations imports..."
113+
if immunisation_imports.joins(:vaccination_records).any?
114+
immunisation_imports_with_records =
115+
immunisation_imports.joins(:vaccination_records).distinct
116+
immunisation_imports =
117+
immunisation_imports.where.not(
118+
id: immunisation_imports_with_records.select(:id)
137119
)
138-
puts "Destroying #{archive_reasons.count} archive reasons..."
139-
archive_reasons.find_each(&:destroy)
120+
puts " - #{immunisation_imports_with_records.count} immunisation" \
121+
" import(s) have associated vaccination records and will NOT" \
122+
" be deleted"
123+
puts " - #{immunisation_imports.count} immunisation import(s) will" \
124+
" be deleted"
125+
end
140126

141-
puts "Updating patient-team relationships..."
142-
PatientTeamUpdater.call(
143-
patient_scope: Patient.where(id: patient_ids_of_not_synced_records)
127+
puts "Destroying #{immunisation_imports.count} immunisation imports..."
128+
immunisation_imports.find_each(&:destroy)
129+
130+
archive_reasons =
131+
ArchiveReason.where(
132+
patient_id: patient_ids_of_not_synced_records,
133+
team:
144134
)
135+
puts "Destroying #{archive_reasons.count} archive reasons..."
136+
archive_reasons.find_each(&:destroy)
137+
138+
puts "Updating patient-team relationships..."
139+
PatientTeamUpdater.call(
140+
patient_scope: Patient.where(id: patient_ids_of_not_synced_records)
141+
)
145142

146-
patients_to_destroy =
147-
find_patients_without_team(patient_ids_of_not_synced_records)
143+
patients_to_destroy =
144+
find_patients_without_team(patient_ids_of_not_synced_records)
148145

149-
patient_ids_to_update +=
150-
patient_ids_of_not_synced_records - patients_to_destroy.ids
151-
puts " - Found #{patients_to_destroy.count}" \
152-
" patient(s) who were in the imports, and no longer have teams"
146+
patient_ids_to_update =
147+
patient_ids_of_not_synced_records - patients_to_destroy.ids
148+
puts " - Found #{patients_to_destroy.count}" \
149+
" patient(s) who were in the imports, and no longer have teams"
153150

154-
puts "Destroying #{patients_to_destroy.count} patients..."
155-
PatientDeleter.call(
156-
patients: patients_to_destroy,
157-
confirm_production_delete: true
158-
)
159-
end
151+
puts "Destroying #{patients_to_destroy.count} patients..."
152+
PatientDeleter.call(
153+
patients: patients_to_destroy,
154+
confirm_production_delete: true
155+
)
160156

161157
puts "Enqueueing jobs to update statuses for" \
162158
" #{patient_ids_to_update.size} patient(s) left over..."

0 commit comments

Comments
 (0)