: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(endpoints): disable materialization for all versions when endpoint is deactivated (#65787)
Jovan Sakovic6eff786b4173
diff --git a/products/endpoints/backend/services/crud.py b/products/endpoints/backend/services/crud.pyindex 450a3d28b45a..b21e82a6e97b 100644--- a/products/endpoints/backend/services/crud.py+++ b/products/endpoints/backend/services/crud.py@@ -199,8 +199,12 @@ class EndpointCrudService:if data.is_active is not None and not version_targeted:endpoint.is_active = data.is_activeendpoint.save()- if not version_targeted and not endpoint.is_active and was_materialized:- self.materialization.disable_materialization(endpoint, current_version)+ if not version_targeted and not endpoint.is_active:+ # A deactivated endpoint serves no version, so none should keep a+ # materialization schedule running — tear down every materialized+ # version, not just the current one.+ for materialized_version in endpoint.versions.filter(saved_query__isnull=False):+ self.materialization.disable_materialization(endpoint, materialized_version)if data.is_active is not None and not version_targeted:# Activation affects throttle classification — force a lazy re-check.clear_endpoint_materialization_cache(self.team.pk, endpoint.name)@@ -336,16 +340,17 @@ class EndpointCrudService:Returns a materialization error message when enabling failed after a newversion was already committed (the update itself still succeeds)."""- if not endpoint.is_active:- # Endpoint-level deactivation already tore down the current version's materialization.- return None-if version_targeted and not target_version.is_active:# Deactivating a version: tear down its own materialization, never enable.+ # Checked before the endpoint-active guard so this holds even on an inactive endpoint.if target_version.saved_query_id is not None:self.materialization.disable_materialization(endpoint, target_version)return None+ if not endpoint.is_active:+ # Endpoint-level deactivation already tore down every version's materialization.+ return None+# When targeting a specific version, check that version's materialization state.# Otherwise use the pre-update state so materialization transfers across a version bump.check_was_materialized = target_version.saved_query_id is not None if version_targeted else was_materializeddiff --git a/products/endpoints/backend/tests/test_endpoint_materialization.py b/products/endpoints/backend/tests/test_endpoint_materialization.pyindex 169877bf7cbd..62fa0f5978c3 100644--- a/products/endpoints/backend/tests/test_endpoint_materialization.py+++ b/products/endpoints/backend/tests/test_endpoint_materialization.py@@ -1709,6 +1709,85 @@ class TestEndpointMaterialization(ClickhouseTestMixin, APIBaseTest):assert v2.saved_query is not Noneself.assertFalse(v2.saved_query.deleted)+ def test_deactivating_materialized_version_disables_its_materialization(self):+ """PATCH ?version=N {is_active: false} on a materialized version tears down its own schedule."""+ endpoint = create_endpoint_with_version(+ name="version-deactivate-materialized",+ team=self.team,+ query=self.sample_hogql_query,+ created_by=self.user,+ is_active=True,+ )++ response = self.client.patch(+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/",+ {"is_materialized": True},+ format="json",+ )+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())+ v1 = endpoint.get_version(1)+ self.assertIsNotNone(v1.saved_query_id)+ saved_query_id = v1.saved_query_id++ response = self.client.patch(+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/",+ {"is_active": False, "version": 1},+ format="json",+ )+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())++ v1.refresh_from_db()+ self.assertFalse(v1.is_active)+ self.assertIsNone(v1.saved_query_id, "deactivated version kept its materialization")+ self.assertTrue(DataWarehouseSavedQuery.objects.get(id=saved_query_id).deleted)++ def test_deactivating_endpoint_disables_all_versions_materialization(self):+ """Deactivating the endpoint must tear down every materialized version, not just the current one."""+ endpoint = create_endpoint_with_version(+ name="endpoint-deactivate-all",+ team=self.team,+ query=self.sample_hogql_query,+ created_by=self.user,+ is_active=True,+ )++ # Materialize v1 (current), then bump to v2 via a query change and materialize it too —+ # leaving both versions materialized at once.+ self.client.patch(+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/",+ {"is_materialized": True},+ format="json",+ )+ self.client.patch(+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/",+ {"query": {"kind": "HogQLQuery", "query": "SELECT event FROM events LIMIT 5"}},+ format="json",+ )+ v1 = endpoint.get_version(1)+ v2 = endpoint.get_version(2)+ self.assertIsNotNone(v1.saved_query_id)+ self.assertIsNotNone(v2.saved_query_id)+ v1_saved_query_id = v1.saved_query_id+ v2_saved_query_id = v2.saved_query_id++ response = self.client.patch(+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/",+ {"is_active": False},+ format="json",+ )+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())++ v1.refresh_from_db()+ v2.refresh_from_db()+ self.assertIsNone(v1.saved_query_id, "non-current version kept its materialization")+ self.assertIsNone(v2.saved_query_id, "current version kept its materialization")+ self.assertTrue(DataWarehouseSavedQuery.objects.get(id=v1_saved_query_id).deleted)+ self.assertTrue(DataWarehouseSavedQuery.objects.get(id=v2_saved_query_id).deleted)+ self.assertIsNone(v1.saved_query_id, "non-current version kept its materialization")+ self.assertIsNone(v2.saved_query_id, "current version kept its materialization")+ self.assertTrue(DataWarehouseSavedQuery.objects.get(id=v1_saved_query_id).deleted)+ self.assertTrue(DataWarehouseSavedQuery.objects.get(id=v2_saved_query_id).deleted)+def test_enable_materialization_does_not_hijack_user_saved_query(self):"""A user-created saved query whose name collides with {endpoint}_v{n} must not be taken over."""endpoint = create_endpoint_with_version(