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.

perf(devex): skip django migrate walk on product test dbs (#60292)

Julian Bez79126eb4aadb
diff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.py
index 46f1824c0774..68cdc89c1e0e 100644
--- a/posthog/settings/data_stores.py
+++ b/posthog/settings/data_stores.py
@@ -218,11 +218,23 @@ for route in product_routes:
DATABASES[reader_alias].setdefault("OPTIONS", {})["connect_timeout"] = 3
if TEST:
- # Tell Django's test runner to create an independent test database and run
- # migrations (via the router). Without this, test databases are created
- # but left empty. Reader shares the writer's test database so reads inside
- # a write transaction see uncommitted data.
- DATABASES[writer_alias]["TEST"] = {"DEPENDENCIES": []}
+ # Skip the global migration-graph walk during test DB setup. Without
+ # `MIGRATE: False`, Django's `create_test_db` calls `migrate --database
+ # <alias>` with no app filter, which walks the full ~1300-migration
+ # graph for `state_forwards` on every alias — even though
+ # `ProductDBRouter.allow_migrate` gates DDL to just the owning product
+ # app. The state-machine walk runs regardless of the router and burns
+ # ~5 min per affected shard on a fresh test DB.
+ #
+ # `MIGRATE: False` makes `create_test_db` skip migrations and fall
+ # through to `run_syncdb=True`, which creates tables directly from the
+ # current model definitions for any app that `allow_migrate` permits.
+ # For Django-owned product DBs (`managed=True` models), that yields
+ # the same final-schema tables in milliseconds instead of minutes.
+ #
+ # Reader shares the writer's test database so reads inside a write
+ # transaction see uncommitted data.
+ DATABASES[writer_alias]["TEST"] = {"MIGRATE": False, "DEPENDENCIES": []}
DATABASES[reader_alias]["TEST"] = {"MIRROR": writer_alias}
if DISABLE_SERVER_SIDE_CURSORS: