Skip to content

Commit 4be314c

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 b6f68e5 commit 4be314c

1 file changed

Lines changed: 61 additions & 65 deletions

File tree

app/lib/mavis_cli/teams/reset_national_reporting.rb

Lines changed: 61 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -90,81 +90,77 @@ def find_teams(workgroup)
9090
end
9191

9292
def reset_team(team)
93-
patient_ids_to_update = Set.new
94-
95-
ActiveRecord::Base.transaction do
96-
immunisation_imports = find_immunisation_imports_for_team(team)
97-
puts " - Found #{immunisation_imports.count} immunisation import(s) created before team's cut off date: " \
93+
immunisation_imports = find_immunisation_imports_for_team(team)
94+
puts " - Found #{immunisation_imports.count} immunisation import(s) created before team's cut off date: " \
9895
"#{team.national_reporting_cut_off_date}"
9996

100-
patient_ids = find_patients_for_team(team).ids
101-
puts " - Found #{patient_ids.count} patient(s) in this team"
102-
103-
vaccination_records = find_vaccination_records_for_team(team)
104-
puts " - Found #{vaccination_records.count} vaccination record(s) in this team's imports"
105-
106-
not_synced_vaccination_records =
107-
vaccination_records.not_synced_to_nhs_immunisations_api
108-
synced_vaccination_records =
109-
vaccination_records.synced_to_nhs_immunisations_api
110-
patient_ids_of_not_synced_records =
111-
not_synced_vaccination_records.pluck(:patient_id).uniq
112-
if synced_vaccination_records.exists?
113-
puts " - #{synced_vaccination_records.count} vaccination" \
114-
" record(s) have been synced to NHS Immunisations API and" \
115-
" will NOT be deleted"
116-
puts " - #{not_synced_vaccination_records.count} vaccination" \
117-
" record(s) will be deleted"
118-
end
119-
120-
puts "Destroying vaccination records..."
121-
not_synced_vaccination_records.destroy_all
122-
123-
puts "Refreshing immunisations imports..."
124-
if immunisation_imports.joins(:vaccination_records).any?
125-
immunisation_imports_with_records =
126-
immunisation_imports.joins(:vaccination_records).distinct
127-
immunisation_imports =
128-
immunisation_imports.where.not(
129-
id: immunisation_imports_with_records.select(:id)
130-
)
131-
puts " - #{immunisation_imports_with_records.count} immunisation" \
132-
" import(s) have associated vaccination records and will NOT" \
133-
" be deleted"
134-
puts " - #{immunisation_imports.count} immunisation import(s) will" \
135-
" be deleted"
136-
end
97+
patient_ids = find_patients_for_team(team).ids
98+
puts " - Found #{patient_ids.count} patient(s) in this team"
99+
100+
vaccination_records = find_vaccination_records_for_team(team)
101+
puts " - Found #{vaccination_records.count} vaccination record(s) in this team's imports"
102+
103+
not_synced_vaccination_records =
104+
vaccination_records.not_synced_to_nhs_immunisations_api
105+
synced_vaccination_records =
106+
vaccination_records.synced_to_nhs_immunisations_api
107+
patient_ids_of_not_synced_records =
108+
not_synced_vaccination_records.pluck(:patient_id).uniq
109+
if synced_vaccination_records.exists?
110+
puts " - #{synced_vaccination_records.count} vaccination" \
111+
" record(s) have been synced to NHS Immunisations API and" \
112+
" will NOT be deleted"
113+
puts " - #{not_synced_vaccination_records.count} vaccination" \
114+
" record(s) will be deleted"
115+
end
137116

138-
puts "Destroying immunisation imports..."
139-
immunisation_imports.destroy_all
117+
puts "Destroying vaccination records..."
118+
not_synced_vaccination_records.destroy_all
140119

141-
archive_reasons =
142-
ArchiveReason.where(
143-
patient_id: patient_ids_of_not_synced_records,
144-
team:
120+
puts "Refreshing immunisations imports..."
121+
if immunisation_imports.joins(:vaccination_records).any?
122+
immunisation_imports_with_records =
123+
immunisation_imports.joins(:vaccination_records).distinct
124+
immunisation_imports =
125+
immunisation_imports.where.not(
126+
id: immunisation_imports_with_records.select(:id)
145127
)
146-
puts "Destroying #{archive_reasons.count} archive reasons..."
147-
archive_reasons.destroy_all
128+
puts " - #{immunisation_imports_with_records.count} immunisation" \
129+
" import(s) have associated vaccination records and will NOT" \
130+
" be deleted"
131+
puts " - #{immunisation_imports.count} immunisation import(s) will" \
132+
" be deleted"
133+
end
134+
135+
puts "Destroying immunisation imports..."
136+
immunisation_imports.destroy_all
148137

149-
puts "Updating patient-team relationships..."
150-
PatientTeamUpdater.call(
151-
patient_scope: Patient.where(id: patient_ids_of_not_synced_records)
138+
archive_reasons =
139+
ArchiveReason.where(
140+
patient_id: patient_ids_of_not_synced_records,
141+
team:
152142
)
143+
puts "Destroying #{archive_reasons.count} archive reasons..."
144+
archive_reasons.destroy_all
153145

154-
patients_to_destroy =
155-
find_patients_without_team(patient_ids_of_not_synced_records)
146+
puts "Updating patient-team relationships..."
147+
PatientTeamUpdater.call(
148+
patient_scope: Patient.where(id: patient_ids_of_not_synced_records)
149+
)
156150

157-
patient_ids_to_update +=
158-
patient_ids_of_not_synced_records - patients_to_destroy.ids
159-
puts " - Found #{patients_to_destroy.count}" \
160-
" patient(s) who were in the imports, and no longer have teams"
151+
patients_to_destroy =
152+
find_patients_without_team(patient_ids_of_not_synced_records)
161153

162-
puts "Destroying #{patients_to_destroy.count} patients..."
163-
PatientDeleter.call(
164-
patients: patients_to_destroy,
165-
confirm_production_delete: true
166-
)
167-
end
154+
patient_ids_to_update =
155+
patient_ids_of_not_synced_records - patients_to_destroy.ids
156+
puts " - Found #{patients_to_destroy.count}" \
157+
" patient(s) who were in the imports, and no longer have teams"
158+
159+
puts "Destroying #{patients_to_destroy.count} patients..."
160+
PatientDeleter.call(
161+
patients: patients_to_destroy,
162+
confirm_production_delete: true
163+
)
168164

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

0 commit comments

Comments
 (0)