Skip to content

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

Optimize 0139_fulfil_orderline_token_old_id_created_at migration (#9935)

Tomasz Szymański63073a86b454
diff --git a/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.py b/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.py
index 9e5de462f5..46ed5fd816 100644
--- a/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.py
+++ b/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.py
@@ -1,47 +1,21 @@
# Generated by Django 3.2.13 on 2022-04-29 08:51
from django.db import migrations
-from django.db.models import F
-import uuid
-
-BATCH_SIZE = 10000
+from django.db.models import F, OuterRef, Subquery
+from django.contrib.postgres.functions import RandomUUID
+from django.contrib.postgres.operations import CryptoExtension
def set_checkout_line_token_and_created_at(apps, _schema_editor):
CheckoutLine = apps.get_model("checkout", "CheckoutLine")
Checkout = apps.get_model("checkout", "Checkout")
- queryset = CheckoutLine.objects.filter(token__isnull=True)
- for batch_pks in queryset_in_batches(queryset):
- checkout_lines = CheckoutLine.objects.filter(pk__in=batch_pks)
- checkout_in_bulk = Checkout.objects.in_bulk(
- checkout_lines.values_list("checkout_id", flat=True)
- )
- for checkout_line in checkout_lines:
- checkout = checkout_in_bulk.get(checkout_line.checkout_id)
- checkout_line.created_at = checkout.created_at
- checkout_line.token = uuid.uuid4()
-
- CheckoutLine.objects.bulk_update(checkout_lines, ["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]
+ CheckoutLine.objects.filter(token__isnull=True).update(
+ token=RandomUUID(),
+ created_at=Subquery(
+ Checkout.objects.filter(lines=OuterRef("id")).values("created_at")[:1]
+ ),
+ )
def set_checkout_line_old_id(apps, schema_editor):
@@ -56,6 +30,7 @@ class Migration(migrations.Migration):
]
operations = [
+ CryptoExtension(),
migrations.RunPython(
set_checkout_line_token_and_created_at,
migrations.RunPython.noop,
diff --git a/saleor/order/migrations/0139_fulfil_orderline_token_old_id_created_at.py b/saleor/order/migrations/0139_fulfil_orderline_token_old_id_created_at.py
index a11124a4f0..65a37b7205 100644
--- a/saleor/order/migrations/0139_fulfil_orderline_token_old_id_created_at.py
+++ b/saleor/order/migrations/0139_fulfil_orderline_token_old_id_created_at.py
@@ -1,47 +1,21 @@
# Generated by Django 3.2.12 on 2022-04-11 12:32
from django.db import migrations
-from django.db.models import F
-import uuid
-
-BATCH_SIZE = 10000
+from django.db.models import F, OuterRef, Subquery
+from django.contrib.postgres.functions import RandomUUID
+from django.contrib.postgres.operations import CryptoExtension
def set_order_line_token_and_created_at(apps, _schema_editor):
OrderLine = apps.get_model("order", "OrderLine")
Order = apps.get_model("order", "Order")
- queryset = OrderLine.objects.filter(token__isnull=True)
- for batch_pks in queryset_in_batches(queryset):
- order_lines = OrderLine.objects.filter(pk__in=batch_pks)
- order_in_bulk = Order.objects.in_bulk(
- order_lines.values_list("order_id", flat=True)
- )
- for order_line in order_lines:
- order = order_in_bulk.get(order_line.order_id)
- order_line.created_at = order.created_at
- order_line.token = uuid.uuid4()
-
- OrderLine.objects.bulk_update(order_lines, ["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]
+ OrderLine.objects.filter(token__isnull=True).update(
+ token=RandomUUID(),
+ created_at=Subquery(
+ Order.objects.filter(lines=OuterRef("id")).values("created_at")[:1]
+ ),
+ )
def set_order_line_old_id(apps, schema_editor):
@@ -56,6 +30,7 @@ class Migration(migrations.Migration):
]
operations = [
+ CryptoExtension(),
migrations.RunPython(
set_order_line_token_and_created_at,
migrations.RunPython.noop,