Skip to content

Commit

Permalink
Adds custom migration to rename existing event records
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Aug 14, 2024
1 parent cba042a commit 9020dea
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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"),
]
Expand All @@ -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,
),
]

0 comments on commit 9020dea

Please sign in to comment.