Skip to content

Commit 50b02d6

Browse files
floehopperchrislo
andcommitted
Move calls to Patient Updaters outside transaction
Previously `PatientTeamUpdater.call` & `PatientStatusUpdater.call` were being called from `ImmunisationImport#postprocess_rows!` which was inside the transaction initiated in `ImmunisationImport#process!`. Since both of these Updaters are just updating a set of records that are effectively a "cache", they don't need to run inside the transaction. The `PatientStatusUpdater` in particular can end up UPSERTing a significant number of records, so it seems sensible to do that outside the transaction to reduce the time the transaction takes and reduces the number of records locked by the transaction. This should reduce the chances of other transactions being blocked by an `ImmunisationImport` transaction. The reason the transaction is needed in `ImmunisationImport#process!` is that it's UPSERTing into a bunch of related tables and JOIN tables via `ImmunisationImport#bulk_import` and if an exception occurs in the middle of this, we need to rollback to a consistent state. In contrast the Updaters are UPSERTing records into the "cache" tables and if an exception occurs in the middle of this, there's no need to rollback; the `ImmunisationImport` will fail and when it is eventually run again, the Updaters will be run again and the "cache" will be fully updated. So there's no need wrap them in a transaction of their own. Co-authored-by: Chris Lowis <chris.lowis@gofreerange.com>
1 parent 5ab5fdc commit 50b02d6

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

app/models/immunisation_import.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ def postprocess_rows!
211211
.find_each do |vaccination_record|
212212
NextDoseTriageFactory.call(vaccination_record:)
213213
end
214+
end
214215

216+
def post_commit!
215217
PatientTeamUpdater.call(patient_scope: patients)
216218
PatientStatusUpdater.call(patient_scope: Patient.where(id: patients.ids))
217-
end
218219

219-
def post_commit!
220220
vaccination_records.sync_all_to_nhs_immunisations_api
221221

222222
vaccination_records

0 commit comments

Comments
 (0)