Skip to content

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

Ensure CheckoutDeliveries are deleted before deleting Checkouts (#19312)

Marcin Wcisło361c6d8a45dc
diff --git a/saleor/core/management/commands/clearorders.py b/saleor/core/management/commands/clearorders.py
index cb72126827..763fe635ce 100644
--- a/saleor/core/management/commands/clearorders.py
+++ b/saleor/core/management/commands/clearorders.py
@@ -8,7 +8,12 @@ configuration, such as: warehouses, shipping zones, staff accounts, plugin confi
from django.core.management.base import BaseCommand
from ....account.models import Address, CustomerEvent, CustomerNote, User
-from ....checkout.models import Checkout, CheckoutLine, CheckoutMetadata
+from ....checkout.models import (
+ Checkout,
+ CheckoutDelivery,
+ CheckoutLine,
+ CheckoutMetadata,
+)
from ....discount.models import (
CheckoutDiscount,
CheckoutLineDiscount,
@@ -90,6 +95,9 @@ class Command(BaseCommand):
checkout_lines = CheckoutLine.objects.all()
checkout_lines._raw_delete(checkout_lines.db)
+ checkout_deliveries = CheckoutDelivery.objects.all()
+ checkout_deliveries._raw_delete(checkout_deliveries.db)
+
checkout = Checkout.objects.all()
checkout._raw_delete(checkout.db)
self.stdout.write("Removed checkouts")
diff --git a/saleor/core/tests/commands/test_clearorders.py b/saleor/core/tests/commands/test_clearorders.py
new file mode 100644
index 0000000000..a17b248eea
--- /dev/null
+++ b/saleor/core/tests/commands/test_clearorders.py
@@ -0,0 +1,19 @@
+import pytest
+from django.core.management import call_command
+
+from ....checkout.models import Checkout, CheckoutDelivery
+
+
+def test_delete_checkouts_with_checkout_delivery(checkout, checkout_delivery):
+ # given
+ delivery = checkout_delivery(checkout)
+ assert Checkout.objects.filter(pk=checkout.pk).exists()
+ assert CheckoutDelivery.objects.filter(pk=delivery.pk).exists()
+
+ # when
+ call_command("clearorders")
+
+ # then
+ assert not Checkout.objects.filter(pk=checkout.pk).exists()
+ assert not CheckoutDelivery.objects.filter(pk=delivery.pk).exists()