Skip to content

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

Optimize 0044_fulfill_checkout_line_new_fields migration (#12078)

Tomasz Szymański4986350865fb
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 ecc98348dc..cbb26042db 100644
--- a/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.py
+++ b/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.py
@@ -1,19 +1,28 @@
# Generated by Django 3.2.13 on 2022-04-29 08:51
from django.db import migrations
-from django.db.models import F, OuterRef, Subquery
+from django.db.models import F, OuterRef, Subquery, Case, When
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):
+def set_checkout_line_token_old_id_and_created_at(apps, _schema_editor):
CheckoutLine = apps.get_model("checkout", "CheckoutLine")
Checkout = apps.get_model("checkout", "Checkout")
- CheckoutLine.objects.filter(token__isnull=True).update(
- token=RandomUUID(),
- created_at=Subquery(
- Checkout.objects.filter(lines=OuterRef("id")).values("created_at")[:1]
+ CheckoutLine.objects.update(
+ old_id=F("id"),
+ token=Case(When(token__isnull=True, then=RandomUUID()), default="token"),
+ created_at=Case(
+ When(
+ token__isnull=True,
+ then=Subquery(
+ Checkout.objects.filter(lines=OuterRef("id")).values("created_at")[
+ :1
+ ]
+ ),
+ ),
+ default="created_at",
),
)
@@ -31,10 +40,7 @@ class Migration(migrations.Migration):
operations = [
CryptoExtension(),
migrations.RunPython(
- set_checkout_line_token_and_created_at,
+ set_checkout_line_token_old_id_and_created_at,
migrations.RunPython.noop,
),
- migrations.RunPython(
- set_checkout_line_old_id, reverse_code=migrations.RunPython.noop
- ),
]