Skip to content

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

Fix checkout shipping addresses which had reference to warehouse addresses (#15747)

Arturde8e3a5085e1
diff --git a/saleor/checkout/migrations/0063_auto_20240402_1114.py b/saleor/checkout/migrations/0063_auto_20240402_1114.py
new file mode 100644
index 0000000000..2267d5dd53
--- /dev/null
+++ b/saleor/checkout/migrations/0063_auto_20240402_1114.py
@@ -0,0 +1,17 @@
+# Generated by Django 3.2.22 on 2024-04-02 11:14
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ (
+ "checkout",
+ "0062_update_checkout_last_transaction_modified_at_and_refundable",
+ )
+ ]
+
+ # Execution of a task moved to 0067_auto_20240405_0756.py.
+ # The migration task was not triggered due to missing proper celeryconf setup.
+ # Migration file left for history consistency.
+ operations = []
diff --git a/saleor/checkout/migrations/0066_merge_0063_auto_20240402_1114_0065_checkout_tax_error.py b/saleor/checkout/migrations/0066_merge_0063_auto_20240402_1114_0065_checkout_tax_error.py
new file mode 100644
index 0000000000..3737c768b4
--- /dev/null
+++ b/saleor/checkout/migrations/0066_merge_0063_auto_20240402_1114_0065_checkout_tax_error.py
@@ -0,0 +1,11 @@
+# Generated by Django 3.2.25 on 2024-04-03 08:09
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("checkout", "0063_auto_20240402_1114"),
+ ("checkout", "0065_checkout_tax_error"),
+ ]
+ operations = []
diff --git a/saleor/checkout/migrations/0067_auto_20240405_0756.py b/saleor/checkout/migrations/0067_auto_20240405_0756.py
new file mode 100644
index 0000000000..d8fd5ccb81
--- /dev/null
+++ b/saleor/checkout/migrations/0067_auto_20240405_0756.py
@@ -0,0 +1,55 @@
+# Generated by Django 3.2.25 on 2024-04-05 07:56
+
+from django.db import migrations
+from django.db.models.expressions import Exists, OuterRef
+from django.forms import model_to_dict
+
+# The batch of size 250 takes ~0.5 second and consumes ~20MB memory at peak
+ADDRESS_UPDATE_BATCH_SIZE = 250
+
+
+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)[:ADDRESS_UPDATE_BATCH_SIZE]
+ pks = list(qs.values_list("pk", flat=True))
+ if not pks:
+ break
+ yield pks
+ start_pk = pks[-1]
+
+
+def update_checkout_addresses(apps, schema_editor):
+ Checkout = apps.get_model("checkout", "Checkout")
+ Warehouse = apps.get_model("warehouse", "Warehouse")
+ Address = apps.get_model("account", "Address")
+
+ queryset = Checkout.objects.filter(
+ Exists(Warehouse.objects.filter(address_id=OuterRef("shipping_address_id"))),
+ ).order_by("pk")
+
+ for batch_pks in queryset_in_batches(queryset):
+ checkouts = Checkout.objects.filter(pk__in=batch_pks)
+ addresses = []
+ for checkout in checkouts:
+ if cc_address := checkout.shipping_address:
+ checkout_address = Address(**model_to_dict(cc_address, exclude=["id"]))
+ checkout.shipping_address = checkout_address
+ addresses.append(checkout_address)
+ Address.objects.bulk_create(addresses, ignore_conflicts=True)
+ Checkout.objects.bulk_update(checkouts, ["shipping_address"])
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("checkout", "0066_merge_0063_auto_20240402_1114_0065_checkout_tax_error"),
+ ]
+
+ operations = [
+ migrations.RunPython(update_checkout_addresses, migrations.RunPython.noop),
+ ]