Skip to content

Commit

Permalink
add migration to populate attachements xform
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Apr 22, 2024
1 parent 1aef912 commit 34d4688
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions onadata/apps/logger/migrations/0014_populate_attachment_xform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.2.11 on 2024-04-22 06:42

from django.db import migrations


def populate_attachment_xform(apps, schema_editor):
"""Populate xform field for Attachments"""
Attachment = apps.get_model("logger", "Attachments")
queryset = Attachment.objects.filter(xform__isnull=True)
count = queryset.count()
print("Start populating attachment xform...")
print(f"Found {count} records")

for attachment in queryset.iterator(chunk_size=100):
# We do not want to trigger Model.save or any signal
# Queryset.update is a workaround to achieve this.
# Instance.save and the post/pre signals may contain
# some side-effects which we are not interested in e.g
# updating date_modified which we do not want
Attachment.objects.filter(pk=attachment.pk).update(
xform=attachment.instance.xform
)
count -= 1
print("f{count} remaining")

print("Done populating attachment xform!")


class Migration(migrations.Migration):

dependencies = [
("logger", "0013_add_xform_to_logger_attachment"),
]

operations = [migrations.RunPython(populate_attachment_xform)]

0 comments on commit 34d4688

Please sign in to comment.