Skip to content

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

Add sending order ids in batches in post migration signal (#12052)

Krzysztof Kwaśniakb5f74b217094
diff --git a/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py b/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py
index 09d5fa1614..460e6d71ec 100644
--- a/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py
+++ b/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py
@@ -6,6 +6,9 @@ from django.db.models.signals import post_migrate
from saleor.order.tasks import send_order_updated
+# 5000 results in 246701 bytes (base64(json(list of ids))) which is the limit for SQS
+SEND_ORDER_UPDATED_BATCH_SIZE = 5000
+
RAW_SQL = """
UPDATE "order_orderline"
@@ -31,24 +34,31 @@ RAW_SQL = """
NOT ("order_orderline"."undiscounted_total_price_net_amount" =
("order_orderline"."undiscounted_unit_price_net_amount" *
"order_orderline"."quantity")))
- RETURNING "order_orderline"."id";
+ RETURNING "order_orderline"."order_id";
""" # noqa: E501
-def set_order_line_base_prices(apps, schema_editor):
- def on_migrations_complete(sender=None, **kwargs):
- order_ids = list(kwargs.get("updated_orders_pks"))
- send_order_updated.delay(order_ids)
+def on_migrations_complete(sender=None, **kwargs):
+ order_ids = list(kwargs.get("updated_orders_pks"))
+
+ for index in range(0, len(order_ids), SEND_ORDER_UPDATED_BATCH_SIZE):
+ send_order_updated.delay(
+ order_ids[index : index + SEND_ORDER_UPDATED_BATCH_SIZE]
+ )
+
+def set_order_line_base_prices(apps, schema_editor):
with connection.cursor() as cursor:
cursor.execute(RAW_SQL)
records = cursor.fetchall()
- updated_orders_pks = {record[0] for record in records}
- if updated_orders_pks:
+ if records:
sender = registry.get_app_config("order")
post_migrate.connect(
- partial(on_migrations_complete, updated_orders_pks=updated_orders_pks),
+ partial(
+ on_migrations_complete,
+ updated_orders_pks=[record[0] for record in records],
+ ),
weak=False,
dispatch_uid="send_order_updated",
sender=sender,