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(warehouse-sources): recover a table's sync when its schedule is missing (#98423)

Tom Owerse67a5450f3e4
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_schema.py b/products/warehouse_sources/backend/presentation/views/external_data_schema.py
index 04c20d384374..17dcb238c756 100644
--- a/products/warehouse_sources/backend/presentation/views/external_data_schema.py
+++ b/products/warehouse_sources/backend/presentation/views/external_data_schema.py
@@ -186,6 +186,21 @@ def _reset_cdc_for_full_resnapshot(instance: ExternalDataSchema) -> None:
instance.save(update_fields=["status", "updated_at"])
+def _trigger_schema_sync(instance: ExternalDataSchema) -> None:
+ """Trigger the schema's sync, creating its Temporal schedule first if it has none.
+
+ A schema can reach the UI with no schedule behind it (never created, or dropped), and
+ triggering one that isn't there raises NOT_FOUND. Retrying can't fix that, so recover the
+ same way the source-level reload does instead of dead-ending a single table's sync.
+ """
+ try:
+ trigger_external_data_workflow(instance)
+ except temporalio.service.RPCError as e:
+ if e.status != temporalio.service.RPCStatusCode.NOT_FOUND:
+ raise
+ sync_external_data_job_workflow(instance, create=True, should_sync=instance.should_sync)
+
+
# Sync frequencies below the 5-minute floor. No longer accepted as input (dropped from the
# serializer's choices), but rows written before the floor may still carry one until the
# migrate_sub_5min_sync_frequencies command bumps them — so the interval mappings keep parsing
@@ -1663,13 +1678,13 @@ class ExternalDataSchemaViewset(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
)
try:
- trigger_external_data_workflow(instance)
+ _trigger_schema_sync(instance)
except temporalio.service.RPCError as e:
# Only mark the schema Running once the trigger succeeded: a Running status with no
# workflow behind it sticks forever (nothing finalizes it) and blocks cancel.
logger.exception(f"Could not trigger external data job for schema {instance.id}", exc_info=e)
return Response(
- data={"detail": "Couldn't start the sync. Please try again."},
+ data={"detail": "Couldn't start the sync. Try again in a few minutes."},
status=status.HTTP_400_BAD_REQUEST,
)
except Exception as e:
@@ -1735,14 +1750,14 @@ class ExternalDataSchemaViewset(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
instance.initial_sync_complete = False
try:
- trigger_external_data_workflow(instance)
+ _trigger_schema_sync(instance)
except temporalio.service.RPCError as e:
# Only mark the schema Running once the trigger succeeded: a Running status with no
# workflow behind it sticks forever (nothing finalizes it) and blocks cancel. The
# sync_type_config reset above stays; the schema's intent is still "resync next run".
logger.exception(f"Could not trigger external data job for schema {instance.id}", exc_info=e)
return Response(
- data={"detail": "Couldn't start the sync. Please try again."},
+ data={"detail": "Couldn't start the sync. Try again in a few minutes."},
status=status.HTTP_400_BAD_REQUEST,
)
diff --git a/products/warehouse_sources/backend/tests/api/test_external_data_schema.py b/products/warehouse_sources/backend/tests/api/test_external_data_schema.py
index 21169d330925..0af231eb3a06 100644
--- a/products/warehouse_sources/backend/tests/api/test_external_data_schema.py
+++ b/products/warehouse_sources/backend/tests/api/test_external_data_schema.py
@@ -3220,6 +3220,31 @@ class TestTriggerFailureDoesNotPaintRunning(APIBaseTest):
schema.refresh_from_db()
assert schema.status == ExternalDataSchema.Status.FAILED
+ @parameterized.expand([("reload",), ("resync",)])
+ @mock.patch(
+ "products.warehouse_sources.backend.presentation.views.external_data_schema.sync_external_data_job_workflow"
+ )
+ @mock.patch(
+ "products.warehouse_sources.backend.presentation.views.external_data_schema.trigger_external_data_workflow"
+ )
+ def test_missing_schedule_is_created_so_the_sync_starts(self, endpoint, mock_trigger, mock_create_schedule):
+ # A schema with no schedule behind it can't be triggered, and retrying never fixes it. The
+ # source-level reload already recovers by creating the schedule; one table must too.
+ from temporalio.service import RPCError
+
+ schema = self._create_schema()
+ mock_trigger.side_effect = RPCError("schedule not found", RPCStatusCode.NOT_FOUND, b"")
+
+ response = self.client.post(
+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}/{endpoint}/",
+ )
+
+ assert response.status_code == 200
+ mock_create_schedule.assert_called_once_with(schema, create=True, should_sync=True)
+
+ schema.refresh_from_db()
+ assert schema.status == ExternalDataSchema.Status.RUNNING
+
class TestExternalDataSchemaAPIKeyScopes(APIBaseTest):
def _make_api_key(self, scopes: list[str]) -> str: