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 schedule update when no schedule exists (#64280)

Marcus Hof1feba8b68a4d
diff --git a/products/data_warehouse/backend/api/external_data_schema.py b/products/data_warehouse/backend/api/external_data_schema.py
index 626cab7ee043..a2dd6294ff89 100644
--- a/products/data_warehouse/backend/api/external_data_schema.py
+++ b/products/data_warehouse/backend/api/external_data_schema.py
@@ -644,10 +644,16 @@ class ExternalDataSchemaSerializer(serializers.ModelSerializer):
pause_external_data_schedule(str(updated_instance.id))
elif should_sync is True:
unpause_external_data_schedule(str(updated_instance.id))
- elif should_sync is True:
+ elif should_sync_value:
+ # No schedule yet but the schema should be syncing — create (or recover) it. The
+ # schedule is built from the current frequency, so a cadence-only edit on an
+ # enabled-but-unscheduled schema still takes effect.
sync_external_data_job_workflow(updated_instance, create=True, should_sync=should_sync_value)
- if was_sync_frequency_updated or was_sync_time_of_day_updated:
+ # Re-issue an existing schedule when the cadence changed. A disabled schema with no
+ # schedule has nothing to update — updating a missing schedule raises "workflow not
+ # found" — so its new cadence is just saved and applies if/when it is enabled.
+ if (was_sync_frequency_updated or was_sync_time_of_day_updated) and schedule_exists:
sync_external_data_job_workflow(updated_instance, create=False, should_sync=should_sync_value)
self._run_temporal_side_effect(update_schedule)
diff --git a/products/data_warehouse/backend/api/test/test_external_data_schema.py b/products/data_warehouse/backend/api/test/test_external_data_schema.py
index 8ff615a0e89f..042df51de827 100644
--- a/products/data_warehouse/backend/api/test/test_external_data_schema.py
+++ b/products/data_warehouse/backend/api/test/test_external_data_schema.py
@@ -580,6 +580,83 @@ class TestExternalDataSchema(APIBaseTest):
assert schema.sync_frequency_interval == timedelta(minutes=1)
assert schema.sync_type == ExternalDataSchema.SyncType.CDC
+ def test_update_schema_frequency_on_disabled_schema_does_not_touch_missing_schedule(self):
+ # A disabled / never-activated schema has no Temporal schedule. Changing its sync frequency
+ # must not try to update a schedule that doesn't exist (which raises "workflow not found");
+ # the new frequency is just saved, to apply if/when the schema is enabled.
+ source = ExternalDataSource.objects.create(
+ team=self.team,
+ source_type=ExternalDataSourceType.POSTGRES,
+ job_inputs={"host": "h", "port": 5432, "database": "d", "user": "u", "password": "p", "schema": "public"},
+ )
+ schema = ExternalDataSchema.objects.create(
+ name="public.orders",
+ team=self.team,
+ source=source,
+ should_sync=False,
+ sync_type=ExternalDataSchema.SyncType.FULL_REFRESH,
+ sync_frequency_interval=timedelta(hours=6),
+ )
+
+ with (
+ mock.patch(
+ "products.data_warehouse.backend.api.external_data_schema.external_data_workflow_exists",
+ return_value=False,
+ ),
+ mock.patch(
+ "products.data_warehouse.backend.api.external_data_schema.sync_external_data_job_workflow"
+ ) as mock_sync_workflow,
+ ):
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}",
+ data={"sync_frequency": "30day"},
+ )
+
+ assert response.status_code == 200, response.content
+ # The frequency is saved...
+ schema.refresh_from_db()
+ assert schema.sync_frequency_interval == timedelta(days=30)
+ # ...but no schedule create/update is attempted, because the schema has no schedule to touch.
+ mock_sync_workflow.assert_not_called()
+
+ def test_update_schema_frequency_on_enabled_schema_without_schedule_creates_it(self):
+ # An enabled schema whose Temporal schedule is missing should have it created when the
+ # cadence is edited (even when should_sync isn't re-sent), not left silently unscheduled.
+ source = ExternalDataSource.objects.create(
+ team=self.team,
+ source_type=ExternalDataSourceType.POSTGRES,
+ job_inputs={"host": "h", "port": 5432, "database": "d", "user": "u", "password": "p", "schema": "public"},
+ )
+ schema = ExternalDataSchema.objects.create(
+ name="public.orders",
+ team=self.team,
+ source=source,
+ should_sync=True,
+ sync_type=ExternalDataSchema.SyncType.FULL_REFRESH,
+ sync_frequency_interval=timedelta(hours=6),
+ )
+
+ with (
+ mock.patch(
+ "products.data_warehouse.backend.api.external_data_schema.external_data_workflow_exists",
+ return_value=False,
+ ),
+ mock.patch(
+ "products.data_warehouse.backend.api.external_data_schema.sync_external_data_job_workflow"
+ ) as mock_sync_workflow,
+ ):
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}",
+ data={"sync_frequency": "30day"},
+ )
+
+ assert response.status_code == 200, response.content
+ schema.refresh_from_db()
+ assert schema.sync_frequency_interval == timedelta(days=30)
+ # The missing schedule is created (recovered) with the new cadence, not left absent.
+ mock_sync_workflow.assert_called_once()
+ assert mock_sync_workflow.call_args.kwargs["create"] is True
+
def test_update_schema_enable_should_sync_rejects_cdc_without_primary_key(self):
# Schemas already in CDC mode with an empty primary_key_columns (created before the
# API gate landed) must not be re-enabled until a PK is added on the source side.
diff --git a/products/data_warehouse/backend/api/test/test_external_data_source.py b/products/data_warehouse/backend/api/test/test_external_data_source.py
index e928549ab309..9416ac0b4b81 100644
--- a/products/data_warehouse/backend/api/test/test_external_data_source.py
+++ b/products/data_warehouse/backend/api/test/test_external_data_source.py
@@ -424,9 +424,10 @@ class TestExternalDataSource(APIBaseTest):
@patch(
"products.data_warehouse.backend.api.external_data_schema.external_data_workflow_exists",
- return_value=False,
+ return_value=True,
)
def test_bulk_update_schemas_runs_deferred_temporal_updates(self, _mock_workflow_exists):
+ # Enabled schema with an existing schedule: a frequency change re-issues (updates) it.
source = self._create_external_data_source()
schema = ExternalDataSchema.objects.create(
name="Customers",