: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
chore(experiments): add a partial unique index on feature flag rule IDs (#99297)
Andersf916415a73e4
diff --git a/products/experiments/backend/migrations/0040_experiment_feature_flag_rule_id_unique.py b/products/experiments/backend/migrations/0040_experiment_feature_flag_rule_id_unique.pynew file mode 100644index 000000000000..8c88f9276e27--- /dev/null+++ b/products/experiments/backend/migrations/0040_experiment_feature_flag_rule_id_unique.py@@ -0,0 +1,36 @@+from django.db import migrations, models++from posthog.migration_helpers import CreateIndexConcurrently+++class Migration(migrations.Migration):+ atomic = False++ dependencies = [("experiments", "0039_experiment_dormant_v2_analysis_columns")]++ operations = [+ # A partial unique constraint compiles to a partial unique index, which Django's+ # AddConstraint builds under an ACCESS EXCLUSIVE lock. Build the index concurrently+ # instead and record only the constraint in Django's state.+ migrations.SeparateDatabaseAndState(+ state_operations=[+ migrations.AddConstraint(+ model_name="experiment",+ constraint=models.UniqueConstraint(+ fields=["feature_flag_rule_id"],+ condition=models.Q(feature_flag_rule_id__isnull=False),+ name="posthog_experiment_feature_flag_rule_id_uniq",+ ),+ ),+ ],+ database_operations=[+ CreateIndexConcurrently(+ index_name="posthog_experiment_feature_flag_rule_id_uniq",+ table_name="posthog_experiment",+ columns="(feature_flag_rule_id)",+ unique=True,+ where="WHERE feature_flag_rule_id IS NOT NULL",+ ),+ ],+ ),+ ]diff --git a/products/experiments/backend/migrations/max_migration.txt b/products/experiments/backend/migrations/max_migration.txtindex 8880c4c93f66..b59fa4a53821 100644--- a/products/experiments/backend/migrations/max_migration.txt+++ b/products/experiments/backend/migrations/max_migration.txt@@ -1 +1 @@-0039_experiment_dormant_v2_analysis_columns+0040_experiment_feature_flag_rule_id_uniquediff --git a/products/experiments/backend/models/experiment.py b/products/experiments/backend/models/experiment.pyindex 9ac0322a1511..9049fbcf9afb 100644--- a/products/experiments/backend/models/experiment.py+++ b/products/experiments/backend/models/experiment.py@@ -159,6 +159,14 @@ class Experiment(FileSystemSyncMixin, ModelActivityMixin, RootTeamMixin, models.class Meta:db_table = "posthog_experiment"+ constraints = [+ # Rule IDs are UUIDs that no later experiment may reuse, so uniqueness is global, not per team.+ models.UniqueConstraint(+ fields=["feature_flag_rule_id"],+ condition=models.Q(feature_flag_rule_id__isnull=False),+ name="posthog_experiment_feature_flag_rule_id_uniq",+ )+ ]def __str__(self):return self.name or "Untitled"diff --git a/products/experiments/backend/test/test_experiment_feature_flag_rule_id_unique.py b/products/experiments/backend/test/test_experiment_feature_flag_rule_id_unique.pynew file mode 100644index 000000000000..0f0eacf605ad--- /dev/null+++ b/products/experiments/backend/test/test_experiment_feature_flag_rule_id_unique.py@@ -0,0 +1,31 @@+import uuid++from posthog.test.base import BaseTest++from django.db import IntegrityError, transaction++from products.experiments.backend.models.experiment import Experiment+from products.feature_flags.backend.models.feature_flag import FeatureFlag+++class TestExperimentFeatureFlagRuleIdUnique(BaseTest):+ def setUp(self) -> None:+ super().setUp()+ self.flag = FeatureFlag.objects.create(team=self.team, key="rule-id-unique", created_by=self.user)++ def _create(self, rule_id: uuid.UUID | None = None) -> Experiment:+ return Experiment.objects.create(+ team=self.team, name="experiment", feature_flag=self.flag, feature_flag_rule_id=rule_id+ )++ def test_duplicate_rule_id_is_rejected_and_null_rows_are_not(self) -> None:+ rule_id = uuid.uuid4()+ self._create(rule_id)++ with self.assertRaises(IntegrityError) as caught, transaction.atomic():+ self._create(rule_id)+ assert 'unique constraint "posthog_experiment_feature_flag_rule_id_uniq"' in str(caught.exception)++ # Legacy experiments keep the column null; the partial index must not treat them as duplicates.+ self._create()+ self._create()