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-imports): reject unknown per-table sync types when creating a source (#110218)

Tom Owers8135eb3915b3
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_source/source_setup.py b/products/warehouse_sources/backend/presentation/views/external_data_source/source_setup.py
index 4761f2b337ad..b46931e06e66 100644
--- a/products/warehouse_sources/backend/presentation/views/external_data_source/source_setup.py
+++ b/products/warehouse_sources/backend/presentation/views/external_data_source/source_setup.py
@@ -1289,6 +1289,26 @@ class ExternalDataSourceSetupMixin(base.ExternalDataSourceViewSetBase):
data={"message": "Schemas given do not exist in source"},
)
+ # `payload` is a free-form dict, so the serializer never checks per-schema sync types. An
+ # unknown one would be saved as-is and fail every sync of that table.
+ valid_sync_types = ExternalDataSchema.SyncType.values
+ invalid_sync_types = sorted(
+ {
+ str(schema.get("sync_type"))
+ for schema in payload_schemas
+ if schema.get("sync_type") is not None and schema.get("sync_type") not in valid_sync_types
+ }
+ )
+ if invalid_sync_types:
+ new_source_model.delete()
+ return Response(
+ status=status.HTTP_400_BAD_REQUEST,
+ data={
+ "message": f"Unknown sync type: {', '.join(invalid_sync_types)}. "
+ f"Use one of: {', '.join(valid_sync_types)}."
+ },
+ )
+
# Refuse per-schema `sync_type=cdc` when source-level CDC is off — `_setup_cdc_resources`
# would be skipped, leaving the source with no replication slot/publication.
if not cdc_enabled:
diff --git a/products/warehouse_sources/backend/tests/api/test_external_data_source.py b/products/warehouse_sources/backend/tests/api/test_external_data_source.py
index 9d6e525de67e..883836f21378 100644
--- a/products/warehouse_sources/backend/tests/api/test_external_data_source.py
+++ b/products/warehouse_sources/backend/tests/api/test_external_data_source.py
@@ -779,11 +779,17 @@ class TestExternalDataSource(APIBaseTest):
source = ExternalDataSource.objects.get()
assert source.schemas.filter(should_sync=True).exists()
+ @parameterized.expand(
+ [
+ ("unknown_name", {"name": "SomeOtherSchema", "should_sync": True, "sync_type": "full_refresh"}),
+ ("unknown_sync_type", {"name": STRIPE_CUSTOMER_RESOURCE_NAME, "should_sync": True, "sync_type": "full"}),
+ ]
+ )
@patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.StripeSource.validate_credentials",
return_value=(True, None),
)
- def test_create_external_data_source_delete_on_bad_schema(self, _mock_validate):
+ def test_create_external_data_source_delete_on_bad_schema(self, _name, schema, _mock_validate):
response = self.client.post(
f"/api/environments/{self.team.pk}/external_data_sources/",
data={
@@ -791,15 +797,14 @@ class TestExternalDataSource(APIBaseTest):
"created_via": "web",
"payload": {
"auth_method": {"selection": "api_key", "stripe_secret_key": "sk_test_123"},
- "schemas": [
- {"name": "SomeOtherSchema", "should_sync": True, "sync_type": "full_refresh"},
- ],
+ "schemas": [schema],
},
},
)
assert response.status_code == 400
assert ExternalDataSource.objects.count() == 0
+ assert ExternalDataSchema.objects.count() == 0
@parameterized.expand(
[
@@ -9584,7 +9589,6 @@ class TestCreateWebhook(APIBaseTest):
@patch("products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.StripeSource.create_webhook")
def test_update_webhook_inputs_partial_update_preserves_other_required_fields(self, mock_create_webhook):
-
from products.cdp.backend.models.hog_functions.hog_function import HogFunction
mock_create_webhook.return_value = self._webhook_result(extra_inputs={"signing_secret": "whsec_initial"})