Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1659 | using iterator and chunks for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Sep 12, 2023
1 parent acd25fb commit db50e83
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions core/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,25 +729,26 @@ def calculate_checksums(resource_type, resource_id):

@app.task(ignore_result=True)
def concepts_update_updated_by(): # pragma: no cover
from django.db.models import F
from core.concepts.models import Concept

for concept in Concept.objects.filter(id=F('versioned_object_id')).iterator(chunk_size=1000):
latest_version = Concept.objects.filter(
versioned_object_id=concept.versioned_object_id, is_latest_version=True).order_by('-created_at').first()
if latest_version:
concept.updated_by = latest_version.updated_by
concept.save(update_fields=['updated_by'])
resource_updated_update_by(Concept)


@app.task(ignore_result=True)
def mappings_update_updated_by(): # pragma: no cover
from django.db.models import F
from core.mappings.models import Mapping
resource_updated_update_by(Mapping)


for mapping in Mapping.objects.filter(id=F('versioned_object_id')).iterator(chunk_size=1000):
latest_version = Mapping.objects.filter(
def resource_updated_update_by(klass):
from django.db.models import F
processed = 0
queryset = klass.objects.filter(id=F('versioned_object_id'))
total = queryset.count()
for mapping in queryset.iterator(chunk_size=10000):
processed += 1
latest_version = klass.objects.filter(
versioned_object_id=mapping.versioned_object_id, is_latest_version=True).order_by('-created_at').first()
if latest_version:
mapping.updated_by = latest_version.updated_by
mapping.save(update_fields=['updated_by'])
print(f"Status: {processed}/{total}")

0 comments on commit db50e83

Please sign in to comment.