-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add migration to populate attachements xform
- Loading branch information
1 parent
1aef912
commit 34d4688
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
onadata/apps/logger/migrations/0014_populate_attachment_xform.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |