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): stop an unreachable source blocking an unrelated settings save (#93499)

Tom Owers9240faf9459b
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_source.py b/products/warehouse_sources/backend/presentation/views/external_data_source.py
index 2ba8e4a3144c..abc813912416 100644
--- a/products/warehouse_sources/backend/presentation/views/external_data_source.py
+++ b/products/warehouse_sources/backend/presentation/views/external_data_source.py
@@ -1463,12 +1463,27 @@ class ExternalDataSourceSerializers(UserAccessControlSerializerMixin, serializer
source_config: Config = source.parse_config(new_job_inputs)
validated_job_inputs = source_config.to_dict()
+
+ # The settings form resubmits the whole connection config on every save, so changing an
+ # unrelated setting (auto-syncing new tables, the prefix, the description) re-probed the
+ # live connection too — and a momentarily unreachable database then failed the whole save,
+ # leaving nothing to do but retry. Compare the parsed config against what's stored so the
+ # probe below only runs when the connection actually changed. Direct query sources still
+ # probe on every save: the same call refreshes their schemas and connection metadata.
+ try:
+ stored_job_inputs = source.parse_config(existing_job_inputs).to_dict()
+ except Exception:
+ # A stored config that no longer parses can't be compared, so treat it as changed and
+ # let the probe run rather than skipping validation on a config we can't read.
+ stored_job_inputs = None
+ connection_config_changed = stored_job_inputs is None or stored_job_inputs != validated_job_inputs
+
for key in _CDC_EXPOSED_JOB_INPUT_KEYS:
if key in existing_job_inputs:
validated_job_inputs[key] = existing_job_inputs[key]
validated_data["job_inputs"] = validated_job_inputs
- if job_inputs_were_submitted:
+ if job_inputs_were_submitted and (connection_config_changed or instance.is_direct_query):
effective_api_version = source.resolve_api_version(instance.api_version)
try:
if isinstance(source, (PostgresSource, MySQLSource)):
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 bbe3b05a0371..9eac4a85d9c0 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
@@ -2784,6 +2784,71 @@ class TestExternalDataSource(APIBaseTest):
source.refresh_from_db()
assert source.job_inputs["auth_method"]["stripe_secret_key"] == "sk_test_123"
+ @parameterized.expand(
+ [
+ # The settings form resubmits the connection config even when someone only flips an
+ # unrelated setting, so an unreachable source must not fail that save.
+ ("unchanged_config", {}, 200, False),
+ # A submitted credential change still has to be probed before it's stored.
+ (
+ "changed_credential",
+ {"auth_method": {"selection": "api_key", "stripe_secret_key": "sk_test_456"}},
+ 400,
+ True,
+ ),
+ ]
+ )
+ @patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.StripeSource.validate_credentials",
+ return_value=(False, "Could not reach your source."),
+ )
+ def test_patch_external_data_source_probes_connection_only_when_config_changed(
+ self, _name, job_input_overrides, expected_status, expect_probe, mock_validate
+ ):
+ source = self._create_external_data_source()
+ get_data = self.client.get(f"/api/environments/{self.team.pk}/external_data_sources/{source.pk}").json()
+
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_sources/{source.pk}/",
+ data={
+ "job_inputs": {**get_data["job_inputs"], **job_input_overrides},
+ "auto_sync_new_schemas": True,
+ },
+ )
+
+ assert response.status_code == expected_status, response.json()
+ assert mock_validate.called is expect_probe
+ source.refresh_from_db()
+ assert source.auto_sync_new_schemas is (expected_status == 200)
+
+ @patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.StripeSource.validate_credentials",
+ return_value=(False, "Could not reach your source."),
+ )
+ def test_patch_external_data_source_probes_connection_when_stored_config_cannot_be_parsed(self, mock_validate):
+ # A stored secret that still carries the Fernet marker (e.g. decrypted with a key that's since
+ # been rotated out) makes the stored config unparseable. The submitted config is otherwise the
+ # same shape the source was created with, so a naive comparison could call this "unchanged" —
+ # but with nothing valid to compare against, the probe must still run rather than being skipped.
+ source = self._create_external_data_source()
+ source.job_inputs = {"auth_method": {"selection": "api_key", "stripe_secret_key": "gAAAAA_undecryptable"}}
+ source.save()
+
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_sources/{source.pk}/",
+ data={
+ "job_inputs": {"auth_method": {"selection": "api_key", "stripe_secret_key": "sk_test_123"}},
+ "auto_sync_new_schemas": True,
+ },
+ )
+
+ assert response.status_code == 400, response.json()
+ assert mock_validate.called is True
+ source.refresh_from_db()
+ # The rejected probe means the save didn't go through — the corrupted secret is still stored,
+ # not silently replaced by an unvalidated one.
+ assert source.job_inputs["auth_method"]["stripe_secret_key"] == "gAAAAA_undecryptable"
+
@patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.snowflake.source.SnowflakeSource.validate_credentials",
return_value=(True, None),
@@ -6055,7 +6120,8 @@ class TestExternalDataSource(APIBaseTest):
source.refresh_from_db()
assert source.job_inputs["password"] == "db_password" # Main DB password preserved
assert source.job_inputs["ssh_tunnel"]["auth"]["password"] == "ssh_secret_password" # SSH password preserved
- mock_validate_credentials.assert_called_once()
+ # Saving without changes leaves the connection untouched, so it isn't probed
+ mock_validate_credentials.assert_not_called()
@patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.postgres.source.PostgresSource.validate_credentials",
@@ -7075,7 +7141,8 @@ class TestExternalDataSource(APIBaseTest):
assert response.status_code == 200, response.json()
source.refresh_from_db()
assert source.job_inputs["api_key"] == "existing_token"
- mock_validate_credentials.assert_called_once()
+ # The domain and the preserved token match what's stored, so the connection isn't probed
+ mock_validate_credentials.assert_not_called()
@patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.postgres.source.PostgresSource.validate_credentials",