Skip to content

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

Add datamigration for fixing invalid undiscounted total on order lines (#10013)

Filip Owczarekf8f3e8bf3903
diff --git a/saleor/order/__init__.py b/saleor/order/__init__.py
index b3d1d7ee39..585b5eb343 100644
--- a/saleor/order/__init__.py
+++ b/saleor/order/__init__.py
@@ -5,6 +5,9 @@ if TYPE_CHECKING:
from .models import FulfillmentLine
+default_app_config = "saleor.order.app.OrderAppConfig"
+
+
class OrderStatus:
DRAFT = "draft" # fully editable, not finalized order created by staff users
UNCONFIRMED = (
diff --git a/saleor/order/app.py b/saleor/order/app.py
new file mode 100644
index 0000000000..25dd295d84
--- /dev/null
+++ b/saleor/order/app.py
@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class OrderAppConfig(AppConfig):
+ name = "saleor.order"
diff --git a/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py b/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py
new file mode 100644
index 0000000000..e0595414ff
--- /dev/null
+++ b/saleor/order/migrations/0139_fix_undiscounted_total_on_lines.py
@@ -0,0 +1,120 @@
+from functools import partial
+
+from django.db import migrations
+from django.db.models import F, Q
+from django.db.models.signals import post_migrate
+from saleor.plugins.manager import get_plugins_manager
+from saleor.order.app import OrderAppConfig
+from saleor.celeryconf import app
+
+# We need to send `order_updated` for each updated order. When we drop plugin manager
+# we should rewrite `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]
+
+
+def send_order_updated(order_ids):
+ from saleor.order.models import Order
+
+ # We need to import Order in this way because when we import it by
+ # `Order = apps.get_model("order", "Order")` models doesn't contains
+ # model methods which are required by `order_updated`
+
+ manager = get_plugins_manager()
+ for order in Order.objects.filter(id__in=order_ids):
+ manager.order_updated(order)
+
+
+def set_order_line_base_prices(apps, schema_editor):
+ def on_migrations_complete(sender=None, **kwargs):
+ # The `post_migrate`` signal is sent once for every app migrated
+ # we should execute it only once after update `order` module.
+ if isinstance(sender, OrderAppConfig):
+ 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",
+ ],
+ )
+
+ # If we updated any order we should trigger `order_updated` after migrations
+ if updated_orders_pks:
+ updated_orders_pks = set(updated_orders_pks)
+ post_migrate.connect(
+ partial(on_migrations_complete, updated_orders_pks=updated_orders_pks),
+ weak=False,
+ dispatch_uid="send_order_updated",
+ )
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("order", "0138_orderline_base_price"),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ set_order_line_base_prices, reverse_code=migrations.RunPython.noop
+ ),
+ ]
diff --git a/saleor/order/migrations/0151_merge_20220615_1224.py b/saleor/order/migrations/0151_merge_20220615_1224.py
new file mode 100644
index 0000000000..a972409471
--- /dev/null
+++ b/saleor/order/migrations/0151_merge_20220615_1224.py
@@ -0,0 +1,13 @@
+# Generated by Django 3.2.13 on 2022-06-15 12:24
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("order", "0139_fix_undiscounted_total_on_lines"),
+ ("order", "0150_update_authorize_and_charge_status"),
+ ]
+
+ operations = []
diff --git a/saleor/order/migrations/0153_merge_20220615_1232.py b/saleor/order/migrations/0153_merge_20220615_1232.py
new file mode 100644
index 0000000000..0d5bb7a675
--- /dev/null
+++ b/saleor/order/migrations/0153_merge_20220615_1232.py
@@ -0,0 +1,13 @@
+# Generated by Django 3.2.13 on 2022-06-15 12:32
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("order", "0151_merge_20220615_1224"),
+ ("order", "0152_update_order_search_vector"),
+ ]
+
+ operations = []