Skip to content

: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.

fix(endpoints): link version before scheduling materialization (#61822)

Jovan Sakovic9fef967976cf
diff --git a/products/endpoints/backend/api.py b/products/endpoints/backend/api.py
index 30ce8a3799fa..57ecbeb2e660 100644
--- a/products/endpoints/backend/api.py
+++ b/products/endpoints/backend/api.py
@@ -1191,6 +1191,10 @@ class EndpointViewSet(TeamAndOrgViewSetMixin, PydanticModelMixin, TaggedItemView
saved_query.save()
+ version.saved_query = saved_query
+ version.bucket_overrides = bucket_overrides
+ version.save(update_fields=["saved_query", "bucket_overrides", "updated_at"])
+
saved_query.schedule_materialization()
try:
@@ -1210,9 +1214,6 @@ class EndpointViewSet(TeamAndOrgViewSetMixin, PydanticModelMixin, TaggedItemView
"saved_query_id": version.saved_query.id if version and version.saved_query else None,
},
)
- version.saved_query = saved_query
- version.bucket_overrides = bucket_overrides
- version.save(update_fields=["saved_query", "bucket_overrides", "updated_at"])
def _disable_materialization(
self, endpoint: Endpoint, request: Request, version: EndpointVersion | None = None
diff --git a/products/endpoints/backend/tests/test_endpoint_materialization.py b/products/endpoints/backend/tests/test_endpoint_materialization.py
index c4fa93c1cba3..4613c4742e74 100644
--- a/products/endpoints/backend/tests/test_endpoint_materialization.py
+++ b/products/endpoints/backend/tests/test_endpoint_materialization.py
@@ -26,6 +26,7 @@ from products.data_modeling.backend.models.modeling import DataWarehouseModelPat
from products.data_warehouse.backend.data_load.saved_query_service import get_saved_query_schedule
from products.endpoints.backend.api import EndpointViewSet
from products.endpoints.backend.materialization import build_endpoint_hogql
+from products.endpoints.backend.models import EndpointVersion
from products.endpoints.backend.services.endpoint_materialization_service import (
OrphanedEndpointSavedQueryError,
prepare_executable_query,
@@ -1441,6 +1442,42 @@ class TestEndpointMaterialization(ClickhouseTestMixin, APIBaseTest):
with self.assertRaises(OrphanedEndpointSavedQueryError):
prepare_executable_query(saved_query)
+ def test_enable_materialization_links_version_before_immediate_run(self):
+ endpoint = create_endpoint_with_version(
+ name="hundred",
+ team=self.team,
+ query=self.sample_hogql_query,
+ created_by=self.user,
+ is_active=True,
+ )
+
+ observed: dict = {}
+
+ def simulate_immediate_temporal_run(self_saved_query):
+ # schedule_materialization() triggers an immediate run on a separate worker
+ # process, which sees only committed DB state. Capture whether the version is
+ # already linked, then run the real activity code that throws when it isn't.
+ observed["link_committed"] = EndpointVersion.objects.filter(saved_query_id=self_saved_query.id).exists()
+ prepare_executable_query(self_saved_query)
+
+ with mock.patch.object(
+ DataWarehouseSavedQuery,
+ "schedule_materialization",
+ autospec=True,
+ side_effect=simulate_immediate_temporal_run,
+ ):
+ response = self.client.patch(
+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/",
+ {"is_materialized": True, "data_freshness_seconds": 86400},
+ format="json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
+ self.assertTrue(
+ observed.get("link_committed"),
+ "EndpointVersion must be linked to the saved query before materialization is scheduled",
+ )
+
def test_build_endpoint_hogql_performs_no_db_writes(self):
_create_event(team=self.team, event="$pageview", distinct_id="u1")
flush_persons_and_events()