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(ci): replay uncached migrations outside pytest (#80472)

Raúl Negrón-Oteroa0d2fa2e4b96
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.yml
index 8f4edf43922e..7577308f9fe9 100644
--- a/.depot/workflows/ci-backend.yml
+++ b/.depot/workflows/ci-backend.yml
@@ -910,17 +910,42 @@ jobs:
restore-keys: |
${{ needs.turbo-discover.outputs.schema_cache_key }}
- name: Prime test_posthog from cached schema
- # No-op on cache miss; pytest --reuse-db falls back to a full migrate.
+ # Treat cache problems as misses; the migrate step below rebuilds the DB
+ # whenever this one reports restored=false.
+ id: prime-schema
if: ${{ github.event_name == 'pull_request' }}
run: |
+ echo "restored=false" >> "$GITHUB_OUTPUT"
if [ ! -f schema.sql.gz ]; then
- echo "::notice::Schema cache miss — pytest --reuse-db will run full migrations"
+ echo "::notice::Schema cache miss; the migrate step will build test_posthog"
exit 0
fi
mkdir -p .postgres-backups
mv schema.sql.gz .postgres-backups/schema-latest.sql.gz
- if ! ./bin/hogli db:restore-test-db; then
- echo "::warning::Schema restore failed (stale/incompatible cached dump?) — pytest --reuse-db will run a full migrate"
+ if ./bin/hogli db:restore-test-db; then
+ echo "restored=true" >> "$GITHUB_OUTPUT"
+ else
+ echo "::warning::Schema restore failed (stale/incompatible cached dump?); the migrate step will build test_posthog"
+ fi
+ - name: Migrate test_posthog from scratch
+ # Any run without a restored schema cache (master, dispatches, PR cache
+ # misses) replays the full migration chain here, in a process that exits
+ # before tests: the executor's memory high-water stays resident in
+ # whichever process runs it, and pytest must not carry it through the
+ # test phase. pytest --reuse-db adopts the DB like the PR prime above;
+ # any failure falls back to the in-pytest migrate.
+ if: ${{ github.event_name != 'pull_request' || steps.prime-schema.outputs.restored != 'true' }}
+ env:
+ DATABASE_URL: postgres://posthog:posthog@localhost:5432/test_posthog
+ run: |
+ admin_psql() { docker compose -f docker-compose.dev.yml exec -T db psql -U posthog postgres "$@"; }
+ if ! admin_psql -c "DROP DATABASE IF EXISTS test_posthog;" -c "CREATE DATABASE test_posthog;"; then
+ echo "::warning::Could not recreate test_posthog; pytest --reuse-db will run the full migrate itself"
+ exit 0
+ fi
+ if ! python manage.py migrate; then
+ echo "::warning::Out-of-process migrate failed; pytest --reuse-db will run the full migrate itself"
+ admin_psql -c "DROP DATABASE IF EXISTS test_posthog;" || true
fi
- name: Run product tests
# --force: discover already decided this product needs testing, skip turbo cache
@@ -1789,17 +1814,42 @@ jobs:
restore-keys: |
${{ needs.turbo-discover.outputs.schema_cache_key }}
- name: Prime test_posthog from cached schema
- # No-op on cache miss; pytest --reuse-db falls back to a full migrate.
+ # Treat cache problems as misses; the migrate step below rebuilds the DB
+ # whenever this one reports restored=false.
+ id: prime-schema
if: ${{ github.event_name == 'pull_request' }}
run: |
+ echo "restored=false" >> "$GITHUB_OUTPUT"
if [ ! -f schema.sql.gz ]; then
- echo "::notice::Schema cache miss — pytest --reuse-db will run full migrations"
+ echo "::notice::Schema cache miss; the migrate step will build test_posthog"
exit 0
fi
mkdir -p .postgres-backups
mv schema.sql.gz .postgres-backups/schema-latest.sql.gz
- if ! ./bin/hogli db:restore-test-db; then
- echo "::warning::Schema restore failed (stale/incompatible cached dump?) — pytest --reuse-db will run a full migrate"
+ if ./bin/hogli db:restore-test-db; then
+ echo "restored=true" >> "$GITHUB_OUTPUT"
+ else
+ echo "::warning::Schema restore failed (stale/incompatible cached dump?); the migrate step will build test_posthog"
+ fi
+ - name: Migrate test_posthog from scratch
+ # Any run without a restored schema cache (master, dispatches, PR cache
+ # misses) replays the full migration chain here, in a process that exits
+ # before tests: the executor's memory high-water stays resident in
+ # whichever process runs it, and pytest must not carry it through the
+ # test phase. pytest --reuse-db adopts the DB like the PR prime above;
+ # any failure falls back to the in-pytest migrate.
+ if: ${{ github.event_name != 'pull_request' || steps.prime-schema.outputs.restored != 'true' }}
+ env:
+ DATABASE_URL: postgres://posthog:posthog@localhost:5432/test_posthog
+ run: |
+ admin_psql() { docker compose -f docker-compose.dev.yml exec -T db psql -U posthog postgres "$@"; }
+ if ! admin_psql -c "DROP DATABASE IF EXISTS test_posthog;" -c "CREATE DATABASE test_posthog;"; then
+ echo "::warning::Could not recreate test_posthog; pytest --reuse-db will run the full migrate itself"
+ exit 0
+ fi
+ if ! python manage.py migrate; then
+ echo "::warning::Out-of-process migrate failed; pytest --reuse-db will run the full migrate itself"
+ admin_psql -c "DROP DATABASE IF EXISTS test_posthog;" || true
fi
# Tests
- name: Set snapshot update flags
diff --git a/.github/scripts/optimize_test_durations.py b/.github/scripts/optimize_test_durations.py
index cb5d3e4ef0d9..6723b69ee8ca 100644
--- a/.github/scripts/optimize_test_durations.py
+++ b/.github/scripts/optimize_test_durations.py
@@ -13,12 +13,14 @@ Prepare test durations for pytest-split sharding.
Merges timing artifacts from CI shards and removes migration-tax
contamination using JUnit call times as a setup-free contamination signal.
-Under --reuse-db the per-shard Django DB build (~7 min on master) is
-absorbed into whichever test first touches the DB, inflating its recorded
-duration and skewing pytest-split. This script merges the per-shard
-artifacts, floors any test recorded far above its JUnit call time (or
-sitting at a flat-default placeholder) back to that call time, and outputs
-clean durations for balanced distribution.
+Jobs without a restored schema cache build the test DB in the "Migrate
+test_posthog from scratch" workflow step, but when that step fails,
+--reuse-db falls back to building it in-process and the walk is absorbed
+into whichever test first touches the DB, inflating its recorded duration
+and skewing pytest-split.
+This script merges the per-shard artifacts, floors any test recorded far
+above its JUnit call time (or sitting at a flat-default placeholder) back
+to that call time, and outputs clean durations for balanced distribution.
"""
import re
@@ -43,10 +45,11 @@ MIN_DURATION = 0.01
# are candidates for migration carriers (real tests rarely exceed this)
CARRIER_THRESHOLD_SECONDS = 200.0
# A test recorded this far above its JUnit call time absorbed per-shard DB
-# setup. Under --reuse-db the migration walk lands on whichever test first
-# touches the DB (not reliably the first test in the file), so detect it by
-# the call-time gap rather than by position. Migration/DB setup is hundreds
-# of seconds; legit per-test setup is tens at most, so this separates them.
+# setup. When the out-of-process migrate step fell back to in-pytest, the
+# walk lands on whichever test first touches the DB (not reliably the first
+# test in the file), so detect it by the call-time gap rather than by
+# position. Migration/DB setup is hundreds of seconds; legit per-test setup
+# is tens at most, so this separates them.
MIGRATION_TAX_THRESHOLD_SECONDS = 120.0
# pytest-split writes these flat values for tests it has no timing for. They
# are placeholders, not measurements — when JUnit has a real call time for
@@ -239,10 +242,13 @@ class TimingMerger:
class MigrationTaxCorrector:
"""Removes migration-tax contamination from merged durations.
- Under --reuse-db the per-shard Django DB build (~7 min on master) lands
- on whichever test first touches the DB, inflating that test's recorded
- setup+call duration and skewing pytest-split's shard balancing. The
- outlier-merge then prefers that inflated value over the test's real one.
+ When a job's out-of-process migrate step fails, --reuse-db builds the
+ DB in-process and the walk lands on whichever test first touches the
+ DB, inflating that test's recorded setup+call duration and skewing
+ pytest-split's shard balancing. The outlier-merge then prefers that
+ inflated value over the test's real one. Normally the walk happens in
+ its own workflow step (or a cached schema is restored), so no test
+ carries tax and both modes below are cheap no-ops.
Two modes:
- JUnit-based (preferred): JUnit call time is the call phase only, so a
@@ -352,7 +358,11 @@ class MigrationTaxCorrector:
Uses expected_shard_count as the number of carriers to look for.
Only selects tests above CARRIER_THRESHOLD_SECONDS to avoid
- false positives from genuinely slow tests.
+ false positives from genuinely slow tests. That guard is now the
+ whole story on tax-free runs (the normal path migrates
+ out-of-process): a genuine test above the threshold would be
+ misread as a carrier and floored, so if one ever appears, raise
+ the threshold or drop this mode rather than trusting it.
"""
candidates = sorted(self.durations.items(), key=lambda x: -x[1])
diff --git a/.github/scripts/turbo-discover.js b/.github/scripts/turbo-discover.js
index 17deeb45a294..4212d924cc93 100644
--- a/.github/scripts/turbo-discover.js
+++ b/.github/scripts/turbo-discover.js
@@ -74,8 +74,9 @@ const STALENESS_FALLBACK_SECONDS_PER_FILE = 5
//
// .test_durations has migration-tax contamination removed by
// optimize_test_durations.py: tests recorded far above their JUnit call
-// time (the DB-setup walk lands on whichever test first hits the DB) are
-// floored back to that call time. Durations reflect actual test work.
+// time (the DB-setup walk lands on the first DB-touching test whenever a
+// run built the DB in-pytest) are floored back to that call time.
+// Durations reflect actual test work.
//
// Per-segment overhead constants below cover the fixed per-shard cost
// outside test work: job setup, pytest collection, per-shard DB setup,
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index 435d0714e6bf..42d76a8aeacd 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -907,17 +907,43 @@ jobs:
${{ needs.turbo-discover.outputs.schema_cache_key }}
- name: Prime test_posthog from cached schema
- # Treat cache problems as misses; db:restore-test-db recreates the DB after failure.
+ # Treat cache problems as misses; the migrate step below rebuilds the DB
+ # whenever this one reports restored=false.
+ id: prime-schema
if: ${{ github.event_name == 'pull_request' }}
run: |
+ echo "restored=false" >> "$GITHUB_OUTPUT"
if [ ! -f schema.sql.gz ]; then
- echo "::notice::Schema cache miss — pytest --reuse-db will run full migrations"
+ echo "::notice::Schema cache miss; the migrate step will build test_posthog"
exit 0
fi
mkdir -p .postgres-backups
mv schema.sql.gz .postgres-backups/schema-latest.sql.gz
- if ! ./bin/hogli db:restore-test-db; then
- echo "::warning::Schema restore failed (stale/incompatible cached dump?) — pytest --reuse-db will run a full migrate"
+ if ./bin/hogli db:restore-test-db; then
+ echo "restored=true" >> "$GITHUB_OUTPUT"
+ else
+ echo "::warning::Schema restore failed (stale/incompatible cached dump?); the migrate step will build test_posthog"
+ fi
+
+ - name: Migrate test_posthog from scratch
+ # Any run without a restored schema cache (master, dispatches, PR cache
+ # misses) replays the full migration chain here, in a process that exits
+ # before tests: the executor's memory high-water stays resident in
+ # whichever process runs it, and pytest must not carry it through the
+ # test phase. pytest --reuse-db adopts the DB like the PR prime above;
+ # any failure falls back to the in-pytest migrate.
+ if: ${{ github.event_name != 'pull_request' || steps.prime-schema.outputs.restored != 'true' }}
+ env:
+ DATABASE_URL: postgres://posthog:posthog@localhost:5432/test_posthog
+ run: |
+ admin_psql() { docker compose -f docker-compose.dev.yml exec -T db psql -U posthog postgres "$@"; }
+ if ! admin_psql -c "DROP DATABASE IF EXISTS test_posthog;" -c "CREATE DATABASE test_posthog;"; then
+ echo "::warning::Could not recreate test_posthog; pytest --reuse-db will run the full migrate itself"
+ exit 0
+ fi
+ if ! python manage.py migrate; then
+ echo "::warning::Out-of-process migrate failed; pytest --reuse-db will run the full migrate itself"
+ admin_psql -c "DROP DATABASE IF EXISTS test_posthog;" || true
fi
- name: Register Temporal search attributes
@@ -2792,17 +2818,43 @@ jobs:
${{ needs.turbo-discover.outputs.schema_cache_key }}
- name: Prime test_posthog from cached schema
- # Treat cache problems as misses; db:restore-test-db recreates the DB after failure.
+ # Treat cache problems as misses; the migrate step below rebuilds the DB
+ # whenever this one reports restored=false.
+ id: prime-schema
if: ${{ github.event_name == 'pull_request' }}
run: |
+ echo "restored=false" >> "$GITHUB_OUTPUT"
if [ ! -f schema.sql.gz ]; then
- echo "::notice::Schema cache miss — pytest --reuse-db will run full migrations"
+ echo "::notice::Schema cache miss; the migrate step will build test_posthog"
exit 0
fi
mkdir -p .postgres-backups
mv schema.sql.gz .postgres-backups/schema-latest.sql.gz
- if ! ./bin/hogli db:restore-test-db; then
- echo "::warning::Schema restore failed (stale/incompatible cached dump?) — pytest --reuse-db will run a full migrate"
+ if ./bin/hogli db:restore-test-db; then
+ echo "restored=true" >> "$GITHUB_OUTPUT"
+ else
+ echo "::warning::Schema restore failed (stale/incompatible cached dump?); the migrate step will build test_posthog"
+ fi
+
+ - name: Migrate test_posthog from scratch
+ # Any run without a restored schema cache (master, dispatches, PR cache
+ # misses) replays the full migration chain here, in a process that exits
+ # before tests: the executor's memory high-water stays resident in
+ # whichever process runs it, and pytest must not carry it through the
+ # test phase. pytest --reuse-db adopts the DB like the PR prime above;
+ # any failure falls back to the in-pytest migrate.
+ if: ${{ github.event_name != 'pull_request' || steps.prime-schema.outputs.restored != 'true' }}
+ env:
+ DATABASE_URL: postgres://posthog:posthog@localhost:5432/test_posthog
+ run: |
+ admin_psql() { docker compose -f docker-compose.dev.yml exec -T db psql -U posthog postgres "$@"; }
+ if ! admin_psql -c "DROP DATABASE IF EXISTS test_posthog;" -c "CREATE DATABASE test_posthog;"; then
+ echo "::warning::Could not recreate test_posthog; pytest --reuse-db will run the full migrate itself"
+ exit 0
+ fi
+ if ! python manage.py migrate; then
+ echo "::warning::Out-of-process migrate failed; pytest --reuse-db will run the full migrate itself"
+ admin_psql -c "DROP DATABASE IF EXISTS test_posthog;" || true
fi
- name: Restore test durations from cache