: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(ci): replay the migration history on branches that change one (#92829)
Georges-Antoine Assiaa54489c8b38
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.ymlindex fb24f6ea601e..72246a883bf5 100644--- a/.depot/workflows/ci-backend.yml+++ b/.depot/workflows/ci-backend.yml@@ -266,6 +266,10 @@ jobs:migrations: ${{ steps.filter.outputs.migrations || 'true' }}migrations_files: ${{ steps.filter.outputs.migrations_files }}migrations_any: ${{ steps.filter.outputs.migrations_any || 'true' }}+ # What the replay gate in check-migrations keys off. A ClickHouse or+ # async-migration change does not pay for a replay of a history the Django+ # planner never sees.+ migration_replay: ${{ steps.filter.outputs.migration_replay || 'true' }}tasks_temporal: ${{ steps.filter.outputs.tasks_temporal || 'true' }}openapi_types: ${{ steps.filter.outputs.openapi_types || 'true' }}# The label has to reach discovery as well: `backend` alone only starts the@@ -473,6 +477,19 @@ jobs:# deleted-migration check in repo-checks.migrations_any:- '**/migrations/*.py'+ # Django migration files in any app, plus the routing files that decide+ # which app labels land in which database. Both change the plan a+ # from-scratch migrate builds. Match everything and exclude the two+ # non-Django directories, so a new app is covered without an edit here.+ # An app missed by a positive list would leave the replay gate silently+ # off for it.+ migration_replay:+ - '**/migrations/*.py'+ - '!posthog/clickhouse/migrations/*.py'+ - '!posthog/async_migrations/migrations/*.py'+ - 'products/db_routing.yaml'+ - 'posthog/product_db_config.py'+ - 'posthog/product_db_router.py'tasks_temporal:- 'products/tasks/backend/temporal/**/*'product_yamls:@@ -1172,12 +1189,18 @@ jobs:# branch so repository switches cannot disturb live Compose bind mounts.check-migrations:needs: [changes]- if: needs.changes.outputs.backend == 'true' || needs.changes.outputs.migrations == 'true'+ # migration_replay is listed because it reaches files the others miss:+ # products/db_routing.yaml is neither Python nor under a backend/ directory, and+ # a migration in a future top-level app matches none of them. Without it the+ # replay gate could be on while the job that runs it never starts.+ if: needs.changes.outputs.backend == 'true' || needs.changes.outputs.migrations == 'true' || needs.changes.outputs.migration_replay == 'true'# On master this job replays the full migration history from scratch to- # produce the schema dump every PR job restores. That replay grows with- # every merged migration, so the limit needs headroom over it: it is- # there to cancel a hang, not to cap how long the history takes.- timeout-minutes: 40+ # produce the schema dump every PR job restores, and a merge queue batch that+ # changes a Django migration replays it a second time onto an empty database.+ # That replay grows with every merged migration, so the limit needs headroom+ # over two of them: it is there to cancel a hang, not to cap how long the+ # history takes.+ timeout-minutes: 60name: Validate migrationsruns-on: depot-ubuntu-24.04steps:@@ -1335,6 +1358,33 @@ jobs:# Then run persons migrations using sqlxDATABASE_URL="postgres://posthog:posthog@localhost:5432/posthog_persons" \sqlx migrate run --source rust/persons_migrations/+ - name: Replay the full migration history from scratch+ # The steps above prime the database from the cached master schema dump, so+ # the migrate that follows only applies the migrations this branch adds.+ # That hides a branch whose new migration moves an older one's position in+ # the plan, because the older one is already recorded as applied and never+ # runs against its new neighbours. A replay onto an empty database is the+ # only step that runs the whole history in the order this branch produces.+ # Master pushes already replay from scratch, which is where such a break+ # surfaces today, one merge too late.+ #+ # Queue lane only. The trunk-merge/** branch is master plus the batch, so it+ # sees the ordering every merged migration produces together; a PR branch+ # sees it only while it is current with master. The replay is by far the+ # most expensive part of this job, so paying it once per batch rather than+ # on every push of every migration branch is what keeps it affordable.+ if: github.event_name == 'pull_request' && startsWith(github.head_ref, 'trunk-merge/') && needs.changes.outputs.migration_replay == 'true'+ env:+ REPLAY_DB: posthog_migration_replay+ run: |+ set -euo pipefail+ docker compose -f docker-compose.dev.yml exec -T db psql -q -U posthog postgres \+ -c "DROP DATABASE IF EXISTS ${REPLAY_DB};" \+ -c "CREATE DATABASE ${REPLAY_DB};"+ if ! DATABASE_URL="postgres://posthog:posthog@localhost:5432/${REPLAY_DB}" python manage.py migrate; then+ echo "::error::The migration history does not apply to an empty database. A merge to master fails the same way. Read the traceback above for the migration that stopped, and check whether another migration this branch adds moved it in the plan."+ exit 1+ fi- name: Dump migrated schemaif: github.event_name == 'push'run: |diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.ymlindex 2d805f066df7..48ff8e84298f 100644--- a/.github/workflows/ci-backend.yml+++ b/.github/workflows/ci-backend.yml@@ -123,6 +123,10 @@ jobs:migrations: ${{ steps.filter.outputs.migrations || 'true' }}migrations_files: ${{ steps.filter.outputs.migrations_files }}migrations_any: ${{ steps.filter.outputs.migrations_any || 'true' }}+ # What the replay gate in check-migrations keys off. A ClickHouse or+ # async-migration change does not pay for a replay of a history the Django+ # planner never sees.+ migration_replay: ${{ steps.filter.outputs.migration_replay || 'true' }}persons_sql: ${{ steps.filter.outputs.persons_sql || 'true' }}tasks_temporal: ${{ steps.filter.outputs.tasks_temporal || 'true' }}openapi_types: ${{ steps.filter.outputs.openapi_types || 'true' }}@@ -336,6 +340,19 @@ jobs:# deleted-migration check in repo-checks.migrations_any:- '**/migrations/*.py'+ # Django migration files in any app, plus the routing files that decide+ # which app labels land in which database. Both change the plan a+ # from-scratch migrate builds. Match everything and exclude the two+ # non-Django directories, so a new app is covered without an edit here.+ # An app missed by a positive list would leave the replay gate silently+ # off for it.+ migration_replay:+ - '**/migrations/*.py'+ - '!posthog/clickhouse/migrations/*.py'+ - '!posthog/async_migrations/migrations/*.py'+ - 'products/db_routing.yaml'+ - 'posthog/product_db_config.py'+ - 'posthog/product_db_router.py'persons_sql:- 'rust/persons_migrations/*.sql'- 'rust/bin/migrate-persons'@@ -1433,12 +1450,18 @@ jobs:needs: [changes]# A per-commit check — every master push runs it (it saves the schema dump# caches), so the hourly scheduled run skips it as already covered.- if: (needs.changes.outputs.backend == 'true' || needs.changes.outputs.migrations == 'true' || needs.changes.outputs.persons_sql == 'true') && github.event_name != 'schedule'+ # migration_replay is listed because it reaches files the other three miss:+ # products/db_routing.yaml is neither Python nor under a backend/ directory, and+ # a migration in a future top-level app matches none of them. Without it the+ # replay gate could be on while the job that runs it never starts.+ if: (needs.changes.outputs.backend == 'true' || needs.changes.outputs.migrations == 'true' || needs.changes.outputs.persons_sql == 'true' || needs.changes.outputs.migration_replay == 'true') && github.event_name != 'schedule'# On master this job replays the full migration history from scratch to- # produce the schema dump every PR job restores. That replay grows with- # every merged migration, so the limit needs headroom over it: it is- # there to cancel a hang, not to cap how long the history takes.- timeout-minutes: 40+ # produce the schema dump every PR job restores, and a merge queue batch that+ # changes a Django migration replays it a second time onto an empty database.+ # That replay grows with every merged migration, so the limit needs headroom+ # over two of them: it is there to cancel a hang, not to cap how long the+ # history takes.+ timeout-minutes: 60outputs:mig_schema_key: ${{ steps.mig-schema-key.outputs.key }}# The "Publish Migration risk check" step posts the check via the@@ -1929,6 +1952,34 @@ jobs:DATABASE_URL="postgres://posthog:posthog@localhost:5432/posthog_persons" \sqlx migrate run --source rust/persons_migrations/+ - name: Replay the full migration history from scratch+ # The steps above prime the database from the cached master schema dump, so+ # the migrate that follows only applies the migrations this branch adds.+ # That hides a branch whose new migration moves an older one's position in+ # the plan, because the older one is already recorded as applied and never+ # runs against its new neighbours. A replay onto an empty database is the+ # only step that runs the whole history in the order this branch produces.+ # Master pushes already replay from scratch, which is where such a break+ # surfaces today, one merge too late.+ #+ # Queue lane only. The trunk-merge/** branch is master plus the batch, so it+ # sees the ordering every merged migration produces together; a PR branch+ # sees it only while it is current with master. The replay is by far the+ # most expensive part of this job, so paying it once per batch rather than+ # on every push of every migration branch is what keeps it affordable.+ if: github.event_name == 'pull_request' && startsWith(github.head_ref, 'trunk-merge/') && needs.changes.outputs.migration_replay == 'true'+ env:+ REPLAY_DB: posthog_migration_replay+ run: |+ set -euo pipefail+ docker compose -f docker-compose.dev.yml exec -T db psql -q -U posthog postgres \+ -c "DROP DATABASE IF EXISTS ${REPLAY_DB};" \+ -c "CREATE DATABASE ${REPLAY_DB};"+ if ! DATABASE_URL="postgres://posthog:posthog@localhost:5432/${REPLAY_DB}" python manage.py migrate; then+ echo "::error::The migration history does not apply to an empty database. A merge to master fails the same way. Read the traceback above for the migration that stopped, and check whether another migration this branch adds moved it in the plan."+ exit 1+ fi+- name: Dump migrated schemaif: github.event_name == 'push'run: |