Skip to content

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

Add replica usage to more DB queries (#15132)

Marcin Gębala8d94c8197c8f
diff --git a/saleor/graphql/warehouse/dataloaders.py b/saleor/graphql/warehouse/dataloaders.py
index 1a2ca4a600..1b3fae7fa9 100644
--- a/saleor/graphql/warehouse/dataloaders.py
+++ b/saleor/graphql/warehouse/dataloaders.py
@@ -222,7 +222,7 @@ class AvailableQuantityByProductVariantIdCountryCodeAndChannelSlugLoader(
)
.values("warehouse_id")
)
- warehouses = Warehouse.objects.filter(
+ warehouses = Warehouse.objects.using(self.database_connection_name).filter(
Exists(warehouse_channels.filter(warehouse_id=OuterRef("id"))),
click_and_collect_option__in=[
WarehouseClickAndCollectOption.LOCAL_STOCK,
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 8943f9d934..c3fbd60706 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -1861,9 +1861,11 @@ class PluginsManager(PaymentInterface):
def _get_all_plugin_configs(self):
with opentracing.global_tracer().start_active_span("_get_all_plugin_configs"):
if not hasattr(self, "_plugin_configs"):
- plugin_configurations = PluginConfiguration.objects.prefetch_related(
- "channel"
- ).all()
+ plugin_configurations = (
+ PluginConfiguration.objects.using(self.database)
+ .prefetch_related("channel")
+ .all()
+ )
self._plugin_configs_per_channel: defaultdict[
Channel, dict
] = defaultdict(dict)
diff --git a/saleor/product/managers.py b/saleor/product/managers.py
index 90c751408b..c9685cc5d4 100644
--- a/saleor/product/managers.py
+++ b/saleor/product/managers.py
@@ -2,6 +2,7 @@ import datetime
from typing import Optional, Union
import pytz
+from django.conf import settings
from django.contrib.postgres.aggregates import StringAgg
from django.db import models
from django.db.models import (
@@ -33,7 +34,9 @@ class ProductsQueryset(models.QuerySet):
today = datetime.datetime.now(pytz.UTC)
if channel := (
- Channel.objects.filter(slug=str(channel_slug), is_active=True).first()
+ Channel.objects.using(settings.DATABASE_CONNECTION_REPLICA_NAME)
+ .filter(slug=str(channel_slug), is_active=True)
+ .first()
):
channel_listings = ProductChannelListing.objects.filter(
Q(published_at__lte=today) | Q(published_at__isnull=True),