From 9020deafd8d7a8baa68e9405b436f570f95d6628 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Wed, 14 Aug 2024 17:40:10 +0530 Subject: [PATCH] Adds custom migration to rename existing event records --- ...tilator_fi02_dailyround_ventilator_fio2.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/care/facility/migrations/0448_rename_ventilator_fi02_dailyround_ventilator_fio2.py b/care/facility/migrations/0448_rename_ventilator_fi02_dailyround_ventilator_fio2.py index 19df9f49f0..9731297e08 100644 --- a/care/facility/migrations/0448_rename_ventilator_fi02_dailyround_ventilator_fio2.py +++ b/care/facility/migrations/0448_rename_ventilator_fi02_dailyround_ventilator_fio2.py @@ -1,9 +1,60 @@ # Generated by Django 4.2.10 on 2024-08-13 10:36 +from django.core.paginator import Paginator from django.db import migrations class Migration(migrations.Migration): + def forward_rename_dailyround_events(apps, schema_editor): + EventType = apps.get_model("facility", "EventType") + PatientConsultationEvent = apps.get_model( + "facility", "PatientConsultationEvent" + ) + + event_type_id = EventType.objects.get(name="RESPIRATORY_SUPPORT").id + paginator = Paginator( + PatientConsultationEvent.objects.filter( + object_model="DailyRound", + event_type_id=event_type_id, + value__has_key="ventilator_fi02", + ).order_by("id"), + 1000, + ) + + for page_number in paginator.page_range: + bulk = [] + for instance in paginator.page(page_number).object_list: + instance.value["ventilator_fio2"] = instance.value.pop( + "ventilator_fi02" + ) + bulk.append(instance) + PatientConsultationEvent.objects.bulk_update(bulk, ["value"]) + + def reverse_rename_dailyround_events(apps, schema_editor): + EventType = apps.get_model("facility", "EventType") + PatientConsultationEvent = apps.get_model( + "facility", "PatientConsultationEvent" + ) + + event_type_id = EventType.objects.get(name="RESPIRATORY_SUPPORT").id + paginator = Paginator( + PatientConsultationEvent.objects.filter( + object_model="DailyRound", + event_type_id=event_type_id, + value__has_key="ventilator_fio2", + ).order_by("id"), + 1000, + ) + + for page_number in paginator.page_range: + bulk = [] + for instance in paginator.page(page_number).object_list: + instance.value["ventilator_fi02"] = instance.value.pop( + "ventilator_fio2" + ) + bulk.append(instance) + PatientConsultationEvent.objects.bulk_update(bulk, ["value"]) + dependencies = [ ("facility", "0447_patientconsultationevent_taken_at"), ] @@ -14,4 +65,8 @@ class Migration(migrations.Migration): old_name="ventilator_fi02", new_name="ventilator_fio2", ), + migrations.RunPython( + forward_rename_dailyround_events, + reverse_code=reverse_rename_dailyround_events, + ), ]