: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(migrations): correct the two-phase column drop guidance (#96798)
Rafael Audibert7a84d28ea473
diff --git a/.agents/skills/django-migrations/SKILL.md b/.agents/skills/django-migrations/SKILL.mdindex 0886e2df8b41..a1e3c40ea57b 100644--- a/.agents/skills/django-migrations/SKILL.md+++ b/.agents/skills/django-migrations/SKILL.md@@ -30,6 +30,21 @@ To retire a model/table:Full guide: `safe-django-migrations.md` (`## Dropping Tables`, `### Removing a whole product or app`). Deleting a migration your branch added but never merged to master is allowed (regenerating).+## Retire a column in two phases++Deleting the field and running `makemigrations` is not the first phase.+Django generates a plain `RemoveField`, which drops the column in the same deploy that removes the code.+Old pods still write to that column, and a rollback finds it gone.++1. Remove all usage and the field from the model. `makemigrations`, then wrap the generated `RemoveField` in `migrations.SeparateDatabaseAndState(state_operations=[...], database_operations=[])`. The column stays in Postgres. Example: `posthog/migrations/1328_remove_userproductlist_reason_state.py`.+2. Deploy, wait at least one full deploy cycle, and confirm no deployed code reads the column.+3. Drop the column in a NEW `RunSQL` migration with `ALTER TABLE ... DROP COLUMN IF EXISTS`. Example: `posthog/migrations/1340_drop_userproductlist_reason_columns.py`.++`RemoveFieldAnalyzer` scores a bare `RemoveField` at 5, the highest risk the "Migration Risk Analysis" CI job reports.+The phase 2 drop scores low only when `check_drop_properly_staged` finds the phase 1 state removal in an ancestor migration, so the two phases must land in that order and never in one migration.++Full guide: `safe-django-migrations.md` (`## Dropping Columns`).+## Retire dedicated migration testsA data migration test protects the rollout, not the permanent behavior of the product. Remove the dedicated test after all supported environments have applied the migration, the rollback window has closed, and no supported upgrade still relies on the old data state.diff --git a/docs/published/handbook/engineering/safe-django-migrations.md b/docs/published/handbook/engineering/safe-django-migrations.mdindex 0e6e3b78f433..d29c5b89dc90 100644--- a/docs/published/handbook/engineering/safe-django-migrations.md+++ b/docs/published/handbook/engineering/safe-django-migrations.md@@ -233,17 +233,44 @@ Leaving the table in place — code gone, table dropped in a follow-up — is a### Safe Approach-Use the same multi-phase pattern as [Dropping Tables](#dropping-tables):+Use the same multi-phase pattern as [Dropping Tables](#dropping-tables).+Deleting the field and running `makemigrations` is **not** phase 1 on its own: Django generates a plain `RemoveField`, which drops the column in the same deploy that removes the code.-1. Remove the field from your Django model (keeps column in database)-2. Deploy and verify no code references it (application servers, workers, background jobs)-3. Wait at least one full deployment cycle-4. Optionally drop the column with `RemoveField` in a later migration+**Phase 1: remove the field from Django state only.**+Delete every reference to the field and the field itself from the model, run `makemigrations`, then wrap the generated `RemoveField` in `SeparateDatabaseAndState`:++```python+# 1328_remove_userproductlist_reason_state.py+operations = [+ migrations.SeparateDatabaseAndState(+ state_operations=[+ migrations.RemoveField(model_name="userproductlist", name="reason"),+ ],+ database_operations=[],+ ),+]+```++The column stays in Postgres, so in-flight requests on the old release keep working and a rollback still finds it.+Deploy this, and verify no code references the field (application servers, workers, background jobs).++**Phase 2: drop the column, at least one full deployment cycle later.**++```python+# 1340_drop_userproductlist_reason_columns.py+operations = [+ migrations.RunSQL(+ sql='ALTER TABLE "posthog_userproductlist" DROP COLUMN IF EXISTS "reason";',+ reverse_sql='ALTER TABLE "posthog_userproductlist" ADD COLUMN IF NOT EXISTS "reason" varchar(32) NULL;',+ ),+]+```**Important notes:**-- `RemoveField` operations are irreversible - column data is permanently deleted-- `DROP COLUMN` takes an `ACCESS EXCLUSIVE` lock (briefly) - schedule during low-traffic windows+- Dropping a column is irreversible - the data is permanently deleted. `reverse_sql` can re-add an empty column so the migration unapplies, but it cannot restore the values+- `DROP COLUMN` takes an `ACCESS EXCLUSIVE` lock (briefly) - on a [hot table](#altering-hot-tables) schedule it for a low-traffic window+- The "Migration Risk Analysis" CI job scores a bare `RemoveField` at 5, its highest risk. Phase 2 scores low only when the analyzer finds the phase 1 state removal in an ancestor migration, so the two phases must land in that order- Consider leaving unused columns indefinitely to avoid data loss risks## Renaming Tablesdiff --git a/posthog/management/migration_analysis/operations.py b/posthog/management/migration_analysis/operations.pyindex d2a0863cb41e..e9cf7b32567a 100644--- a/posthog/management/migration_analysis/operations.py+++ b/posthog/management/migration_analysis/operations.py@@ -170,10 +170,10 @@ class RemoveFieldAnalyzer(OperationAnalyzer):score=5,reason="Dropping column breaks backwards compatibility and can't rollback",details={"model": op.model_name, "field": op.name},- guidance=f"""Multi-phase column drop:-1. Remove field from Django model (keeps column in DB)+ guidance=f"""Use SeparateDatabaseAndState for multi-phase column drops:+1. Remove field from Django state (state_operations only, column stays in DB)2. Wait at least one full deployment cycle-3. Optionally drop column with RemoveField+3. Drop the column with RunSQL: ALTER TABLE ... DROP COLUMN IF EXISTS[See the migration safety guide]({SAFE_MIGRATIONS_DOCS_URL}#dropping-columns)""",)