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(data-warehouse): skip enrichment for a deleted schema (#121347)

Tom Owersf0d604b67adc
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/enrich_table_semantics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/enrich_table_semantics.py
index 5709b1a202ec..b67b731a366c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/enrich_table_semantics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/enrich_table_semantics.py
@@ -297,13 +297,21 @@ def enrich_table_semantics_sync(team_id: int, schema_id: uuid.UUID) -> dict[str,
# Retried individually, not via `with_internal_db_retries` on the whole function, because the
# LLM call further down is expensive and must not be repeated for a blip reaching our own database.
- team = retry_internal_db_operation(
- lambda: (
- Team.objects.select_related("organization")
- .only("id", "uuid", "organization_id", "organization__is_ai_data_processing_approved")
- .get(id=team_id)
+ # The team can also be legitimately gone by the time this fire-and-forget child workflow runs
+ # (deleted between the post-import gate check and now) — not a bug, so skip rather than let
+ # DoesNotExist reach the activity's except block and error tracking. No analytics event here:
+ # emit_completed needs the team this lookup just failed to find.
+ try:
+ team = retry_internal_db_operation(
+ lambda: (
+ Team.objects.select_related("organization")
+ .only("id", "uuid", "organization_id", "organization__is_ai_data_processing_approved")
+ .get(id=team_id)
+ )
)
- )
+ except Team.DoesNotExist:
+ log.info("warehouse_enrichment.skipped", reason="team_deleted")
+ return {"status": "skipped", "reason": "team_deleted"}
# Context accumulated as we learn it; shared by the "started" and "completed" analytics events.
event_props: dict[str, Any] = {"schema_id": str(schema_id)}
@@ -318,13 +326,22 @@ def enrich_table_semantics_sync(team_id: int, schema_id: uuid.UUID) -> dict[str,
emit_completed("skipped", reason="ai_data_processing_not_approved")
return {"status": "skipped", "reason": "ai_data_processing_not_approved"}
- schema = retry_internal_db_operation(
- lambda: (
- ExternalDataSchema.objects.select_related("source", "table")
- .filter(team_id=team_id, deleted=False)
- .get(id=schema_id)
+ # The schema can be legitimately gone by the time this fire-and-forget child workflow runs
+ # (deleted between the post-import gate check and now) — not a bug, so skip like the other
+ # not-found cases below rather than let DoesNotExist reach the activity's except block and
+ # error tracking.
+ try:
+ schema = retry_internal_db_operation(
+ lambda: (
+ ExternalDataSchema.objects.select_related("source", "table")
+ .filter(team_id=team_id, deleted=False)
+ .get(id=schema_id)
+ )
)
- )
+ except ExternalDataSchema.DoesNotExist:
+ log.info("warehouse_enrichment.skipped", reason="schema_deleted")
+ emit_completed("skipped", reason="schema_deleted")
+ return {"status": "skipped", "reason": "schema_deleted"}
table = schema.table
event_props["source_type"] = schema.source.source_type
event_props["schema_name"] = schema.name
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_enrich_table_semantics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_enrich_table_semantics.py
index 65eb238c3f94..bd6c31de08b4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_enrich_table_semantics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_enrich_table_semantics.py
@@ -528,6 +528,28 @@ class TestEnrichTableSemanticsSync:
assert result["reason"] == "ai_data_processing_not_approved"
assert _annotations(team, table) == {}
+ def test_skipped_when_team_was_deleted(self):
+ # Same fire-and-forget race as the schema case below, but for the team itself.
+ team = _team()
+ team_id = team.pk
+ team.delete()
+ with patch.object(enrich, "_generate_descriptions") as mock_llm:
+ result = enrich_table_semantics_sync(team_id, uuid.uuid4())
+ mock_llm.assert_not_called()
+ assert result == {"status": "skipped", "reason": "team_deleted"}
+
+ def test_skipped_when_schema_was_deleted(self):
+ # This activity is a fire-and-forget child workflow started after a sync completes, so the
+ # schema can be deleted by the user before it actually runs. That's a benign race, not a bug.
+ team = _team()
+ schema, table = _make_schema(team, columns=[{"name": "amount", "data_type": "Int64", "is_nullable": False}])
+ schema.soft_delete()
+ with patch.object(enrich, "_generate_descriptions") as mock_llm:
+ result = enrich_table_semantics_sync(team.pk, schema.id)
+ mock_llm.assert_not_called()
+ assert result == {"status": "skipped", "reason": "schema_deleted"}
+ assert _annotations(team, table) == {}
+
def test_skipped_when_table_has_no_columns(self):
team = _team()
schema, table = _make_schema(team, columns=[])