Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Optimize 0041_fulfill_orderdiscount_token_created_at_old_id migration (#10009)

Tomasz Szymański60b6327ff3c9
diff --git a/saleor/discount/migrations/0041_fulfill_orderdiscount_token_created_at_old_id.py b/saleor/discount/migrations/0041_fulfill_orderdiscount_token_created_at_old_id.py
index fd7e384dbc..649858c1e0 100644
--- a/saleor/discount/migrations/0041_fulfill_orderdiscount_token_created_at_old_id.py
+++ b/saleor/discount/migrations/0041_fulfill_orderdiscount_token_created_at_old_id.py
@@ -1,51 +1,27 @@
# Generated by Django 3.2.13 on 2022-05-02 11:06
-
-from django.db import migrations
-from django.db.models import F
-import uuid
import datetime
import pytz
-BATCH_SIZE = 10000
+from django.db import migrations
+from django.db.models import F, OuterRef, Subquery
+from django.contrib.postgres.functions import RandomUUID
+from django.contrib.postgres.operations import CryptoExtension
+from django.db.models.functions import Coalesce
def set_order_discount_token_and_created_at(apps, _schema_editor):
OrderDiscount = apps.get_model("discount", "OrderDiscount")
Order = apps.get_model("order", "Order")
- queryset = OrderDiscount.objects.filter(token__isnull=True)
- for batch_pks in queryset_in_batches(queryset):
- discounts = OrderDiscount.objects.filter(pk__in=batch_pks)
- order_in_bulk = Order.objects.in_bulk(
- discounts.values_list("order_id", flat=True)
- )
- for discount in discounts:
- order = order_in_bulk.get(discount.order_id)
- discount.created_at = (
- order.created_at if order else datetime.datetime.now(pytz.UTC)
- )
- discount.token = uuid.uuid4()
-
- OrderDiscount.objects.bulk_update(discounts, ["token", "created_at"])
-
-
-def queryset_in_batches(queryset):
- """Slice a queryset into batches.
-
- Input queryset should be sorted be pk.
- """
- start_pk = 0
-
- while True:
- qs = queryset.order_by("pk").filter(pk__gt=start_pk)[:BATCH_SIZE]
- pks = list(qs.values_list("pk", flat=True))
-
- if not pks:
- break
-
- yield pks
-
- start_pk = pks[-1]
+ OrderDiscount.objects.filter(token__isnull=True).update(
+ token=RandomUUID(),
+ created_at=Coalesce(
+ Subquery(
+ Order.objects.filter(discounts=OuterRef("id")).values("created_at")[:1]
+ ),
+ datetime.datetime.now(pytz.UTC),
+ ),
+ )
def set_order_discount_old_id(apps, schema_editor):
@@ -60,6 +36,7 @@ class Migration(migrations.Migration):
]
operations = [
+ CryptoExtension(),
migrations.RunPython(
set_order_discount_token_and_created_at,
migrations.RunPython.noop,