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(warehouse-sources): set primary keys in bulk schema updates (#82273)

jake sciottoec92f6a25e2e
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 e20c85c23853..f1ba93119036 100644
--- a/products/warehouse_sources/backend/presentation/views/external_data_source.py
+++ b/products/warehouse_sources/backend/presentation/views/external_data_source.py
@@ -700,6 +700,12 @@ class ExternalDataSourceBulkUpdateSchemaSerializer(serializers.Serializer):
allow_null=True,
help_text="UTC anchor time for scheduled syncs.",
)
+ primary_key_columns = serializers.ListField(
+ child=serializers.CharField(),
+ required=False,
+ allow_null=True,
+ help_text="Column names for primary key deduplication.",
+ )
cdc_table_mode = serializers.ChoiceField(
required=False,
allow_null=True,
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 90cefe6a8647..702f630ab011 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
@@ -858,6 +858,40 @@ class TestExternalDataSource(APIBaseTest):
assert schema_one.should_sync is False
assert schema_two.should_sync is False
+ @patch(
+ "products.warehouse_sources.backend.presentation.views.external_data_schema.external_data_workflow_exists",
+ return_value=False,
+ )
+ def test_bulk_update_schemas_sets_primary_key_columns(self, _mock_workflow_exists):
+ # A keyless incremental table fails its sync until someone picks a primary key, and a source
+ # can land hundreds of them at once. Undeclared fields are dropped in validation, so without
+ # primary_key_columns on this serializer the batch returns 200 having changed nothing.
+ source = self._create_external_data_source()
+ schemas = [
+ ExternalDataSchema.objects.create(
+ name=name,
+ team_id=self.team.pk,
+ source=source,
+ should_sync=True,
+ sync_type=ExternalDataSchema.SyncType.INCREMENTAL,
+ sync_type_config={"incremental_field": "updated_at", "incremental_field_type": "datetime"},
+ )
+ for name in ("Customers", "Invoices")
+ ]
+
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_sources/{source.id}/bulk_update_schemas",
+ data={"schemas": [{"id": str(schema.id), "primary_key_columns": ["id"]} for schema in schemas]},
+ format="json",
+ )
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ assert [schema["primary_key_columns"] for schema in response.json()] == [["id"], ["id"]]
+
+ for schema in schemas:
+ schema.refresh_from_db()
+ assert schema.sync_type_config.get("primary_key_columns") == ["id"]
+
@patch(
"products.warehouse_sources.backend.presentation.views.external_data_schema.external_data_workflow_exists",
return_value=True,
diff --git a/products/warehouse_sources/frontend/generated/api.schemas.ts b/products/warehouse_sources/frontend/generated/api.schemas.ts
index 3247ec95c6fc..498c49705a55 100644
--- a/products/warehouse_sources/frontend/generated/api.schemas.ts
+++ b/products/warehouse_sources/frontend/generated/api.schemas.ts
@@ -4629,6 +4629,11 @@ export interface ExternalDataSourceBulkUpdateSchemaApi {
* @nullable
*/
sync_time_of_day?: string | null
+ /**
+ * Column names for primary key deduplication.
+ * @nullable
+ */
+ primary_key_columns?: string[] | null
/** How CDC-backed tables should be exposed.
*
* * `consolidated` - consolidated
diff --git a/products/warehouse_sources/frontend/generated/api.zod.ts b/products/warehouse_sources/frontend/generated/api.zod.ts
index a1dd37627686..4e78788c6bcb 100644
--- a/products/warehouse_sources/frontend/generated/api.zod.ts
+++ b/products/warehouse_sources/frontend/generated/api.zod.ts
@@ -564,6 +564,10 @@ export const ExternalDataSourcesBulkUpdateSchemasPartialUpdateBody = /* @__PURE_
incremental_field_type: zod.string().nullish().describe('Type of the incremental cursor field.'),
sync_frequency: zod.string().nullish().describe('Human-readable sync frequency value.'),
sync_time_of_day: zod.iso.time({}).nullish().describe('UTC anchor time for scheduled syncs.'),
+ primary_key_columns: zod
+ .array(zod.string())
+ .nullish()
+ .describe('Column names for primary key deduplication.'),
cdc_table_mode: zod
.union([
zod
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 26bcd83429f9..a35f4078836b 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -33287,6 +33287,11 @@ export namespace Schemas {
* @nullable
*/
sync_time_of_day?: string | null;
+ /**
+ * Column names for primary key deduplication.
+ * @nullable
+ */
+ primary_key_columns?: string[] | null;
/** How CDC-backed tables should be exposed.
*
* * `consolidated` - consolidated