:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): load schema source and team in one activity-log query (#126423)
Paul D'Ambrab7bed59d2f52
diff --git a/posthog/test/activity_logging/test_external_data_schema_activity_logging.py b/posthog/test/activity_logging/test_external_data_schema_activity_logging.pyindex 115d93946243..6f8558b83b87 100644--- a/posthog/test/activity_logging/test_external_data_schema_activity_logging.py+++ b/posthog/test/activity_logging/test_external_data_schema_activity_logging.py@@ -1,9 +1,12 @@from typing import get_args+from unittest.mock import patch+from django.utils import timezonefrom posthog.models.activity_logging.activity_log import ActivityScope, field_exclusionsfrom posthog.models.activity_logging.model_activity import ModelActivityMixin+from posthog.models.signals import model_activity_signalfrom posthog.test.activity_log_utils import ActivityLogTestHelperfrom products.warehouse_sources.backend.facade.models import ExternalDataSchema, ExternalDataSource@@ -106,6 +109,29 @@ class TestExternalDataSchemaActivityLogging(ActivityLogTestHelper):changes = activity_logs[0].detail.get("changes", [])self.assertEqual([change["field"] for change in changes], ["enabled"])+ def test_logging_an_update_reads_source_and_team_in_one_query(self):+ external_data_source = self.create_external_data_source()+ schema_id = ExternalDataSchema.objects.filter(source_id=external_data_source["id"]).values_list(+ "id", flat=True+ )[0]+ schema = ExternalDataSchema.objects.get(id=schema_id)++ with patch("products.warehouse_sources.backend.activity_logging.log_activity") as log_activity:+ for expected_queries in (1, 0):+ with self.assertNumQueries(expected_queries):+ model_activity_signal.send(+ sender=ExternalDataSchema,+ scope="ExternalDataSchema",+ before_update=None,+ after_update=schema,+ activity="updated",+ user=self.user,+ )++ assert log_activity.call_args.kwargs["organization_id"] == self.organization.id+ context = log_activity.call_args.kwargs["detail"].context+ assert (context.source_id, context.source_type) == (str(external_data_source["id"]), "Stripe")+def test_external_data_schema_relationship_logging(self):external_data_source = self.create_external_data_source()diff --git a/products/warehouse_sources/backend/activity_logging.py b/products/warehouse_sources/backend/activity_logging.pyindex 48cef55f4517..4b9e77240667 100644--- a/products/warehouse_sources/backend/activity_logging.py+++ b/products/warehouse_sources/backend/activity_logging.py@@ -1,4 +1,5 @@import dataclasses+from uuid import UUIDfrom posthog.models.activity_logging.activity_log import ActivityContextBase, Detail, changes_between, log_activityfrom posthog.models.signals import model_activity_signal, mutable_receiver@@ -28,6 +29,17 @@ class ExternalDataSchemaContext(ActivityContextBase):source_type: str+def _source_and_organization_id(schema: ExternalDataSchema) -> tuple[ExternalDataSource, UUID]:+ if not (ExternalDataSchema.source.is_cached(schema) and ExternalDataSchema.team.is_cached(schema)):+ # Lazy loads read through `_base_manager` too, so this join finds the same rows the two lazy loads did.+ source = ExternalDataSource._base_manager.select_related("team").get(pk=schema.source_id)+ # Fill the relation caches so later saves of this instance skip the lookup.+ schema.source = source+ if source.team_id == schema.team_id:+ schema.team = source.team+ return schema.source, schema.team.organization_id++@mutable_receiver(model_activity_signal, sender=ExternalDataSchema)def handle_external_data_schema_change(sender, scope, before_update, after_update, activity, user, was_impersonated=False, **kwargs@@ -41,7 +53,7 @@ def handle_external_data_schema_change(if not external_data_schema:return- source = external_data_schema.source+ source, organization_id = _source_and_organization_id(external_data_schema)source_type = source.source_type if source else ""sync_frequency = None@@ -57,7 +69,7 @@ def handle_external_data_schema_change()log_activity(- organization_id=external_data_schema.team.organization_id,+ organization_id=organization_id,team_id=external_data_schema.team_id,user=user,was_impersonated=was_impersonated,