: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(experiments): allow detaching a shared metric with a colliding uuid (#106852)
Anders14f2c4715e30
diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.pyindex 5c86c075c2c9..90bb231924c6 100644--- a/products/experiments/backend/experiment_service.py+++ b/products/experiments/backend/experiment_service.py@@ -5023,10 +5023,21 @@ class ExperimentService:new_primary_uuids = new_uuids["primary"]new_secondary_uuids = new_uuids["secondary"]+ # An inline metric can share its uuid with a detached shared metric. The ordering entry+ # still belongs to the inline metric, so keep it.+ inline_primary_uuids = {+ uuid for m in update_data.get("metrics", experiment.metrics) or [] if (uuid := m.get("uuid"))+ }+ inline_secondary_uuids = {+ uuid+ for m in update_data.get("metrics_secondary", experiment.metrics_secondary) or []+ if (uuid := m.get("uuid"))+ }+added_primary = new_primary_uuids - old_saved_metric_uuids["primary"]- removed_primary = old_saved_metric_uuids["primary"] - new_primary_uuids+ removed_primary = old_saved_metric_uuids["primary"] - new_primary_uuids - inline_primary_uuidsadded_secondary = new_secondary_uuids - old_saved_metric_uuids["secondary"]- removed_secondary = old_saved_metric_uuids["secondary"] - new_secondary_uuids+ removed_secondary = old_saved_metric_uuids["secondary"] - new_secondary_uuids - inline_secondary_uuids# Fields whose new value is purely a side effect of add/remove — save these# via a muted save to avoid logging a spurious "reordered metrics" entrydiff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.pyindex d3122540610e..2cbda04d1ee2 100644--- a/products/experiments/backend/test/test_experiment_service.py+++ b/products/experiments/backend/test/test_experiment_service.py@@ -6318,6 +6318,40 @@ class TestExperimentService(APIBaseTest):assert saved_metric_uuid in experiment.primary_metrics_ordered_uuidsassert inline_uuid in experiment.primary_metrics_ordered_uuids+ @parameterized.expand(+ [+ ("primary", "metrics", "primary_metrics_ordered_uuids"),+ ("secondary", "metrics_secondary", "secondary_metrics_ordered_uuids"),+ ]+ )+ def test_detaching_saved_metric_keeps_uuid_its_stored_inline_copy_uses(+ self, metric_type: str, field: str, ordering_attr: str+ ) -> None:+ self._create_flag(key="detach-stored-collision")+ shared_uuid = "99bfb66a-51f5-48d0-a87e-bde2b4c958a6"+ metric = {+ "kind": "ExperimentMetric",+ "metric_type": "mean",+ "uuid": shared_uuid,+ "source": {"kind": "EventsNode", "event": "$pageview"},+ }+ sm = ExperimentSavedMetric.objects.create(team=self.team, name="Linked", query=metric)+ service = self._service()+ experiment = service.create_experiment(+ name="Detach stored collision",+ feature_flag_key="detach-stored-collision",+ allow_unknown_events=True,+ saved_metrics_ids=[{"id": sm.id, "metadata": {"type": metric_type}}],+ )+ Experiment.objects.filter(id=experiment.id).update(**{field: [metric], ordering_attr: [shared_uuid]})+ experiment.refresh_from_db()++ updated = service.update_experiment(experiment, {"saved_metrics_ids": []})++ assert not updated.experimenttosavedmetric_set.exists()+ assert [m["uuid"] for m in getattr(updated, field)] == [shared_uuid]+ assert getattr(updated, ordering_attr) == [shared_uuid]+def test_clone_regenerates_uuids_even_when_source_uuid_matches_saved_metric(self):"""Cloning regenerates inline metric uuids so they no longer collide with thesaved metric's uuid carried by the cloned saved-metric link."""