Skip to content

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

Optimize queries in migration order 0158_migrate_base_shipping_price_amount (#11929)

Krzysztof Kwaśniakbab3e4a1d467
diff --git a/saleor/order/migrations/0158_migrate_base_shipping_price_amount.py b/saleor/order/migrations/0158_migrate_base_shipping_price_amount.py
index 8c7040b861..56e86e8cc4 100644
--- a/saleor/order/migrations/0158_migrate_base_shipping_price_amount.py
+++ b/saleor/order/migrations/0158_migrate_base_shipping_price_amount.py
@@ -1,7 +1,7 @@
# Generated by Django 4.0.7 on 2022-09-26 10:15
from django.db import migrations
-from django.db.models import F
+from django.db.models import Case, When
AVATAX_PLUGIN_ID = "mirumee.taxes.avalara"
@@ -12,8 +12,8 @@ def set_base_shipping_price(apps, _schema_editor):
SiteSettings = apps.get_model("site", "SiteSettings")
Channel = apps.get_model("channel", "Channel")
- channel_ids_with_tax_included = []
- channel_ids_with_tax_excluded = []
+ channel_ids_with_tax_included = set()
+ channel_ids_with_tax_excluded = set()
# Fetch channel ids with overrode `SiteSettings.include_taxes_in_prices`
avatax_configs = PluginConfiguration.objects.filter(
@@ -25,38 +25,44 @@ def set_base_shipping_price(apps, _schema_editor):
if override_global_tax:
include_taxes_in_prices = config_dict.get("include_taxes_in_prices")
if include_taxes_in_prices:
- channel_ids_with_tax_included.append(config.channel_id)
+ channel_ids_with_tax_included.add(config.channel_id)
else:
- channel_ids_with_tax_excluded.append(config.channel_id)
+ channel_ids_with_tax_excluded.add(config.channel_id)
# Fetch channel ids with default settings
- channel_ids_with_override_config = (
- channel_ids_with_tax_included + channel_ids_with_tax_excluded
+ channel_ids_with_override_config = channel_ids_with_tax_included.union(
+ channel_ids_with_tax_excluded
)
site_settings = SiteSettings.objects.first()
channel_ids_without_override_config = Channel.objects.exclude(
id__in=channel_ids_with_override_config
).values_list("id", flat=True)
if site_settings and site_settings.include_taxes_in_prices:
- channel_ids_with_tax_included.extend(channel_ids_without_override_config)
+ channel_ids_with_tax_included.update(channel_ids_without_override_config)
else:
- channel_ids_with_tax_excluded.extend(channel_ids_without_override_config)
+ channel_ids_with_tax_excluded.update(channel_ids_without_override_config)
# Set base shipping price for channels with tax included in prices
- if channel_ids_with_tax_included:
- Order.objects.filter(channel_id__in=channel_ids_with_tax_included).update(
- base_shipping_price_amount=F("shipping_price_gross_amount")
+ Order.objects.filter(
+ channel_id__in=channel_ids_with_tax_included.union(
+ channel_ids_with_tax_excluded
)
-
- # Set base shipping price for channels with tax excluded in prices
- if channel_ids_with_tax_excluded:
- Order.objects.filter(channel_id__in=channel_ids_with_tax_excluded).update(
- base_shipping_price_amount=F("shipping_price_net_amount")
+ ).update(
+ base_shipping_price_amount=Case(
+ When(
+ channel_id__in=channel_ids_with_tax_included,
+ then="shipping_price_gross_amount",
+ ),
+ When(
+ channel_id__in=channel_ids_with_tax_excluded,
+ then="shipping_price_net_amount",
+ ),
+ default="base_shipping_price_amount",
)
+ )
class Migration(migrations.Migration):
-
dependencies = [
("order", "0157_order_base_shipping_price_amount"),
("site", "0034_sitesettings_limit_quantity_per_checkout"),