Skip to content

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

Optimize Order line is gift card migration (#8427)

Iga Karbowiak10bc09b976cf
diff --git a/saleor/order/migrations/0119_orderline_is_gift_card.py b/saleor/order/migrations/0119_orderline_is_gift_card.py
index c1f4fa80df..0eb7c73329 100644
--- a/saleor/order/migrations/0119_orderline_is_gift_card.py
+++ b/saleor/order/migrations/0119_orderline_is_gift_card.py
@@ -1,17 +1,32 @@
# Generated by Django 3.2.6 on 2021-09-09 12:58
from django.db import migrations, models
+from django.db.models.expressions import Exists, OuterRef
def set_is_gift_card_field(apps, schema_editor):
OrderLine = apps.get_model("order", "OrderLine")
- for line in OrderLine.objects.prefetch_related(
- "variant__product__product_type"
- ).iterator():
- line.is_gift_card = (
- bool(line.variant) and line.variant.product.product_type.kind == "gift_card"
- )
- line.save(update_fields=["is_gift_card"])
+ ProductType = apps.get_model("product", "ProductType")
+ Product = apps.get_model("product", "Product")
+ ProductVariant = apps.get_model("product", "ProductVariant")
+
+ product_types = ProductType.objects.filter(kind="gift_card")
+ products = Product.objects.filter(
+ Exists(product_types.filter(pk=OuterRef("product_type_id")))
+ )
+ variants = ProductVariant.objects.filter(
+ Exists(products.filter(pk=OuterRef("product_id")))
+ )
+
+ gift_card_lines = OrderLine.objects.filter(
+ Exists(variants.filter(pk=OuterRef("variant_id")))
+ )
+ gift_card_lines.update(is_gift_card=True)
+
+ lines = OrderLine.objects.exclude(
+ Exists(variants.filter(pk=OuterRef("variant_id")))
+ )
+ lines.update(is_gift_card=False)
class Migration(migrations.Migration):