: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(warehouse-sources): explain empty schema discovery (#96905)
jake sciotto4e02d4638bae
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_schema.py b/products/warehouse_sources/backend/presentation/views/external_data_schema.pyindex ec029a9152d2..04c20d384374 100644--- a/products/warehouse_sources/backend/presentation/views/external_data_schema.py+++ b/products/warehouse_sources/backend/presentation/views/external_data_schema.py@@ -1933,6 +1933,16 @@ class ExternalDataSchemaViewset(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):data={"message": str(e)},)+ if not schemas:+ return Response(+ data={+ "message": f"Could not discover schema {instance.name}. The connection may be missing SELECT or "+ "schema access privileges, or discovery may not support this relation type. Check that the "+ "relation exists, restore read privileges, or expose it as a supported table or view, then try again."+ },+ status=status.HTTP_400_BAD_REQUEST,+ )+# 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.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.pyindex cae87fb7e434..21169d330925 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@@ -43,6 +43,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.basWebhookSyncResult,)from products.warehouse_sources.backend.temporal.data_imports.sources.common.schema import SourceSchema+from products.warehouse_sources.backend.temporal.data_imports.sources.redshift.source import RedshiftSourcefrom products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source import StripeSourcefrom products.warehouse_sources.backend.tests.api.utils import create_external_data_source_ok@@ -487,9 +488,46 @@ class TestExternalDataSchema(APIBaseTest):assert response.status_code == status.HTTP_200_OKassert response.json()["supports_webhooks"] is True- def test_incremental_fields_returns_400_when_schema_name_absent(self):+ @parameterized.expand(+ [+ (+ "empty_discovery",+ RedshiftSource,+ [],+ "Could not discover schema C999. The connection may be missing SELECT or schema access privileges, "+ "or discovery may not support this relation type. Check that the relation exists, restore read "+ "privileges, or expose it as a supported table or view, then try again.",+ ),+ (+ "nonempty_discovery",+ RedshiftSource,+ [SourceSchema(name="$channels", supports_incremental=False, supports_append=False)],+ "Schema with name C999 not found",+ ),+ (+ "nonempty_api_discovery",+ StripeSource,+ [SourceSchema(name="$channels", supports_incremental=False, supports_append=False)],+ "Schema with name C999 not found",+ ),+ ]+ )+ def test_incremental_fields_returns_400_when_schema_name_absent(+ self, _name, source_class, discovered_schemas, expected_message+ ):source = ExternalDataSource.objects.create(- team=self.team, source_type=ExternalDataSourceType.STRIPE, job_inputs={"stripe_secret_key": "test_key"}+ team=self.team,+ source_type=source_class().source_type,+ job_inputs={+ "host": "localhost",+ "port": 5439,+ "database": "dev",+ "user": "test",+ "password": "test",+ "schema": "public",+ }+ if source_class is RedshiftSource+ else {"stripe_secret_key": "test_key"},)schema = ExternalDataSchema.objects.create(name="C999",@@ -500,19 +538,16 @@ class TestExternalDataSchema(APIBaseTest):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),+ mock.patch.object(source_class, "validate_credentials", return_value=(True, None)),+ mock.patch.object(source_class, "get_schemas", return_value=discovered_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+ assert response.json() == {"message": expected_message}def test_update_schema_change_sync_type(self):source = ExternalDataSource.objects.create(