django-oscar / django-oscar

Domain-driven e-commerce for Django

Home Page:http://oscarcommerce.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

migration creates performance issue in case of huge records in LineAttribute table

awais786 opened this issue · comments

django-oscar==3.2.2

Issue Summary

This migration brings all records and then update them 1/1.

Proposed solution

Instead doing some thing like this can make it more reliable.

LineAttribute = apps.get_model("basket", "LineAttribute")

batch_size = 100  # Set your desired batch size
update_batch = []

# Use iterator() to avoid loading the entire queryset into memory
for at in LineAttribute.objects.all().iterator():
    try:  # if the value is already valid JSON, continue
        json.loads(at.value)
        continue
    except json.JSONDecodeError:
        pass

    try:  # to parse the string as Python, then convert to JSON, then continue
        val = literal_eval(at.value)
        at.value = json.dumps(val, cls=DjangoJSONEncoder)
        update_batch.append(at)
        continue
    except ValueError:
        pass

 # Perform a bulk update 
    if len(update_batch) >= batch_size:
        LineAttribute.objects.bulk_update(update_batch, ['value'])
        update_batch = []

# Perform a final bulk update for less than 100
if update_batch:
    LineAttribute.objects.bulk_update(update_batch, ['value'])