Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Optimize queries in migration 0139_fix_undiscounted_total_on_lines (#11951)
Krzysztof Kwaśniakce90f7a0ce02
diff --git a/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py b/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.pyindex 4d98635a00..09d5fa1614 100644--- a/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py+++ b/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py@@ -1,32 +1,38 @@from functools import partialfrom django.apps import apps as registry-from django.db import migrations-from django.db.models import F, Q+from django.db import connection, migrationsfrom django.db.models.signals import post_migratefrom saleor.order.tasks import send_order_updated-BATCH_SIZE = 500--def queryset_in_batches(queryset):- """Slice a queryset into batches.-- Input queryset should be sorted be pk.- """- start_pk = 0-- while True:- qs = queryset.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]+RAW_SQL = """+ UPDATE "order_orderline"+ SET+ "undiscounted_total_price_gross_amount" = CASE+ WHEN NOT (+ ("order_orderline"."undiscounted_unit_price_gross_amount" * "order_orderline"."quantity") =+ "order_orderline"."undiscounted_total_price_gross_amount")+ THEN ("order_orderline"."undiscounted_unit_price_gross_amount" * "order_orderline"."quantity")+ ELSE "order_orderline"."undiscounted_total_price_gross_amount" END,++ "undiscounted_total_price_net_amount" = CASE+ WHEN NOT (+ ("order_orderline"."undiscounted_unit_price_net_amount" * "order_orderline"."quantity") =+ "order_orderline"."undiscounted_total_price_net_amount")+ THEN ("order_orderline"."undiscounted_unit_price_net_amount" * "order_orderline"."quantity")+ ELSE "order_orderline"."undiscounted_total_price_net_amount" END++ WHERE (+ NOT ("order_orderline"."undiscounted_total_price_gross_amount" =+ ("order_orderline"."undiscounted_unit_price_gross_amount" *+ "order_orderline"."quantity")) OR+ NOT ("order_orderline"."undiscounted_total_price_net_amount" =+ ("order_orderline"."undiscounted_unit_price_net_amount" *+ "order_orderline"."quantity")))+ RETURNING "order_orderline"."id";+""" # noqa: E501def set_order_line_base_prices(apps, schema_editor):@@ -34,53 +40,12 @@ def set_order_line_base_prices(apps, schema_editor):order_ids = list(kwargs.get("updated_orders_pks"))send_order_updated.delay(order_ids)- OrderLine = apps.get_model("order", "OrderLine")- order_lines_to_update = OrderLine.objects.filter(- ~Q(- undiscounted_total_price_gross_amount=F(- "undiscounted_unit_price_gross_amount"- )- * F("quantity")- )- | ~Q(- undiscounted_total_price_net_amount=F("undiscounted_unit_price_net_amount")- * F("quantity")- )- ).order_by("pk")- updated_orders_pks = []- for batch_pks in queryset_in_batches(order_lines_to_update):- order_lines = OrderLine.objects.filter(pk__in=batch_pks)- for order_line in order_lines:- old_undiscounted_total_price_gross_amount = (- order_line.undiscounted_total_price_gross_amount- )- old_undiscounted_total_price_net_amount = (- order_line.undiscounted_total_price_net_amount- )- order_line.undiscounted_total_price_gross_amount = (- order_line.undiscounted_unit_price_gross_amount * order_line.quantity- )- order_line.undiscounted_total_price_net_amount = (- order_line.undiscounted_unit_price_net_amount * order_line.quantity- )- if (- order_line.undiscounted_total_price_gross_amount- != old_undiscounted_total_price_gross_amount- or order_line.undiscounted_total_price_net_amount- != old_undiscounted_total_price_net_amount- ):- updated_orders_pks.append(order_line.order_id)- OrderLine.objects.bulk_update(- order_lines,- [- "undiscounted_total_price_gross_amount",- "undiscounted_total_price_net_amount",- ],- )+ with connection.cursor() as cursor:+ cursor.execute(RAW_SQL)+ records = cursor.fetchall()- # If we updated any order we should trigger `order_updated` after migrations+ updated_orders_pks = {record[0] for record in records}if updated_orders_pks:- updated_orders_pks = set(updated_orders_pks)sender = registry.get_app_config("order")post_migrate.connect(partial(on_migrations_complete, updated_orders_pks=updated_orders_pks),@@ -91,7 +56,6 @@ def set_order_line_base_prices(apps, schema_editor):class Migration(migrations.Migration):-dependencies = [("order", "0138_orderline_base_price"),]