Skip to content

: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.

fix(devex): let partially migrated databases migrate after the squash (#99932)

Julian Beze3d1370fc787
diff --git a/posthog/migrations/0001_squash_2026_09_07_initial.py b/posthog/migrations/0001_squash_2026_09_07_initial.py
index 274f9a1bd9cf..3eadfb5494f1 100644
--- a/posthog/migrations/0001_squash_2026_09_07_initial.py
+++ b/posthog/migrations/0001_squash_2026_09_07_initial.py
@@ -1278,10 +1278,6 @@ class Migration(migrations.Migration):
initial = True
- run_before = [
- ("oauth2_provider", "0001_initial"),
- ]
-
dependencies = [
("auth", "__latest__"),
("posthog", "0000_squash_stub"),
diff --git a/posthog/migrations/1344_squash_2026_09_07_finalize_fks.py b/posthog/migrations/1344_squash_2026_09_07_finalize_fks.py
index feb5e4f0af8f..bc7720472a93 100644
--- a/posthog/migrations/1344_squash_2026_09_07_finalize_fks.py
+++ b/posthog/migrations/1344_squash_2026_09_07_finalize_fks.py
@@ -22,6 +22,7 @@ class Migration(migrations.Migration):
("exports", "0001_squash_2026_09_07_initial"),
("feature_flags", "0001_squash_2026_09_07_initial"),
("notebooks", "0001_squash_2026_09_07_initial"),
+ ("posthog", "0000_squash_stub"),
("posthog", "0001_squash_2026_09_07_initial"),
("product_analytics", "0001_squash_2026_09_07_initial"),
("replay_vision", "0001_squash_2026_09_07_initial"),
diff --git a/posthog/test/repo_invariants/test_squash_partial_fallback.py b/posthog/test/repo_invariants/test_squash_partial_fallback.py
new file mode 100644
index 000000000000..732153f14b1f
--- /dev/null
+++ b/posthog/test/repo_invariants/test_squash_partial_fallback.py
@@ -0,0 +1,47 @@
+from django.db.migrations import Migration
+from django.db.migrations.loader import MigrationLoader
+
+
+def _disk_migrations() -> dict[tuple[str, str], Migration]:
+ loader = MigrationLoader(connection=None, load=False)
+ loader.load_disk()
+ return loader.disk_migrations
+
+
+def test_replacing_squash_migrations_declare_no_run_before() -> None:
+ offenders = sorted(
+ f"{app}.{name}"
+ for (app, name), migration in _disk_migrations().items()
+ if migration.replaces and migration.run_before
+ )
+ assert not offenders, (
+ "These squash migrations declare run_before while they still replace migrations. On a partially migrated "
+ "database Django drops the squash and moves the run_before target onto the last replaced migration, so "
+ "check_consistent_history refuses to migrate. Remove run_before; the replaced files already carry it.\n "
+ + "\n ".join(offenders)
+ )
+
+
+def test_squash_same_app_dependencies_keep_a_child_outside_the_replaced_range() -> None:
+ disk = _disk_migrations()
+ orphaned: list[str] = []
+ for (app, name), squash in sorted(disk.items()):
+ if not squash.replaces:
+ continue
+ replaced = set(squash.replaces)
+ for dependency in squash.dependencies:
+ if dependency[0] != app or dependency not in disk:
+ continue
+ survivors = [
+ key
+ for key, migration in disk.items()
+ if key[0] == app and key != (app, name) and key not in replaced and dependency in migration.dependencies
+ ]
+ if not survivors:
+ orphaned.append(f"{dependency[0]}.{dependency[1]} (squash {app}.{name})")
+ assert not orphaned, (
+ "These migrations have the squash as their only child in their app. On a partially migrated database "
+ "Django drops the squash, the migration becomes a second leaf of the app, and migrate stops with "
+ "'Conflicting migrations detected'. Make a migration that runs after the squash, such as its "
+ "finalize_fks tail, depend on it too.\n " + "\n ".join(orphaned)
+ )