From 4a53cdebec3ba8b48e48356372c738adbd383d04 Mon Sep 17 00:00:00 2001 From: Paul Bugni Date: Wed, 20 Sep 2023 14:49:29 -0700 Subject: [PATCH] expand migration downgrade step, to remove all sequential hard domain counts, so the test db can be switched over to a hotfix without this migration. --- portal/migrations/versions/80c3b1e96c45_.py | 31 ++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/portal/migrations/versions/80c3b1e96c45_.py b/portal/migrations/versions/80c3b1e96c45_.py index fee8ec078..304fdc509 100644 --- a/portal/migrations/versions/80c3b1e96c45_.py +++ b/portal/migrations/versions/80c3b1e96c45_.py @@ -81,4 +81,33 @@ def upgrade(): def downgrade(): - pass # no value in removing + # for each active EMPRO patient with at least 1 hard triggered domain, + # remove any sequential counts found + bind = op.get_bind() + session = Session(bind=bind) + + patient_ids = [] + for patient_id in session.execute( + "SELECT DISTINCT(user_id) FROM trigger_states JOIN users" + " ON users.id = user_id WHERE deleted_id IS NULL"): + patient_ids.append(patient_id[0]) + + output = StringIO() + for pid in patient_ids: + output.write(f"\n\nPatient: {pid}\n") + trigger_states = db.session.query(TriggerState).filter( + TriggerState.user_id == pid).filter( + TriggerState.state == "resolved").order_by( + TriggerState.timestamp.asc()) + for ts in trigger_states: + improved_triggers = deepcopy(ts.triggers) + for d in EMPRO_DOMAINS: + if sequential_hard_trigger_count_key in improved_triggers["domain"][d]: + del improved_triggers["domain"][d][sequential_hard_trigger_count_key] + output.write(f" removed sequential from {ts.visit_month}:{d} {improved_triggers['domain'][d]}\n") + + # retain triggers now containing sequential counts + ts.triggers = improved_triggers + + db.session.commit() + print(output.getvalue())