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(batch-exports): keep the integration on postgres exports (#129959)

Ross28308e3b8224
diff --git a/products/batch_exports/backend/logic/exports.py b/products/batch_exports/backend/logic/exports.py
index 9e5ad784ff06..31d05f42e379 100644
--- a/products/batch_exports/backend/logic/exports.py
+++ b/products/batch_exports/backend/logic/exports.py
@@ -388,6 +388,15 @@ def validate_destination(
raise contracts.InvalidBatchExportDestinationError(str(e))
if destination_type == BatchExportDestinationType.POSTGRES:
+ # Sticky integration: an explicit `integration: null` would make the export fall back to
+ # any inline credentials still left in its config, and nothing records that switch.
+ # TODO: remove this guard once inline credentials are gone.
+ if instance is not None and instance.destination.integration is not None and kept_integration is None:
+ raise contracts.InvalidBatchExportDestinationError(
+ "Cannot remove the integration from a Postgres batch export that uses one. "
+ "Omit `integration` to keep it, or send a different one to swap."
+ )
+
# New Postgres exports must use an Integration for credentials. Exports created before
# integrations existed keep their inline credentials, so only require it on create
# (`instance is None`); existing inline-credential exports stay valid when edited.
diff --git a/products/batch_exports/backend/tests/api/test_update_postgres.py b/products/batch_exports/backend/tests/api/test_update_postgres.py
new file mode 100644
index 000000000000..c9fc44cb1a21
--- /dev/null
+++ b/products/batch_exports/backend/tests/api/test_update_postgres.py
@@ -0,0 +1,55 @@
+import pytest
+
+from django.test.client import Client as HttpClient
+
+from rest_framework import status
+
+from products.batch_exports.backend.tests.api.operations import create_batch_export, patch_batch_export
+from products.batch_exports.backend.tests.api.test_create_postgres import create_postgresql_integration
+
+pytestmark = [
+ pytest.mark.django_db,
+ pytest.mark.usefixtures("temporal_worker", "cleanup"),
+]
+
+
+ "destination_patch, expected_status",
+ [
+ ({"integration": None}, status.HTTP_400_BAD_REQUEST),
+ ({}, status.HTTP_200_OK),
+ ],
+ ids=["explicit_null_is_rejected", "omitted_keeps_integration"],
+)
+def test_updating_postgres_batch_export_keeps_its_integration(
+ client: HttpClient, temporal, organization, team, user, destination_patch, expected_status
+):
+ integration = create_postgresql_integration(team, user, host="8.8.8.8")
+ client.force_login(user)
+ response = create_batch_export(
+ client,
+ team.pk,
+ {
+ "name": "my-postgres-destination",
+ "destination": {
+ "type": "Postgres",
+ "config": {"database": "my-db", "schema": "public", "table_name": "my_events"},
+ "integration": integration.id,
+ },
+ "interval": "hour",
+ },
+ )
+ assert response.status_code == status.HTTP_201_CREATED, response.json()
+
+ response = patch_batch_export(
+ client,
+ team.pk,
+ response.json()["id"],
+ {"destination": {"type": "Postgres", "config": {"table_name": "my_new_events"}, **destination_patch}},
+ )
+
+ assert response.status_code == expected_status, response.json()
+ if expected_status == status.HTTP_200_OK:
+ assert response.json()["destination"]["integration"] == integration.id
+ else:
+ assert response.json()["detail"].startswith("Cannot remove the integration from a Postgres batch export")