Skip to content

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

Rewrite order 0200 migration to standard migration (#17591)

Iga Karbowiak3948cb6c8399
diff --git a/saleor/order/migrations/0200_order_with_waiting_for_approval_fulfillments.py b/saleor/order/migrations/0200_order_with_waiting_for_approval_fulfillments.py
index 94c6ed5e35..603a34ba9d 100644
--- a/saleor/order/migrations/0200_order_with_waiting_for_approval_fulfillments.py
+++ b/saleor/order/migrations/0200_order_with_waiting_for_approval_fulfillments.py
@@ -1,16 +1,41 @@
-from django.apps import apps as registry
-from django.db import migrations
-from django.db.models.signals import post_migrate
+from django.db import migrations, transaction
+from django.db.models import Exists, OuterRef
-from .tasks.saleor3_21 import migrate_orders_with_waiting_for_approval_fulfillment_task
+# The batch of size 250 takes ~0.2 second and consumes ~15MB memory at peak
+BATCH_SIZE = 250
def migrate_orders_with_waiting_for_approval_fulfillment(apps, _schema_editor):
- def on_migrations_complete(sender=None, **kwargs):
- migrate_orders_with_waiting_for_approval_fulfillment_task.delay()
+ Order = apps.get_model("order", "Order")
+ Fulfillment = apps.get_model("order", "Fulfillment")
- sender = registry.get_app_config("order")
- post_migrate.connect(on_migrations_complete, weak=False, sender=sender)
+ waiting_for_approval_fulfillments = Fulfillment.objects.filter(
+ status="waiting_for_approval"
+ )
+ fulfilled_fulfillments = Fulfillment.objects.filter(status="fulfilled")
+ # get orders that has at least one waiting for approval fulfillment and not
+ # fulfilled fulfillment
+ qs = Order.objects.filter(
+ Exists(waiting_for_approval_fulfillments.filter(order_id=OuterRef("id"))),
+ ~Exists(fulfilled_fulfillments.filter(order_id=OuterRef("id"))),
+ status="partially fulfilled",
+ ).order_by("pk")
+ for ids in queryset_in_batches(qs):
+ orders = Order.objects.filter(id__in=ids).order_by("pk")
+ with transaction.atomic():
+ _orders_lock = list(orders.select_for_update(of=(["self"])))
+ orders.update(status="unfulfilled")
+
+
+def queryset_in_batches(queryset):
+ 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]
class Migration(migrations.Migration):
diff --git a/saleor/order/migrations/tasks/saleor3_21.py b/saleor/order/migrations/tasks/saleor3_21.py
deleted file mode 100644
index 4dcdfdc9bf..0000000000
--- a/saleor/order/migrations/tasks/saleor3_21.py
+++ /dev/null
@@ -1,40 +0,0 @@
-from django.db import transaction
-from django.db.models import Exists, OuterRef
-
-from ....celeryconf import app
-from ....core.db.connection import allow_writer
-from ... import FulfillmentStatus, OrderStatus
-from ...models import Fulfillment, Order
-
-# The batch of size 250 takes ~0.2 second and consumes ~15MB memory at peak
-ORDER_BATCH_SIZE = 250
-
-
-@allow_writer()
-def migrate_orders_with_waiting_for_approval_fulfillment_task():
- """Migrate order with waiting for approval fulfillment to unfulfilled status.
-
- If the order has a fulfillment with waiting for approval status, it will be set to unfulfilled,
- instead of `partially_fulfilled` status.
- """
- waiting_for_approval_fulfillments = Fulfillment.objects.filter(
- status=FulfillmentStatus.WAITING_FOR_APPROVAL
- )
- fulfilled_fulfillments = Fulfillment.objects.filter(
- status=FulfillmentStatus.FULFILLED
- )
- # get orders that has at least one waiting for approval fulfillment and not
- # fulfilled fulfillment
- orders = Order.objects.filter(
- Exists(waiting_for_approval_fulfillments.filter(order_id=OuterRef("id"))),
- ~Exists(fulfilled_fulfillments.filter(order_id=OuterRef("id"))),
- status=OrderStatus.PARTIALLY_FULFILLED,
- ).order_by("pk")
- ids = orders.values_list("pk", flat=True)[:ORDER_BATCH_SIZE]
- if ids:
- orders = Order.objects.filter(id__in=ids).order_by("pk")
- with transaction.atomic():
- _orders_lock = list(orders.select_for_update(of=(["self"])))
- orders.update(status=OrderStatus.UNFULFILLED)
- migrate_orders_with_waiting_for_approval_fulfillment_task.delay()