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): trim a stray space from destination bucket names on save (#119205)

Tom Owersc9dbca3de0ea
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_destination.py b/products/warehouse_sources/backend/presentation/views/external_data_destination.py
index 966f7a4d9353..085c4bb2cdaf 100644
--- a/products/warehouse_sources/backend/presentation/views/external_data_destination.py
+++ b/products/warehouse_sources/backend/presentation/views/external_data_destination.py
@@ -249,6 +249,9 @@ class ExternalDataDestinationSerializer(serializers.ModelSerializer):
expected = " or ".join(f"'{kind}'" for kind in allowed_kinds)
raise ValidationError({"integration": f"A {destination_type} destination needs a {expected} integration."})
+ if isinstance(attrs.get("config"), dict):
+ attrs["config"] = self._trim_names(str(destination_type), attrs["config"])
+
if self.instance is not None:
self._reject_retargeting(attrs)
elif destination_type == ExternalDataDestination.Type.POSTGRES:
@@ -290,6 +293,20 @@ class ExternalDataDestinationSerializer(serializers.ModelSerializer):
str(ExternalDataDestination.Type.AZURE_BLOB): ("container_name", "container", "prefix"),
}
+ # Config fields whose names cannot hold a space, per type. A pasted bucket or container name
+ # often has a stray space at one end, and the store then rejects the name on every run. A
+ # prefix is not here: a prefix with a space is a valid key, and rows may already sit under it.
+ TRIMMED_FIELDS_BY_TYPE: dict[str, tuple[str, ...]] = {
+ str(ExternalDataDestination.Type.S3): ("bucket", "bucket_name", "region"),
+ str(ExternalDataDestination.Type.AZURE_BLOB): ("container_name", "container"),
+ }
+
+ def _trim_names(self, destination_type: str, config: dict[str, Any]) -> dict[str, Any]:
+ fields = self.TRIMMED_FIELDS_BY_TYPE.get(destination_type, ())
+ return {
+ key: value.strip() if key in fields and isinstance(value, str) else value for key, value in config.items()
+ }
+
def _reject_retargeting(self, attrs: dict[str, Any]) -> None:
assert self.instance is not None
if "integration" in attrs and attrs["integration"] != self.instance.integration:
@@ -305,9 +322,10 @@ class ExternalDataDestinationSerializer(serializers.ModelSerializer):
if "config" not in attrs:
return
- current = self.instance.config or {}
- incoming = attrs["config"] or {}
destination_type = attrs.get("type", getattr(self.instance, "type", None))
+ # A name saved with a stray space never worked, so the trimmed name is the same target.
+ current = self._trim_names(str(destination_type), self.instance.config or {})
+ incoming = attrs["config"] or {}
for field in self.RETARGETING_FIELDS_BY_TYPE.get(str(destination_type), ()):
if field in incoming and incoming[field] != current.get(field):
label = field.replace("_", " ")
diff --git a/products/warehouse_sources/backend/tests/api/test_external_data_destination.py b/products/warehouse_sources/backend/tests/api/test_external_data_destination.py
index 20c2a520419f..3093568d43e5 100644
--- a/products/warehouse_sources/backend/tests/api/test_external_data_destination.py
+++ b/products/warehouse_sources/backend/tests/api/test_external_data_destination.py
@@ -255,6 +255,66 @@ class TestExternalDataDestinationAPI(DestinationAPITestBase):
assert response.status_code == status.HTTP_200_OK, response.json()
+ @parameterized.expand(
+ [
+ ("s3_bucket", ExternalDataDestination.Type.S3, Integration.IntegrationKind.AWS_S3, "bucket", True),
+ (
+ "s3_bucket_alias",
+ ExternalDataDestination.Type.S3,
+ Integration.IntegrationKind.AWS_S3,
+ "bucket_name",
+ True,
+ ),
+ ("s3_region", ExternalDataDestination.Type.S3, Integration.IntegrationKind.AWS_S3, "region", True),
+ ("s3_prefix", ExternalDataDestination.Type.S3, Integration.IntegrationKind.AWS_S3, "prefix", False),
+ (
+ "azure_container",
+ ExternalDataDestination.Type.AZURE_BLOB,
+ Integration.IntegrationKind.AZURE_BLOB,
+ "container_name",
+ True,
+ ),
+ (
+ "azure_container_alias",
+ ExternalDataDestination.Type.AZURE_BLOB,
+ Integration.IntegrationKind.AZURE_BLOB,
+ "container",
+ True,
+ ),
+ (
+ "postgres_schema",
+ ExternalDataDestination.Type.POSTGRES,
+ Integration.IntegrationKind.POSTGRESQL,
+ "schema",
+ False,
+ ),
+ ]
+ )
+ def test_a_name_saved_with_a_stray_space_is_trimmed(
+ self, _name: str, destination_type: str, kind: str, field: str, trimmed: bool
+ ) -> None:
+ destination = self._create_destination(
+ type=destination_type, integration=self._integration(kind).pk, config={field: " example-name "}
+ )
+
+ assert destination.config[field] == ("example-name" if trimmed else " example-name ")
+
+ @parameterized.expand([("resent_as_stored", " example-bucket"), ("resent_trimmed", "example-bucket")])
+ def test_a_bucket_saved_before_trimming_can_still_be_edited(self, _name: str, resent: str) -> None:
+ destination = ExternalDataDestination.objects.for_team(self.team.pk).create(
+ team_id=self.team.pk,
+ type=ExternalDataDestination.Type.S3,
+ name="exports",
+ integration=self._integration(Integration.IntegrationKind.AWS_S3),
+ config={"bucket": " example-bucket", "region": "us-east-1"},
+ )
+
+ response = self.client.patch(f"{self.base}/{destination.id}", {"config": {"bucket": resent}})
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ destination.refresh_from_db()
+ assert destination.config == {"bucket": "example-bucket", "region": "us-east-1"}
+
def test_a_partial_config_update_keeps_the_fields_it_did_not_mention(self) -> None:
# `config` is one JSON blob. A PATCH naming only the key it means to change must not
# replace the whole blob and silently drop the target fields alongside it.