Skip to content

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

Fix usage of replica in get_webhooks_for_event (#14171)

Marcin Gębaladc08f06119f5
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 30aac76b28..3dc41895d3 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -15,6 +15,7 @@ from typing import (
)
import graphene
+from django.conf import settings
from ...app.models import App
from ...checkout.fetch import CheckoutInfo, CheckoutLineInfo
@@ -2446,12 +2447,16 @@ class WebhookPlugin(BasePlugin):
if payment_app_data is not None:
if payment_app_data.app_identifier:
- apps = App.objects.for_event_type(event_type).filter(
- identifier=payment_app_data.app_identifier
+ apps = (
+ App.objects.using(settings.DATABASE_CONNECTION_REPLICA_NAME)
+ .for_event_type(event_type)
+ .filter(identifier=payment_app_data.app_identifier)
)
else:
- apps = App.objects.for_event_type(event_type).filter(
- pk=payment_app_data.app_pk
+ apps = (
+ App.objects.using(settings.DATABASE_CONNECTION_REPLICA_NAME)
+ .for_event_type(event_type)
+ .filter(pk=payment_app_data.app_pk)
)
if not apps:
diff --git a/saleor/webhook/utils.py b/saleor/webhook/utils.py
index 4814e31a0d..409cca913b 100644
--- a/saleor/webhook/utils.py
+++ b/saleor/webhook/utils.py
@@ -32,9 +32,9 @@ def get_webhooks_for_event(
# no risk that any mutation would change the result of these querysets.
if webhooks is None:
- webhooks = Webhook.objects.using(
- settings.DATABASE_CONNECTION_REPLICA_NAME
- ).all()
+ # For this QS replica usage is applied later, as this QS could be also passed
+ # as parameter.
+ webhooks = Webhook.objects.all()
app_kwargs: dict = {"is_active": True, **permissions}
if apps_ids:
@@ -53,7 +53,8 @@ def get_webhooks_for_event(
settings.DATABASE_CONNECTION_REPLICA_NAME
).filter(event_type__in=event_types)
return (
- webhooks.filter(
+ webhooks.using(settings.DATABASE_CONNECTION_REPLICA_NAME)
+ .filter(
Q(is_active=True, app__in=apps)
& Q(Exists(webhook_events.filter(webhook_id=OuterRef("id"))))
)