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): persist incremental_field on bare schema PATCH (#64933)

Daniel RCb184dd8e5dd6
diff --git a/products/data_warehouse/backend/api/external_data_schema.py b/products/data_warehouse/backend/api/external_data_schema.py
index a6ad6d082190..166cb7987fc1 100644
--- a/products/data_warehouse/backend/api/external_data_schema.py
+++ b/products/data_warehouse/backend/api/external_data_schema.py
@@ -567,20 +567,53 @@ class ExternalDataSchemaSerializer(serializers.ModelSerializer):
if "sync_type" in data:
validated_data["sync_type"] = sync_type
- trigger_refresh = False
- # Update the validated_data with incremental fields
- if sync_type in (
+ # The sync type the schema will end up with: the new one if the request changes it, else the
+ # existing one. Incremental-style config (incremental_field, incremental_field_type,
+ # primary_key_columns, lookback) is keyed off this so a PATCH that edits only those fields —
+ # without re-sending sync_type — still persists. Previously the whole block below was gated on
+ # `sync_type` being in the request, so a bare `{"incremental_field": ...}` PATCH was silently
+ # dropped while the 200 response still echoed the submitted value from the in-memory config.
+ resulting_sync_type = sync_type if "sync_type" in data else instance.sync_type
+ incremental_style_types = (
ExternalDataSchema.SyncType.INCREMENTAL,
ExternalDataSchema.SyncType.APPEND,
ExternalDataSchema.SyncType.WEBHOOK,
- ):
+ )
+
+ # A bare edit (no sync_type in the request) of sync-config fields on a schema whose sync type
+ # can't apply them would otherwise be an unpersisted no-op that still returns 200 — only the
+ # incremental-style branch below writes these, and a non-incremental schema falls through it.
+ # Reject it so the caller gets a clear error instead of a response that looks applied but isn't.
+ # (primary_key_columns is included: CDC/xmin do use it, but only when sync_type is sent, so a
+ # bare PK edit on those is dropped just like on full_refresh.)
+ if "sync_type" not in data and resulting_sync_type not in incremental_style_types:
+ unappliable_fields = [
+ field
+ for field in (
+ "incremental_field",
+ "incremental_field_type",
+ "incremental_field_lookback_seconds",
+ "primary_key_columns",
+ )
+ if field in data and data.get(field) is not None
+ ]
+ if unappliable_fields:
+ raise ValidationError(
+ f"{', '.join(unappliable_fields)} cannot be applied to a schema with sync type "
+ f"{resulting_sync_type or 'not set'} on its own. "
+ "Include sync_type in the same request to change the sync type."
+ )
+
+ trigger_refresh = False
+ # Update the validated_data with incremental fields
+ if resulting_sync_type in incremental_style_types:
payload = instance.sync_type_config
if "primary_key_columns" in data:
new_pk = data.get("primary_key_columns")
old_pk = instance.sync_type_config.get("primary_key_columns")
if (
- sync_type == ExternalDataSchema.SyncType.INCREMENTAL
+ resulting_sync_type == ExternalDataSchema.SyncType.INCREMENTAL
and new_pk != old_pk
and instance.table is not None
):
@@ -593,7 +626,7 @@ class ExternalDataSchemaSerializer(serializers.ModelSerializer):
# Detect incremental field changes before mutating payload
incremental_field_changed = False
incremental_field = data.get("incremental_field")
- if sync_type in (ExternalDataSchema.SyncType.INCREMENTAL, ExternalDataSchema.SyncType.APPEND):
+ if resulting_sync_type in (ExternalDataSchema.SyncType.INCREMENTAL, ExternalDataSchema.SyncType.APPEND):
if "incremental_field" in data:
incremental_field_changed = (
payload.get("incremental_field") != incremental_field
@@ -607,7 +640,10 @@ class ExternalDataSchemaSerializer(serializers.ModelSerializer):
# Lookback only applies to incremental — merge-by-PK makes re-reading the overlap window
# idempotent. `null` clears it.
- if sync_type == ExternalDataSchema.SyncType.INCREMENTAL and "incremental_field_lookback_seconds" in data:
+ if (
+ resulting_sync_type == ExternalDataSchema.SyncType.INCREMENTAL
+ and "incremental_field_lookback_seconds" in data
+ ):
payload["incremental_field_lookback_seconds"] = data.get("incremental_field_lookback_seconds")
if incremental_field_changed:
@@ -665,7 +701,6 @@ class ExternalDataSchemaSerializer(serializers.ModelSerializer):
# frequency the schema will actually end up with — the new value if one is supplied, else
# the existing interval — against the sync type it will end up with. This also catches
# switching a 1-minute CDC schema to a non-CDC type without re-sending the frequency.
- resulting_sync_type = sync_type if "sync_type" in data else instance.sync_type
resulting_frequency = sync_frequency
if not resulting_frequency and instance.sync_frequency_interval is not None:
resulting_frequency = sync_frequency_interval_to_sync_frequency(instance.sync_frequency_interval)
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 b937b921472e..dbbdcd93dd5a 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
@@ -497,6 +497,84 @@ class TestExternalDataSchema(APIBaseTest):
schema.refresh_from_db()
assert schema.sync_type_config["incremental_field_lookback_seconds"] is None
+ def test_update_incremental_field_without_sync_type_persists(self):
+ source = ExternalDataSource.objects.create(
+ team=self.team,
+ source_type=ExternalDataSourceType.STRIPE,
+ job_inputs={"auth_method": {"selection": "api_key", "stripe_secret_key": "123"}},
+ )
+ schema = ExternalDataSchema.objects.create(
+ name="BalanceTransaction",
+ team=self.team,
+ source=source,
+ should_sync=True,
+ status=ExternalDataSchema.Status.COMPLETED,
+ sync_type=ExternalDataSchema.SyncType.INCREMENTAL,
+ sync_type_config={
+ "incremental_field": "created_at",
+ "incremental_field_type": "timestamp",
+ "incremental_field_last_value": "2026-06-14T15:33:31.802833",
+ },
+ )
+
+ with (
+ mock.patch("products.data_warehouse.backend.api.external_data_schema.trigger_external_data_workflow"),
+ mock.patch(
+ "products.data_warehouse.backend.api.external_data_schema.external_data_workflow_exists",
+ return_value=False,
+ ),
+ ):
+ # A bare incremental_field edit — no sync_type re-sent — must actually persist, not just
+ # echo the submitted value back in the response.
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}",
+ data={"incremental_field": "updated_at"},
+ )
+
+ assert response.status_code == 200, response.json()
+ assert response.json()["incremental_field"] == "updated_at"
+ schema.refresh_from_db()
+ assert schema.sync_type_config["incremental_field"] == "updated_at"
+
+ def test_update_incremental_field_on_non_incremental_schema_errors(self):
+ source = ExternalDataSource.objects.create(
+ team=self.team,
+ source_type=ExternalDataSourceType.STRIPE,
+ job_inputs={"auth_method": {"selection": "api_key", "stripe_secret_key": "123"}},
+ )
+ schema = ExternalDataSchema.objects.create(
+ name="BalanceTransaction",
+ team=self.team,
+ source=source,
+ should_sync=True,
+ status=ExternalDataSchema.Status.COMPLETED,
+ sync_type=ExternalDataSchema.SyncType.FULL_REFRESH,
+ # Seed leftover config so the post-request assertion catches a regression that writes the
+ # value rather than leaving it untouched (a missing key would pass trivially).
+ sync_type_config={"incremental_field": "old_value", "primary_key_columns": ["id"]},
+ )
+
+ # Setting incremental_field on a full_refresh schema without switching sync_type can't be
+ # applied — it must fail loudly instead of returning 200 and dropping the change.
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}",
+ data={"incremental_field": "updated_at"},
+ )
+
+ assert response.status_code == 400, response.json()
+ schema.refresh_from_db()
+ assert schema.sync_type_config.get("incremental_field") == "old_value"
+
+ # primary_key_columns is dropped the same way on a non-incremental schema, so it errors too.
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}",
+ data={"primary_key_columns": ["other_id"]},
+ )
+
+ assert response.status_code == 400, response.json()
+ schema.refresh_from_db()
+ assert schema.sync_type_config.get("primary_key_columns") == ["id"]
+
def test_incremental_field_lookback_seconds_survives_reset(self):
source = ExternalDataSource.objects.create(
team=self.team,