: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): match incremental_fields schema by name (#61115)
Marcus Hof4c4f2236822e
diff --git a/products/data_warehouse/backend/api/external_data_schema.py b/products/data_warehouse/backend/api/external_data_schema.pyindex 9ed44b02dd0d..9cb8a883d303 100644--- a/products/data_warehouse/backend/api/external_data_schema.py+++ b/products/data_warehouse/backend/api/external_data_schema.py@@ -884,13 +884,15 @@ class ExternalDataSchemaViewset(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):data={"message": str(e)},)- if not schemas:+ # Not every source honors the `names` filter (e.g. Slack returns all schemas regardless), so+ # `schemas` may contain unrelated tables in any order. Pick the one that matches this schema+ # instead of trusting `schemas[0]`, whose metadata could belong to a different table.+ schema = next((s for s in schemas if s.name == instance.name), None)+ if schema is None:return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": f"Schema with name {instance.name} not found"})- schema = schemas[0]-source_cdc_enabled = bool(source.job_inputs.get("cdc_enabled"))cdc_available = schema.supports_cdc if is_cdc_enabled_for_team(self.team) and source_cdc_enabled else Nonediff --git a/products/data_warehouse/backend/api/test/test_external_data_schema.py b/products/data_warehouse/backend/api/test/test_external_data_schema.pyindex a57599253258..a798d7ce3fe5 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@@ -285,6 +285,64 @@ class TestExternalDataSchema(APIBaseTest):assert response.status_code == status.HTTP_200_OKassert response.json()["cdc_available"] is expected_cdc_available+ def test_incremental_fields_matches_schema_by_name(self):+ source = ExternalDataSource.objects.create(+ team=self.team, source_type=ExternalDataSourceType.STRIPE, job_inputs={"stripe_secret_key": "test_key"}+ )+ schema = ExternalDataSchema.objects.create(+ name="C123",+ team=self.team,+ source=source,+ should_sync=True,+ status=ExternalDataSchema.Status.COMPLETED,+ sync_type=ExternalDataSchema.SyncType.WEBHOOK,+ )++ # Mimics sources (e.g. Slack) that ignore `names` and return every schema. The first+ # element is an unrelated table; the endpoint must pick the one matching instance.name.+ all_schemas = [+ SourceSchema(name="$channels", supports_incremental=False, supports_append=False, supports_webhooks=False),+ SourceSchema(name="C123", supports_incremental=False, supports_append=False, supports_webhooks=True),+ ]++ with (+ mock.patch.object(StripeSource, "validate_credentials", return_value=(True, None)),+ mock.patch.object(StripeSource, "get_schemas", return_value=all_schemas),+ ):+ response = self.client.post(+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}/incremental_fields",+ )++ assert response.status_code == status.HTTP_200_OK+ assert response.json()["supports_webhooks"] is True++ def test_incremental_fields_returns_400_when_schema_name_absent(self):+ source = ExternalDataSource.objects.create(+ team=self.team, source_type=ExternalDataSourceType.STRIPE, job_inputs={"stripe_secret_key": "test_key"}+ )+ schema = ExternalDataSchema.objects.create(+ name="C999",+ team=self.team,+ source=source,+ should_sync=True,+ status=ExternalDataSchema.Status.COMPLETED,+ sync_type=ExternalDataSchema.SyncType.WEBHOOK,+ )++ other_schemas = [+ SourceSchema(name="$channels", supports_incremental=False, supports_append=False, supports_webhooks=False),+ ]++ with (+ mock.patch.object(StripeSource, "validate_credentials", return_value=(True, None)),+ mock.patch.object(StripeSource, "get_schemas", return_value=other_schemas),+ ):+ response = self.client.post(+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}/incremental_fields",+ )++ assert response.status_code == status.HTTP_400_BAD_REQUEST+def test_update_schema_change_sync_type(self):source = ExternalDataSource.objects.create(team=self.team,