: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(migrations): give MigrationRisk a score alias (#96222)
posthog[bot]702da3b3dbf3
diff --git a/.agents/skills/django-migrations/SKILL.md b/.agents/skills/django-migrations/SKILL.mdindex 59a844de10ff..345823c13b35 100644--- a/.agents/skills/django-migrations/SKILL.md+++ b/.agents/skills/django-migrations/SKILL.md@@ -83,6 +83,10 @@ All concurrent-index ops require `atomic = False`.Meta-principle when you hit a risky-but-common pattern with no helper: don't hand-roll the safe DDL from docs — ship a drop-in helper in `posthog/migration_helpers` and point the CI policy at it. A one-import helper beats a wall of caveated `RunSQL` every time.+## Scripting against the risk analyzer++`posthog/management/migration_analysis/models.py` holds two risk dataclasses. `OperationRisk` scores one operation; `MigrationRisk` scores a whole migration file and exposes `max_score`, `level`, and `category`. Both answer `.score`, so on a `MigrationRisk` you can read either `score` or `max_score` — they return the same number.+## Hot table hazard`posthog_team`, `posthog_user`, `posthog_organization`, and `posthog_project` are read on virtually every request. Any `ALTER TABLE` on them — including a plain nullable `AddField`, which is "safe" everywhere else — needs an `ACCESS EXCLUSIVE` lock, and while that lock request waits behind in-flight queries, every later query on the table queues behind it. Even a metadata-only `ADD COLUMN` can stall site-wide traffic in waves (one per `bin/migrate` retry) until the ALTER wins the lock race. This has caused production 5xx incidents.diff --git a/posthog/management/migration_analysis/models.py b/posthog/management/migration_analysis/models.pyindex 2e57ff3e902e..fd19f7748a00 100644--- a/posthog/management/migration_analysis/models.py+++ b/posthog/management/migration_analysis/models.py@@ -3,6 +3,8 @@from dataclasses import dataclass, fieldfrom enum import Enum+from posthog.dataclasses import frozen+class RiskLevel(Enum):"""Risk level definitions with scoring ranges"""@@ -40,7 +42,7 @@ class OperationRisk:return RiskLevel.from_score(self.score)-@dataclass+@frozenclass MigrationRisk:path: strapp: str@@ -63,6 +65,11 @@ class MigrationRisk:return max(base_score, 4)return base_score+ @property+ def score(self) -> int:+ """Alias of `max_score`, so both risk dataclasses answer `.score`."""+ return self.max_score+@propertydef level(self) -> RiskLevel:return RiskLevel.from_score(self.max_score)diff --git a/posthog/management/migration_analysis/test/test_risk_analyzer.py b/posthog/management/migration_analysis/test/test_risk_analyzer.pyindex 0d7e651298a0..532113470135 100644--- a/posthog/management/migration_analysis/test/test_risk_analyzer.py+++ b/posthog/management/migration_analysis/test/test_risk_analyzer.py@@ -6,7 +6,7 @@ from django.db import migrations, modelsfrom parameterized import parameterizedfrom posthog.management.migration_analysis.analyzer import RiskAnalyzer-from posthog.management.migration_analysis.models import RiskLevel+from posthog.management.migration_analysis.models import MigrationRisk, OperationRisk, RiskLevelfrom posthog.management.migration_analysis.policies import (AtomicFalsePolicy,ConcurrentIndexIdempotencyPolicy,@@ -49,6 +49,26 @@ class TestRiskLevelScoring:assert RiskLevel.from_score(-1) == RiskLevel.SAFE+class TestMigrationRiskScore:+ @parameterized.expand(+ [+ ("no_boost", [], 3),+ ("policy_violation_boost", ["hot table alter"], 4),+ ]+ )+ def test_score_matches_max_score(self, _name, policy_violations, expected):+ risk = MigrationRisk(+ path="posthog.0001_initial",+ app="posthog",+ name="0001_initial",+ operations=[OperationRisk(type="AddField", score=3, reason="test", details={})],+ policy_violations=policy_violations,+ )++ assert risk.score == expected+ assert risk.score == risk.max_score++class TestAddFieldOperations:def setup_method(self):self.analyzer = RiskAnalyzer()diff --git a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt b/posthog/test/repo_invariants/dataclass_frozen_baseline.txtindex ef59f985bed3..b0eae65d1e81 100644--- a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt+++ b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt@@ -1384,7 +1384,7 @@1 posthog/hogql/context.py2 posthog/hogql/database/models.py2 posthog/management/commands/generate_test_events.py-2 posthog/management/migration_analysis/models.py+1 posthog/management/migration_analysis/models.py2 posthog/models/data_deletion_request.py2 posthog/models/js_snippet_versioning.py2 posthog/models/kafka_partition_stats/sql.py