: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
perf(ci): share schema cache by migration-set key (#65887)
Raúl Negrón-Oterofb69ae445423
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.ymlindex 4cceea24c9d2..148de2cf5a3e 100644--- a/.github/workflows/ci-backend.yml+++ b/.github/workflows/ci-backend.yml@@ -29,6 +29,10 @@ concurrency:cancel-in-progress: ${{ github.event_name == 'pull_request' }}env:+ # Schema cache epoch. Bump to abandon every shared schema cache entry at once+ # (key is posthog-schema-mig-<epoch>-<hash>). Single-sourced so it can't drift+ # between the merge-base (PR) and HEAD (push) key computations below.+ SCHEMA_CACHE_EPOCH: v1DOCKERHUB_USERNAME: ${{ vars.DOCKERHUB_USER }}DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only@@ -373,6 +377,7 @@ jobs:run_legacy: ${{ steps.discover.outputs.run_legacy }}matrix: ${{ steps.discover.outputs.matrix }}schema_cache_key: ${{ steps.schema-key.outputs.key }}+ schema_migrations_key: ${{ steps.schema-key.outputs.migrations_key }}django_shards: ${{ steps.discover.outputs.django_shards }}steps:@@ -397,11 +402,31 @@ jobs:# The fetch-depth:1000 checkout + base-ref fetch above ensure the# full ancestry needed to find the divergence point is available.MERGE_BASE=$(git merge-base HEAD^2 "origin/${BASE_REF}" 2>/dev/null || echo "")- if [ -n "$MERGE_BASE" ]; then- echo "key=posthog-schema-master-${MERGE_BASE}" >> $GITHUB_OUTPUT- else+ if [ -z "$MERGE_BASE" ]; thenecho "key=" >> $GITHUB_OUTPUT+ echo "migrations_key=" >> $GITHUB_OUTPUTecho "::notice::merge-base not found (branch too stale?) — schema cache will be skipped"+ exit 0+ fi+ # Exact branch-point key, kept as the fallback restore key below.+ echo "key=posthog-schema-master-${MERGE_BASE}" >> $GITHUB_OUTPUT+ # Shared key: hash merge-base schema inputs, not PR head, so PR migrations still+ # apply as the pytest delta. uv.lock covers third-party migration changes; the+ # postgres image is folded in so a server-version bump invalidates the key.+ # Non-Django migration frameworks are excluded from this Postgres schema dump.+ # SCHEMA_CACHE_EPOCH (workflow env) prefixes the key as a manual cache-bust knob.+ MIG_FILES=$(git ls-tree -r --format='%(objectname) %(path)' "$MERGE_BASE" \+ | grep -E '/migrations/[^/]+\.py$|^[0-9a-f]+ uv\.lock$' \+ | grep -vE '/(clickhouse|async_migrations)/migrations/' \+ | LC_ALL=C sort) || true+ PG_IMAGE=$(git show "${MERGE_BASE}:docker-compose.base.yml" 2>/dev/null \+ | grep -m1 -E '^[[:space:]]*image:[[:space:]]*postgres:' | tr -d '[:space:]')+ if [ -z "$MIG_FILES" ]; then+ echo "migrations_key=" >> $GITHUB_OUTPUT+ echo "::notice::no migration files matched — restore will fall back to the SHA schema key"+ else+ MIG_HASH=$(printf '%s\n%s' "$MIG_FILES" "$PG_IMAGE" | sha256sum | cut -c1-40)+ echo "migrations_key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUTfi- name: Mint setup-action GitHub token@@ -732,10 +757,13 @@ jobs:uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3with:path: schema.sql.gz- key: ${{ needs.turbo-discover.outputs.schema_cache_key }}+ # Prefer the shared content key; fall back to the exact merge-base SHA key.+ key: ${{ needs.turbo-discover.outputs.schema_migrations_key || needs.turbo-discover.outputs.schema_cache_key }}+ 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; db:restore-test-db recreates the DB after failure.if: ${{ github.event_name == 'pull_request' }}run: |if [ ! -f schema.sql.gz ]; then@@ -744,7 +772,9 @@ jobs:fimkdir -p .postgres-backupsmv schema.sql.gz .postgres-backups/schema-latest.sql.gz- ./bin/hogli db:restore-test-db+ 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"+ fi- name: Register Temporal search attributes# Some product groups (data_warehouse, batch_exports) connect to a live Temporal@@ -1461,16 +1491,39 @@ jobs:retention-days: 90- name: Save schema to Actions cache for PR shards- # Seeds a Postgres schema cache consumed by PR test jobs (turbo-tests,- # django, dagster). PR jobs restore using the merge-base SHA as the key- # so they always get a schema from their exact branch point rather than- # the newest master. LRU eviction handles cleanup.+ # Kept for fallback and for other workflows that still restore by merge-base SHA.if: github.event_name == 'push'uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3with:path: schema.sql.gzkey: posthog-schema-master-${{ github.sha }}+ - name: Compute migration-set schema key+ # Must match the merge-base computation in turbo-discover (inputs, hash, and key prefix).+ if: github.event_name == 'push'+ id: mig-schema-key+ run: |+ MIG_FILES=$(git ls-tree -r --format='%(objectname) %(path)' HEAD \+ | grep -E '/migrations/[^/]+\.py$|^[0-9a-f]+ uv\.lock$' \+ | grep -vE '/(clickhouse|async_migrations)/migrations/' \+ | LC_ALL=C sort) || true+ PG_IMAGE=$(git show "HEAD:docker-compose.base.yml" 2>/dev/null \+ | grep -m1 -E '^[[:space:]]*image:[[:space:]]*postgres:' | tr -d '[:space:]')+ if [ -z "$MIG_FILES" ]; then+ echo "key=" >> $GITHUB_OUTPUT+ echo "::notice::no migration files matched — skipping migration-set schema save"+ else+ MIG_HASH=$(printf '%s\n%s' "$MIG_FILES" "$PG_IMAGE" | sha256sum | cut -c1-40)+ echo "key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT+ fi++ - name: Save schema to Actions cache (migration-set key)+ if: github.event_name == 'push' && steps.mig-schema-key.outputs.key != ''+ uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3+ with:+ path: schema.sql.gz+ key: ${{ steps.mig-schema-key.outputs.key }}+- name: Check migrations# Skip migration safety check on master push (no migration_files from path filter)if: github.event_name != 'push'@@ -2200,10 +2253,13 @@ jobs:uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3with:path: schema.sql.gz- key: ${{ needs.turbo-discover.outputs.schema_cache_key }}+ # Prefer the shared content key; fall back to the exact merge-base SHA key.+ key: ${{ needs.turbo-discover.outputs.schema_migrations_key || needs.turbo-discover.outputs.schema_cache_key }}+ 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; db:restore-test-db recreates the DB after failure.if: ${{ github.event_name == 'pull_request' }}run: |if [ ! -f schema.sql.gz ]; then@@ -2212,7 +2268,9 @@ jobs:fimkdir -p .postgres-backupsmv schema.sql.gz .postgres-backups/schema-latest.sql.gz- ./bin/hogli db:restore-test-db+ 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"+ fi- name: Restore test durations from cache# pytest-split distributes Core/Temporal tests across shards using@@ -2957,10 +3015,13 @@ jobs:uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3with:path: schema.sql.gz- key: ${{ needs.turbo-discover.outputs.schema_cache_key }}+ # Prefer the shared content key; fall back to the exact merge-base SHA key.+ key: ${{ needs.turbo-discover.outputs.schema_migrations_key || needs.turbo-discover.outputs.schema_cache_key }}+ 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; db:restore-test-db recreates the DB after failure.if: ${{ github.event_name == 'pull_request' }}run: |if [ ! -f schema.sql.gz ]; then@@ -2969,7 +3030,9 @@ jobs:fimkdir -p .postgres-backupsmv schema.sql.gz .postgres-backups/schema-latest.sql.gz- ./bin/hogli db:restore-test-db+ 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"+ fi- name: Run async migrations testsrun: |