Skip to content

Commit

Permalink
TN-3313 Robustly handle race case of an updating adherence_row (#4402)
Browse files Browse the repository at this point in the history
Add a check for existing row before attempted insert, to prevent race
case observed in logs. See
[TN-3313](https://movember.atlassian.net/browse/TN-3313) for details.
  • Loading branch information
pbugni authored and ivan-c committed Sep 10, 2024
1 parent 7f790be commit 9f7c5c5
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions portal/models/adherence_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,20 @@ def persist(patient_id, rs_id_visit, valid_for_days, data):
except TypeError:
raise ValueError(f"couldn't encode {k}:{v}, {type(v)}")

record = AdherenceData(
patient_id=patient_id,
rs_id_visit=rs_id_visit,
valid_till=valid_till,
data=data)
db.session.add(record)
# only a single row for a given patient, rs_id_visit allowed. replace or add
record = AdherenceData.query.filter(
AdherenceData.patient_id == patient_id).filter(
AdherenceData.rs_id_visit == rs_id_visit).first()
if record:
record.valid_till = valid_till
record.data = data
else:
record = AdherenceData(
patient_id=patient_id,
rs_id_visit=rs_id_visit,
valid_till=valid_till,
data=data)
db.session.add(record)
db.session.commit()
return db.session.merge(record)

Expand Down

0 comments on commit 9f7c5c5

Please sign in to comment.