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): keep materializations that other queries read (#118492)

posthog[bot]69e36f29d7c9
diff --git a/products/data_modeling/backend/logic/saved_query_dag_sync.py b/products/data_modeling/backend/logic/saved_query_dag_sync.py
index f498935aeb9e..200248ff10c4 100644
--- a/products/data_modeling/backend/logic/saved_query_dag_sync.py
+++ b/products/data_modeling/backend/logic/saved_query_dag_sync.py
@@ -480,8 +480,14 @@ def _dependent_metrics(nodes: QuerySet[Node]) -> list[Dependent]:
]
-def describe_dependents(view_name: str, dependents: Sequence[Dependent], *, any_hidden: bool = False) -> str:
- """The message a person reads when a delete is refused, naming at most three dependents.
+def describe_dependents(
+ view_name: str,
+ dependents: Sequence[Dependent],
+ *,
+ any_hidden: bool = False,
+ action: Literal["delete", "turn off materialization for"] = "delete",
+) -> str:
+ """The message a person reads when an action is refused, naming at most three dependents.
`dependents` must already be filtered to what the reader may see, and the count in the overflow
tail is taken over that list, so the tail cannot report how many were withheld.
@@ -489,10 +495,12 @@ def describe_dependents(view_name: str, dependents: Sequence[Dependent], *, any_
if not dependents:
if any_hidden:
return (
- f"Can't delete {view_name} yet. Something you don't have access to reads from it. "
+ f"Can't {action} {view_name} yet. Something you don't have access to reads from it. "
"Ask a project admin to find what depends on it."
)
- return _nameless_refusal(view_name)
+ if action == "delete":
+ return _nameless_refusal(view_name)
+ return f"Can't {action} {view_name} yet. Something else reads from it. Update or delete it first."
named = dependents[:MAX_NAMED_DEPENDENTS]
listed = ", ".join(f"{dependent.name} ({DEPENDENT_KIND_LABELS.get(dependent.kind, 'view')})" for dependent in named)
@@ -501,10 +509,10 @@ def describe_dependents(view_name: str, dependents: Sequence[Dependent], *, any_
listed = f"{listed}, and {remaining} more"
if any_hidden:
return (
- f"Can't delete {view_name} yet. These read from it: {listed}. Something you don't have access to "
+ f"Can't {action} {view_name} yet. These read from it: {listed}. Something you don't have access to "
"reads from it too. Update or delete the ones listed, then ask a project admin about the rest."
)
- return f"Can't delete {view_name} yet. These read from it: {listed}. Update or delete them first."
+ return f"Can't {action} {view_name} yet. These read from it: {listed}. Update or delete them first."
def blocked_lineage_node_id(dependents: Sequence[Dependent], fallback_node_id: str | None) -> str | None:
diff --git a/products/endpoints/backend/logic/crud.py b/products/endpoints/backend/logic/crud.py
index a646c9a669cc..d2191d3c2990 100644
--- a/products/endpoints/backend/logic/crud.py
+++ b/products/endpoints/backend/logic/crud.py
@@ -30,6 +30,7 @@ from products.data_modeling.backend.facade.api import HasDependentsError, delete
from products.endpoints.backend.constants import DEFAULT_DATA_FRESHNESS_SECONDS
from products.endpoints.backend.logic.activity import EndpointContext
from products.endpoints.backend.logic.materialization import EndpointMaterializationService
+from products.endpoints.backend.logic.materialization_dependencies import EndpointMaterializationDependencyService
from products.endpoints.backend.logic.validation import validate_bucket_overrides
from products.endpoints.backend.models import Endpoint, EndpointVersion
from products.endpoints.backend.rate_limit import clear_endpoint_materialization_cache
@@ -65,6 +66,7 @@ class EndpointCrudService:
self.request = request
self.user = cast(User, request.user)
self.materialization = EndpointMaterializationService(team, request)
+ self.materialization_dependencies = EndpointMaterializationDependencyService(team.pk)
def _log_activity(self, *, item_id: str, scope: str, activity: str, detail: Detail) -> None:
log_activity(
@@ -200,6 +202,9 @@ class EndpointCrudService:
version_before_update = EndpointVersion.objects.get(pk=target_version.pk)
was_materialized = current_version.saved_query_id is not None
+ # Before any write, so a refused update leaves the endpoint as it was.
+ self._check_disable_breaks_no_reader(endpoint, data, target_version, version_targeted)
+
step = "endpoint_activation"
# Endpoint-level activation only — deactivating a single version must never
# touch the endpoint or the current version's materialization.
@@ -249,9 +254,7 @@ class EndpointCrudService:
materialization_error=materialization_error,
)
- except APIException:
- raise
- except ValidationError:
+ except (APIException, ValidationError, HasDependentsError):
raise
except Exception as e:
current_version = endpoint.get_version()
@@ -282,6 +285,32 @@ class EndpointCrudService:
)
return target_version_override
+ def _check_disable_breaks_no_reader(
+ self,
+ endpoint: Endpoint,
+ data: EndpointRequest,
+ target_version: EndpointVersion,
+ version_targeted: bool,
+ ) -> None:
+ """Raise HasDependentsError if the update would disable a saved query that other queries read.
+
+ Disabling soft-deletes the saved query, so every view or endpoint that selects from it fails.
+ Mirrors the disable decisions in `update` and `_reconcile_materialization`.
+ """
+ versions_to_disable: list[EndpointVersion] = []
+ if version_targeted:
+ if data.is_active is False or data.is_materialized is False:
+ versions_to_disable = [target_version]
+ elif data.is_active is False or (data.is_active is None and not endpoint.is_active):
+ versions_to_disable = list(endpoint.versions.filter(saved_query__isnull=False))
+ elif data.is_materialized is False and (
+ data.query is None or not endpoint.has_query_changed(data.query.model_dump())
+ ):
+ versions_to_disable = [target_version]
+
+ for version in versions_to_disable:
+ self.materialization_dependencies.check_deletable(version)
+
def _apply_query_change(
self,
endpoint: Endpoint,
@@ -492,6 +521,7 @@ class EndpointCrudService:
for version in endpoint.versions.filter(saved_query__isnull=False):
try:
if version.saved_query:
+ self.materialization_dependencies.check_deletable(version)
delete_node_from_dag(version.saved_query)
except HasDependentsError:
raise
diff --git a/products/endpoints/backend/logic/materialization_dependencies.py b/products/endpoints/backend/logic/materialization_dependencies.py
new file mode 100644
index 000000000000..2a47122548a4
--- /dev/null
+++ b/products/endpoints/backend/logic/materialization_dependencies.py
@@ -0,0 +1,104 @@
+import re
+from collections.abc import Iterator
+
+from posthog.schema_enums import DataWarehouseSavedQueryOrigin
+
+from products.data_modeling.backend.facade.api import (
+ HasDependentsError,
+ check_saved_query_deletable,
+ saved_query_definitions,
+)
+from products.data_modeling.backend.facade.contracts import Dependent, SavedQueryDefinition
+from products.endpoints.backend.models import EndpointVersion
+
+
+class EndpointMaterializationDependencyService:
+ def __init__(self, team_id: int) -> None:
+ self.team_id = team_id
+ self._saved_query_readers: list[SavedQueryDefinition] | None = None
+ self._endpoint_readers: list[EndpointVersion] | None = None
+
+ def check_deletable(self, version: EndpointVersion) -> None:
+ saved_query = version.saved_query
+ if saved_query is None:
+ return
+
+ dag_dependents: tuple[Dependent, ...] = ()
+ fallback_node_id: str | None = None
+ try:
+ check_saved_query_deletable(self.team_id, saved_query.id)
+ except HasDependentsError as error:
+ dag_dependents = error.dependents
+ fallback_node_id = error.fallback_node_id
+
+ dependents = self._deduplicate(
+ [*dag_dependents, *self._fallback_dependents(version, saved_query.name, str(saved_query.id))]
+ )
+ if dependents:
+ raise HasDependentsError(saved_query.name, dependents, fallback_node_id=fallback_node_id)
+
+ def _fallback_dependents(
+ self, source_version: EndpointVersion, saved_query_name: str, saved_query_id: str
+ ) -> Iterator[Dependent]:
+ name_pattern = re.compile(rf"(?<![\w$]){re.escape(saved_query_name)}(?![\w$])")
+
+ for definition in self._get_saved_query_readers():
+ if (
+ str(definition.id) != saved_query_id
+ and definition.origin != DataWarehouseSavedQueryOrigin.ENDPOINT
+ and name_pattern.search(definition.hogql)
+ ):
+ yield Dependent(
+ name=definition.name,
+ kind="matview" if definition.materializes else "view",
+ saved_query_id=str(definition.id),
+ )
+
+ for reader in self._get_endpoint_readers():
+ if reader.id != source_version.id and self._query_references(reader.query, saved_query_name, name_pattern):
+ yield Dependent(
+ name=reader.materialized_view_name if reader.saved_query_id else reader.endpoint.name,
+ kind="endpoint",
+ saved_query_id=str(reader.saved_query_id) if reader.saved_query_id else None,
+ created_by_id=reader.endpoint.created_by_id,
+ )
+
+ def _get_saved_query_readers(self) -> list[SavedQueryDefinition]:
+ if self._saved_query_readers is None:
+ self._saved_query_readers = saved_query_definitions(self.team_id)
+ return self._saved_query_readers
+
+ def _get_endpoint_readers(self) -> list[EndpointVersion]:
+ if self._endpoint_readers is None:
+ self._endpoint_readers = list(
+ EndpointVersion.objects.filter(
+ endpoint__team_id=self.team_id,
+ endpoint__deleted=False,
+ endpoint__is_active=True,
+ is_active=True,
+ )
+ .select_related("endpoint")
+ .order_by("created_at", "id")
+ )
+ return self._endpoint_readers
+
+ @classmethod
+ def _query_references(cls, value: object, table_name: str, name_pattern: re.Pattern[str]) -> bool:
+ if isinstance(value, dict):
+ if value.get("kind") == "HogQLQuery" and isinstance(value.get("query"), str):
+ if name_pattern.search(value["query"]):
+ return True
+ if str(value.get("kind", "")).endswith("DataWarehouseNode") and value.get("table_name") == table_name:
+ return True
+ return any(cls._query_references(child, table_name, name_pattern) for child in value.values())
+ if isinstance(value, list):
+ return any(cls._query_references(child, table_name, name_pattern) for child in value)
+ return False
+
+ @staticmethod
+ def _deduplicate(dependents: list[Dependent]) -> tuple[Dependent, ...]:
+ unique: dict[tuple[str, str], Dependent] = {}
+ for dependent in dependents:
+ key = (dependent.kind, dependent.saved_query_id or dependent.name)
+ unique.setdefault(key, dependent)
+ return tuple(unique.values())
diff --git a/products/endpoints/backend/presentation/views/api.py b/products/endpoints/backend/presentation/views/api.py
index 861c2cc9d0e5..ccfa1730cbbc 100644
--- a/products/endpoints/backend/presentation/views/api.py
+++ b/products/endpoints/backend/presentation/views/api.py
@@ -11,8 +11,10 @@ and response serialization. Business logic lives in ``backend/logic``:
"""
import re
+import builtins
import dataclasses
-from typing import cast
+from typing import Literal, cast
+from uuid import UUID
from django.db.models import Count, F, Prefetch
from django.shortcuts import get_object_or_404
@@ -58,7 +60,13 @@ from posthog.schema_migrations.upgrade import upgrade
from products.access_control.backend.facade.user_access_control import access_level_satisfied_for_resource
from products.access_control.backend.presentation.access_control import AccessControlViewSetMixin
-from products.data_modeling.backend.facade.api import HasDependentsError
+from products.data_modeling.backend.facade.api import (
+ HasDependentsError,
+ allowed_saved_query_ids,
+ blocked_lineage_node_id,
+ describe_dependents,
+)
+from products.data_modeling.backend.facade.contracts import Dependent
from products.data_modeling.backend.facade.models import DataModelingJob
from products.endpoints.backend.facade.api import (
REWRITE_CONTRACT,
@@ -552,7 +560,10 @@ class EndpointViewSet(
data, self.team, cast(User, request.user), endpoint=endpoint, version_number=version_number
)
- outcome = EndpointCrudService(self.team, request).update(endpoint, data, request.data, version_number)
+ try:
+ outcome = EndpointCrudService(self.team, request).update(endpoint, data, request.data, version_number)
+ except HasDependentsError as error:
+ raise self._dependents_error(endpoint.name, error, action="turn off materialization for")
# When targeting a specific version, return version data; otherwise return endpoint data
if outcome.version_targeted and outcome.target_version is not None:
@@ -571,13 +582,94 @@ class EndpointViewSet(
try:
EndpointCrudService(self.team, request).destroy(endpoint)
except HasDependentsError as error:
- can_read_lineage = self.user_access_control.check_access_level_for_resource(
- "warehouse_view", required_level="viewer"
+ raise self._dependents_error(error.view_name, error)
+ return Response(status=status.HTTP_204_NO_CONTENT)
+
+ def _dependents_error(
+ self,
+ subject_name: str,
+ error: HasDependentsError,
+ *,
+ action: Literal["delete", "turn off materialization for"] = "delete",
+ ) -> EndpointDependentsValidationError:
+ visible = self._visible_dependents(error.dependents)
+ can_read_lineage = self.user_access_control.check_access_level_for_resource(
+ "warehouse_view", required_level="viewer"
+ )
+ return EndpointDependentsValidationError(
+ describe_dependents(
+ subject_name,
+ visible,
+ any_hidden=len(visible) < len(error.dependents),
+ action=action,
+ ),
+ node_id=blocked_lineage_node_id(visible, error.fallback_node_id) if can_read_lineage else None,
+ )
+
+ def _visible_dependents(self, dependents: tuple[Dependent, ...]) -> builtins.list[Dependent]:
+ endpoint_dependents = [dependent for dependent in dependents if dependent.kind == "endpoint"]
+ endpoint_saved_query_ids = [
+ dependent.saved_query_id for dependent in endpoint_dependents if dependent.saved_query_id is not None
+ ]
+ endpoint_names = [dependent.name for dependent in endpoint_dependents if dependent.saved_query_id is None]
+ endpoint_versions = EndpointVersion.objects.filter(
+ endpoint__team=self.team,
+ saved_query_id__in=endpoint_saved_query_ids,
+ ).select_related("endpoint")
+ endpoints = {
+ endpoint.id: endpoint
+ for endpoint in [
+ *(version.endpoint for version in endpoint_versions),
+ *Endpoint.objects.filter(team=self.team, name__in=endpoint_names, deleted=False),
+ ]
+ }
+ self.user_access_control.preload_object_access_controls(list(endpoints.values()))
+ readable_endpoint_ids = {
+ str(endpoint.id)
+ for endpoint in endpoints.values()
+ if self.user_access_control.check_access_level_for_object(endpoint, "viewer")
+ }
+ endpoint_ids_by_saved_query = {
+ str(version.saved_query_id): str(version.endpoint_id) for version in endpoint_versions
+ }
+ endpoint_ids_by_name = {endpoint.name: str(endpoint.id) for endpoint in endpoints.values()}
+
+ saved_query_ids = [
+ UUID(dependent.saved_query_id)
+ for dependent in dependents
+ if dependent.kind not in {"endpoint", "metric"} and dependent.saved_query_id is not None
+ ]
+ readable_saved_query_ids = {
+ str(saved_query_id)
+ for saved_query_id in allowed_saved_query_ids(
+ self.team_id,
+ self.user_access_control,
+ ids=saved_query_ids,
)
- raise EndpointDependentsValidationError(
- str(error), node_id=error.fallback_node_id if can_read_lineage else None
+ }
+ metrics_readable = self.user_access_control.check_access_level_for_resource(
+ "data_catalog", required_level="viewer"
+ )
+
+ return [
+ dependent
+ for dependent in dependents
+ if (
+ (
+ dependent.kind == "endpoint"
+ and (
+ endpoint_ids_by_saved_query.get(dependent.saved_query_id or "")
+ or endpoint_ids_by_name.get(dependent.name)
+ )
+ in readable_endpoint_ids
+ )
+ or (dependent.kind == "metric" and metrics_readable)
+ or (
+ dependent.kind not in {"endpoint", "metric"}
+ and dependent.saved_query_id in readable_saved_query_ids
+ )
)
- return Response(status=status.HTTP_204_NO_CONTENT)
+ ]
# ------------------------------------------------------------------
# Execution
diff --git a/products/endpoints/backend/tasks/tasks.py b/products/endpoints/backend/tasks/tasks.py
index 1b7bd8c0edd0..d108c62e1286 100644
--- a/products/endpoints/backend/tasks/tasks.py
+++ b/products/endpoints/backend/tasks/tasks.py
@@ -10,8 +10,10 @@ from structlog import get_logger
from posthog.celery_queues import CeleryQueue
from posthog.scoping_audit import skip_team_scope_audit
+from products.data_modeling.backend.facade.api import HasDependentsError
from products.data_modeling.backend.facade.models import DataModelingJob, DataModelingJobStatus
from products.endpoints.backend.logic.ducklake_shadow import run_ducklake_shadow_comparison
+from products.endpoints.backend.logic.materialization_dependencies import EndpointMaterializationDependencyService
from products.endpoints.backend.metrics import ENDPOINT_MATERIALIZATION_EVENT_TOTAL
from products.endpoints.backend.models import EndpointVersion
@@ -64,6 +66,7 @@ def deactivate_stale_materializations() -> None:
3. The materialization was enabled at least 30 days ago (saved_query.created_at)
4. The version was last executed over 30 days ago (via API key), or it is a superseded
version that was never executed
+ 5. No other view, endpoint or metric reads the saved query
For matching versions, the materialization is reverted to save resources.
"""
@@ -103,18 +106,42 @@ def deactivate_stale_materializations() -> None:
return
deactivated_count = 0
+ skipped_count = 0
+ dependency_services: dict[int, EndpointMaterializationDependencyService] = {}
for version in stale_versions:
+ team_id = version.endpoint.team_id
+ log_context = {
+ "endpoint_id": str(version.endpoint.id),
+ "endpoint_name": version.endpoint.name,
+ "version": version.version,
+ "team_id": team_id,
+ "saved_query_name": version.saved_query.name if version.saved_query else None,
+ }
+ dependency_service = dependency_services.setdefault(team_id, EndpointMaterializationDependencyService(team_id))
+ try:
+ dependency_service.check_deletable(version)
+ except HasDependentsError as error:
+ logger.info(
+ "deactivate_stale_materialization_skipped_dependents",
+ dependents=len(error.dependents),
+ **log_context,
+ )
+ skipped_count += 1
+ ENDPOINT_MATERIALIZATION_EVENT_TOTAL.labels(action="deactivate_stale", status="skipped_dependents").inc()
+ continue
+ except Exception:
+ logger.exception("deactivate_stale_materialization_dependents_check_failed", **log_context)
+ skipped_count += 1
+ ENDPOINT_MATERIALIZATION_EVENT_TOTAL.labels(action="deactivate_stale", status="skipped_dependents").inc()
+ continue
try:
_deactivate_version_materialization(version)
except Exception as e:
logger.exception(
"deactivate_stale_materialization_failed",
- endpoint_id=str(version.endpoint.id),
- endpoint_name=version.endpoint.name,
- version=version.version,
- team_id=version.endpoint.team_id,
error=str(e),
+ **log_context,
)
ENDPOINT_MATERIALIZATION_EVENT_TOTAL.labels(action="deactivate_stale", status="error").inc()
continue
@@ -124,6 +151,7 @@ def deactivate_stale_materializations() -> None:
logger.info(
"deactivate_stale_materializations_completed",
deactivated_count=deactivated_count,
+ skipped_count=skipped_count,
)
diff --git a/products/endpoints/backend/tests/test_deactivate_stale_materializations.py b/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
index b5d084c50464..1e1591046644 100644
--- a/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
+++ b/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
@@ -1,4 +1,4 @@
-from datetime import timedelta
+from datetime import datetime, timedelta
import pytest
from posthog.test.base import BaseTest
@@ -8,7 +8,16 @@ from django.utils import timezone
from parameterized import parameterized
-from products.data_modeling.backend.facade.models import DataModelingJob, DataWarehouseSavedQuery
+from posthog.models.team import Team
+
+from products.data_modeling.backend.facade.models import (
+ DAG,
+ DataModelingJob,
+ DataWarehouseSavedQuery,
+ Edge,
+ Node,
+ NodeType,
+)
from products.endpoints.backend.models import Endpoint, EndpointVersion
from products.endpoints.backend.tasks.tasks import (
STALE_THRESHOLD_DAYS,
@@ -31,13 +40,15 @@ class TestDeactivateStaleMaterializationsTask(BaseTest):
def _create_materialized_endpoint(
self,
name: str,
- last_run_at=None,
- last_executed_at=None,
- materialization_created_at=None,
+ last_run_at: datetime | None = None,
+ last_executed_at: datetime | None = None,
+ materialization_created_at: datetime | None = None,
+ team: Team | None = None,
) -> tuple[Endpoint, EndpointVersion]:
"""Create an endpoint with a materialized version."""
+ team = team or self.team
saved_query = DataWarehouseSavedQuery.objects.create(
- team=self.team,
+ team=team,
name=name,
query=self.sample_hogql_query,
is_materialized=True,
@@ -52,13 +63,13 @@ class TestDeactivateStaleMaterializationsTask(BaseTest):
endpoint = Endpoint.objects.create(
name=name,
- team=self.team,
+ team=team,
created_by=self.user,
is_active=True,
last_executed_at=last_executed_at,
)
table = DataWarehouseTable.objects.create(
- team=self.team,
+ team=team,
name=f"{name}_table",
format=DataWarehouseTable.TableFormat.Parquet,
url_pattern=f"s3://test-bucket/{name}_table",
@@ -299,6 +310,73 @@ class TestDeactivateStaleMaterializationsTask(BaseTest):
assert deactivated_ids == {old_version.id}
assert current_version.id not in deactivated_ids
+ @parameterized.expand(
+ [("lineage_edge",), ("view_without_lineage",), ("endpoint_hogql_query",), ("endpoint_structured_query",)]
+ )
+ def test_keeps_stale_version_that_another_query_reads(self, reader: str):
+ now = timezone.now()
+ _, version = self._create_materialized_endpoint(
+ "upstream_endpoint_v1",
+ last_run_at=now - timedelta(hours=1),
+ last_executed_at=now - timedelta(days=45),
+ materialization_created_at=now - timedelta(days=45),
+ )
+ reader_query = (
+ {
+ "kind": "TrendsQuery",
+ "series": [{"kind": "DataWarehouseNode", "table_name": "upstream_endpoint_v1"}],
+ }
+ if reader == "endpoint_structured_query"
+ else {"kind": "HogQLQuery", "query": "SELECT count() FROM upstream_endpoint_v1"}
+ )
+ if reader.startswith("endpoint_"):
+ reader_endpoint = Endpoint.objects.create(name="reader", team=self.team, created_by=self.user)
+ EndpointVersion.objects.create(
+ endpoint=reader_endpoint, version=1, query=reader_query, created_by=self.user
+ )
+ else:
+ dag = DAG.get_or_create_default(self.team)
+ source = Node.objects.create(
+ team=self.team, dag=dag, saved_query=version.saved_query, type=NodeType.ENDPOINT
+ )
+ downstream = DataWarehouseSavedQuery.objects.create(
+ team=self.team, name="downstream_view", query=reader_query
+ )
+ target = Node.objects.create(team=self.team, dag=dag, saved_query=downstream, type=NodeType.VIEW)
+ if reader != "view_without_lineage":
+ Edge.objects.create(team=self.team, dag=dag, source=source, target=target)
+
+ deactivate_stale_materializations()
+
+ version.refresh_from_db()
+ assert version.saved_query is not None
+ assert version.saved_query.deleted is False
+
+ def test_dependency_check_failure_only_skips_candidates_in_that_team(self):
+ now = timezone.now()
+ _, guarded_version = self._create_materialized_endpoint(
+ "guarded_endpoint",
+ last_run_at=now - timedelta(hours=1),
+ last_executed_at=now - timedelta(days=45),
+ materialization_created_at=now - timedelta(days=45),
+ )
+ DataWarehouseSavedQuery.objects.create(team=self.team, name="malformed_reader", query=[{"broken": True}])
+ other_team = Team.objects.create(organization=self.organization, name="other team")
+ _, deactivated_version = self._create_materialized_endpoint(
+ "other_team_endpoint",
+ last_run_at=now - timedelta(hours=1),
+ last_executed_at=now - timedelta(days=45),
+ materialization_created_at=now - timedelta(days=45),
+ team=other_team,
+ )
+
+ deactivate_stale_materializations()
+
+ guarded_version.refresh_from_db()
+ deactivated_version.refresh_from_db()
+ assert guarded_version.saved_query is not None
+ assert deactivated_version.saved_query is None
+
class TestDeactivateEndpointMaterialization(BaseTest):
def setUp(self):
diff --git a/products/endpoints/backend/tests/test_endpoint.py b/products/endpoints/backend/tests/test_endpoint.py
index 628f2e15dd5b..a27e0963a29a 100644
--- a/products/endpoints/backend/tests/test_endpoint.py
+++ b/products/endpoints/backend/tests/test_endpoint.py
@@ -440,7 +440,48 @@ class TestEndpoint(ClickhouseTestMixin, APIBaseTest):
assert log.detail is not None
self.assertEqual(log.detail.get("name"), "delete_test")
- def test_delete_endpoint_is_refused_when_a_model_depends_on_it(self):
+ @parameterized.expand(
+ [
+ ("delete", "delete", None, "dag_view", "Can't delete blocked_endpoint_v1 yet.", "downstream_view", True),
+ (
+ "disable_materialization",
+ "patch",
+ {"is_materialized": False},
+ "dag_view",
+ "Can't turn off materialization for blocked_endpoint yet.",
+ "downstream_view",
+ True,
+ ),
+ (
+ "deactivate_endpoint",
+ "patch",
+ {"is_active": False},
+ "dag_view",
+ "Can't turn off materialization for blocked_endpoint yet.",
+ "downstream_view",
+ True,
+ ),
+ (
+ "disable_materialization_for_untracked_endpoint_reader",
+ "patch",
+ {"is_materialized": False},
+ "untracked_endpoint",
+ "Can't turn off materialization for blocked_endpoint yet.",
+ "reader",
+ False,
+ ),
+ ]
+ )
+ def test_removing_endpoint_materialization_is_refused_when_a_model_depends_on_it(
+ self,
+ _name: str,
+ method: str,
+ body: dict[str, Any] | None,
+ reader: str,
+ refusal: str,
+ dependent_name: str,
+ has_lineage_link: bool,
+ ):
endpoint = create_endpoint_with_version(
name="blocked_endpoint",
team=self.team,
@@ -454,11 +495,6 @@ class TestEndpoint(ClickhouseTestMixin, APIBaseTest):
query=self.sample_hogql_query,
)
version.enable_materialization(backing_query)
- downstream_query = DataWarehouseSavedQuery.objects.create(
- team=self.team,
- name="downstream_view",
- query={"kind": "HogQLQuery", "query": "SELECT * FROM blocked_endpoint_v1"},
- )
dag = DAG.get_or_create_default(self.team)
endpoint_node = Node.objects.create(
team=self.team,
@@ -466,27 +502,47 @@ class TestEndpoint(ClickhouseTestMixin, APIBaseTest):
saved_query=backing_query,
type=NodeType.ENDPOINT,
)
- downstream_node = Node.objects.create(
- team=self.team,
- dag=dag,
- saved_query=downstream_query,
- type=NodeType.VIEW,
- )
- Edge.objects.create(team=self.team, dag=dag, source=endpoint_node, target=downstream_node)
+ if reader == "dag_view":
+ downstream_query = DataWarehouseSavedQuery.objects.create(
+ team=self.team,
+ name="downstream_view",
+ query={"kind": "HogQLQuery", "query": "SELECT * FROM blocked_endpoint_v1"},
+ )
+ downstream_node = Node.objects.create(
+ team=self.team,
+ dag=dag,
+ saved_query=downstream_query,
+ type=NodeType.VIEW,
+ )
+ Edge.objects.create(team=self.team, dag=dag, source=endpoint_node, target=downstream_node)
+ else:
+ reader_endpoint = Endpoint.objects.create(name="reader", team=self.team, created_by=self.user)
+ EndpointVersion.objects.create(
+ endpoint=reader_endpoint,
+ version=1,
+ query={"kind": "HogQLQuery", "query": "SELECT * FROM blocked_endpoint_v1"},
+ created_by=self.user,
+ )
- response = self.client.delete(f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/")
+ response = getattr(self.client, method)(
+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/", body, format="json"
+ )
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.json()["code"], "has_dependents")
- self.assertEqual(response.json()["extra"], {"node_id": str(endpoint_node.id)})
+ if has_lineage_link:
+ self.assertEqual(response.json()["extra"], {"node_id": str(endpoint_node.id)})
+ else:
+ self.assertNotIn("extra", response.json())
self.assertEqual(
response.json()["detail"],
- "Can't delete blocked_endpoint_v1 yet. Something else reads from it. Update or delete it first.",
+ f"{refusal} These read from it: {dependent_name} ({'endpoint' if reader == 'untracked_endpoint' else 'view'}). Update or delete them first.",
)
endpoint.refresh_from_db()
backing_query.refresh_from_db()
version.refresh_from_db()
self.assertFalse(endpoint.deleted)
+ self.assertTrue(endpoint.is_active)
self.assertFalse(backing_query.deleted)
self.assertEqual(version.saved_query_id, backing_query.id)
self.assertTrue(Node.objects.filter(id=endpoint_node.id).exists())