: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(flags): clean up malformed multivariate in flag filters (#68030)
Patricio2bb7cedf4664
diff --git a/products/feature_flags/backend/migrations/0007_clean_invalid_multivariate_filters.py b/products/feature_flags/backend/migrations/0007_clean_invalid_multivariate_filters.pynew file mode 100644index 000000000000..79cc97017664--- /dev/null+++ b/products/feature_flags/backend/migrations/0007_clean_invalid_multivariate_filters.py@@ -0,0 +1,77 @@+from django.db import migrations+from django.db.models import Q++import structlog++logger = structlog.get_logger(__name__)++BATCH_SIZE = 500+++def clean_invalid_multivariate_filters(apps, schema_editor):+ """+ One-time cleanup before strict filters validation ships (#50084):+ null out malformed `filters.multivariate` (not an object, or `variants`+ missing/empty/not a list). These flags already evaluate as boolean, so+ also drop the now-dangling group variant overrides and any payload keys+ other than "true". Includes soft-deleted flags so a restore can't+ resurrect a violation.+ """+ FeatureFlag = apps.get_model("feature_flags", "FeatureFlag")++ # _base_manager: the default manager excludes soft-deleted flags.+ # Deliberately broad (any non-null multivariate): key-transform lookups can't+ # match a non-array `variants` scalar, so the per-row guard below decides.+ candidates = FeatureFlag._base_manager.filter(Q(filters__has_key="multivariate") & ~Q(filters__multivariate=None))++ total = 0+ batch: list = []++ for flag in candidates.iterator(chunk_size=BATCH_SIZE):+ if not isinstance(flag.filters, dict):+ continue++ multivariate = flag.filters.get("multivariate")+ if multivariate is None:+ continue+ variants = multivariate.get("variants") if isinstance(multivariate, dict) else None+ if isinstance(variants, list) and len(variants) > 0:+ continue++ new_filters = {**flag.filters, "multivariate": None}++ # The flag is boolean: no variant overrides can resolve,+ # and only the "true" payload key is meaningful.+ groups = new_filters.get("groups")+ if isinstance(groups, list):+ new_filters["groups"] = [+ {**group, "variant": None} if isinstance(group, dict) and group.get("variant") is not None else group+ for group in groups+ ]+ payloads = new_filters.get("payloads")+ if isinstance(payloads, dict) and any(key != "true" for key in payloads):+ new_filters["payloads"] = {key: value for key, value in payloads.items() if key == "true"}++ flag.filters = new_filters+ batch.append(flag)++ if len(batch) >= BATCH_SIZE:+ FeatureFlag._base_manager.bulk_update(batch, ["filters"])+ total += len(batch)+ batch = []++ if batch:+ FeatureFlag._base_manager.bulk_update(batch, ["filters"])+ total += len(batch)++ logger.info("cleaned_invalid_multivariate_filters", updated_rows=total)+++class Migration(migrations.Migration):+ dependencies = [+ ("feature_flags", "0006_validate_archived_flag_must_be_disabled"),+ ]++ operations = [+ migrations.RunPython(clean_invalid_multivariate_filters, migrations.RunPython.noop, elidable=True),+ ]diff --git a/products/feature_flags/backend/migrations/max_migration.txt b/products/feature_flags/backend/migrations/max_migration.txtindex 0eb81c439068..940cf879f893 100644--- a/products/feature_flags/backend/migrations/max_migration.txt+++ b/products/feature_flags/backend/migrations/max_migration.txt@@ -1 +1 @@-0006_validate_archived_flag_must_be_disabled+0007_clean_invalid_multivariate_filters