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

Add xform to attachment model #2587

Merged
merged 11 commits into from
Apr 23, 2024
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)]