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(api): keep the events schema switch when modifiers are cleared (#116672)

Paul D'Ambraca1daff7a504
diff --git a/posthog/api/project.py b/posthog/api/project.py
index 0975c245aed8..2fa13d80407f 100644
--- a/posthog/api/project.py
+++ b/posthog/api/project.py
@@ -54,6 +54,7 @@ from posthog.api.team import (
heatmaps_screenshot_secret_for_reader,
live_events_token_for_request,
merge_conversations_settings_locked,
+ merge_modifiers,
report_conversations_settings_changes,
strip_managed_conversations_settings,
team_event_ingestion_restrictions_view,
@@ -1369,11 +1370,8 @@ class ProjectBackwardCompatSerializer(
}
# Merge modifiers with existing values so that updating one modifier doesn't wipe out others
- if "modifiers" in validated_data and validated_data["modifiers"] is not None:
- validated_data["modifiers"] = {
- **(team.modifiers or {}),
- **validated_data["modifiers"],
- }
+ if "modifiers" in validated_data:
+ validated_data["modifiers"] = merge_modifiers(validated_data["modifiers"], team.modifiers)
# Persist only the fields this request changes. A full-row save() writes back every
# column from this request's snapshot of the team, so two concurrent PATCHes clobber
diff --git a/posthog/api/team.py b/posthog/api/team.py
index 1353b0e56458..47edee323990 100644
--- a/posthog/api/team.py
+++ b/posthog/api/team.py
@@ -1468,6 +1468,17 @@ class ConversationsSettingsField(serializers.JSONField):
pass
+def merge_modifiers(requested: dict | None, existing: object) -> dict | None:
+ # Django admin can store a scalar or list here. Treat it as empty rather than raising.
+ stored = existing if isinstance(existing, dict) else {}
+ if requested is not None:
+ return {**stored, **requested}
+ # PostHog staff set useNewEventsSchema per project, so a client that clears its modifiers keeps it.
+ if "useNewEventsSchema" in stored:
+ return {"useNewEventsSchema": stored["useNewEventsSchema"]}
+ return None
+
+
def live_events_token_for_request(team: Team, request: request.Request | None) -> str | None:
if request is None or not request.user.is_authenticated:
return None
@@ -2456,11 +2467,8 @@ class TeamSerializer(serializers.ModelSerializer, UserPermissionsSerializerMixin
}
# Merge modifiers with existing values so that updating one modifier doesn't wipe out others
- if "modifiers" in validated_data and validated_data["modifiers"] is not None:
- validated_data["modifiers"] = {
- **(instance.modifiers or {}),
- **validated_data["modifiers"],
- }
+ if "modifiers" in validated_data:
+ validated_data["modifiers"] = merge_modifiers(validated_data["modifiers"], instance.modifiers)
# Persist only the fields this request changes. A full-row save() writes back every
# column from this request's snapshot of the team, so two concurrent PATCHes clobber
diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.py
index 81e09a6b1b56..7777a39969a9 100644
--- a/posthog/api/test/test_team.py
+++ b/posthog/api/test/test_team.py
@@ -1424,6 +1424,37 @@ def team_api_test_factory():
assert response.status_code == status.HTTP_200_OK, response.json()
assert response.json()["modifiers"] == {"useNewEventsSchema": True, "bounceRateDurationSeconds": 30}
+ @parameterized.expand(
+ [
+ (
+ "environments",
+ {"useNewEventsSchema": True, "bounceRateDurationSeconds": 30},
+ None,
+ {"useNewEventsSchema": True},
+ ),
+ (
+ "projects",
+ {"useNewEventsSchema": True, "bounceRateDurationSeconds": 30},
+ None,
+ {"useNewEventsSchema": True},
+ ),
+ ("environments", True, None, None),
+ ("projects", ["useNewEventsSchema"], None, None),
+ ("environments", True, {"bounceRateDurationSeconds": 30}, {"bounceRateDurationSeconds": 30}),
+ ]
+ )
+ def test_patching_modifiers_over_a_stored_value(
+ self, prefix: str, stored: object, patched: dict | None, expected: dict | None
+ ) -> None:
+ self.team.modifiers = stored
+ self.team.save()
+
+ response = self.client.patch(f"/api/{prefix}/{self.team.id}", {"modifiers": patched}, format="json")
+ assert response.status_code == status.HTTP_200_OK, response.json()
+
+ self.team.refresh_from_db()
+ assert self.team.modifiers == expected
+
@parameterized.expand(
[
(1, True),