Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TN-3313 Robustly handle race case of an updating adherence_row #4402

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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