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): port migration-set schema cache key to ci-dagster and ci-mcp (#74011)

Raúl Negrón-Otero83c16338d688
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.yml
index 43a14e3d1eeb..43fbcbbbd0b1 100644
--- a/.depot/workflows/ci-backend.yml
+++ b/.depot/workflows/ci-backend.yml
@@ -110,7 +110,7 @@ env:
# Schema cache epoch. Bump to abandon every shared schema cache entry at once
# (key is posthog-schema-mig-<epoch>-<hash>). Single-sourced so the merge-base (PR)
# and HEAD (push) key computations below can't drift.
- # ci-e2e-playwright.yml restores by the same key — keep its copy in sync when bumping.
+ # ci-e2e-playwright.yml, ci-dagster.yml, and ci-mcp.yml restore by the same key; keep their copies in sync when bumping.
SCHEMA_CACHE_EPOCH: v1
SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only
DATABASE_URL: 'postgres://posthog:posthog@localhost:5432/posthog'
@@ -1199,7 +1199,7 @@ jobs:
# Verify the dump is valid
gunzip -t schema.sql.gz
- name: Save schema to Actions cache for PR shards
- # Kept for fallback and for other workflows that still restore by merge-base SHA.
+ # Kept as the exact merge-base key that every consumer's restore-keys falls back to.
if: github.event_name == 'push'
uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
with:
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index 32702aafd302..25cb21ca3394 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -37,7 +37,7 @@ 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.
- # ci-e2e-playwright.yml restores by the same key — keep its copy in sync when bumping.
+ # ci-e2e-playwright.yml, ci-dagster.yml, and ci-mcp.yml restore by the same key; keep their copies in sync when bumping.
SCHEMA_CACHE_EPOCH: v1
SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only
DATABASE_URL: 'postgres://posthog:posthog@localhost:5432/posthog'
@@ -1803,7 +1803,7 @@ jobs:
retention-days: 90
- name: Save schema to Actions cache for PR shards
- # Kept for fallback and for other workflows that still restore by merge-base SHA.
+ # Kept as the exact merge-base key that every consumer's restore-keys falls back to.
if: github.event_name == 'push'
uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
with:
diff --git a/.github/workflows/ci-dagster.yml b/.github/workflows/ci-dagster.yml
index 5c323490394d..4a72b6f42c23 100644
--- a/.github/workflows/ci-dagster.yml
+++ b/.github/workflows/ci-dagster.yml
@@ -15,6 +15,8 @@ permissions:
pull-requests: read
env:
+ # Manual cache-bust knob. Keep in sync with SCHEMA_CACHE_EPOCH in ci-backend.yml, which saves these caches on master pushes.
+ SCHEMA_CACHE_EPOCH: v1
SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only
DATABASE_URL: 'postgres://posthog:posthog@localhost:5432/posthog'
REDIS_URL: 'redis://localhost'
@@ -64,6 +66,7 @@ jobs:
oldest_supported: ${{ steps.read-versions.outputs.oldest_supported }}
matrix_include: ${{ steps.build-matrix.outputs.include || '[]' }}
schema_cache_key: ${{ steps.schema-key.outputs.key }}
+ schema_migrations_key: ${{ steps.schema-key.outputs.migrations_key }}
steps:
# For pull requests it's not necessary to checkout the code, but we
# also want this to run on master so we need to checkout.
@@ -235,11 +238,26 @@ jobs:
run: |
# HEAD is the synthetic merge commit; HEAD^2 is the PR branch tip.
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" ]; then
echo "key=" >> $GITHUB_OUTPUT
+ echo "migrations_key=" >> $GITHUB_OUTPUT
echo "::notice::merge-base not found (branch too stale?) — schema cache will be skipped"
+ exit 0
+ fi
+ echo "key=posthog-schema-master-${MERGE_BASE}" >> $GITHUB_OUTPUT
+ # Shared migration-set key. Must match ci-backend.yml's save-side computation byte for byte or it never hits.
+ 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_OUTPUT
fi
dagster:
@@ -345,7 +363,10 @@ jobs:
id: schema-cache
with:
path: schema.sql.gz
- key: ${{ needs.changes.outputs.schema_cache_key }}
+ # Prefer the shared content key; fall back to the exact merge-base SHA key.
+ key: ${{ needs.changes.outputs.schema_migrations_key || needs.changes.outputs.schema_cache_key }}
+ restore-keys: |
+ ${{ needs.changes.outputs.schema_cache_key }}
- name: Prime test_posthog and posthog from cached schema (PR)
# Primes both test_posthog (used by pytest --reuse-db) and the prod-style
diff --git a/.github/workflows/ci-mcp.yml b/.github/workflows/ci-mcp.yml
index 5597d17390af..bb97913692f0 100644
--- a/.github/workflows/ci-mcp.yml
+++ b/.github/workflows/ci-mcp.yml
@@ -20,6 +20,8 @@ permissions:
pull-requests: read
env:
+ # Manual cache-bust knob. Keep in sync with SCHEMA_CACHE_EPOCH in ci-backend.yml, which saves these caches on master pushes.
+ SCHEMA_CACHE_EPOCH: v1
RUNS_ON_INTERNAL_PR: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false }}
jobs:
@@ -414,11 +416,26 @@ 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" ]; then
echo "key=" >> $GITHUB_OUTPUT
+ echo "migrations_key=" >> $GITHUB_OUTPUT
echo "::notice::merge-base not found (branch too stale?) — schema cache will be skipped"
+ exit 0
+ fi
+ echo "key=posthog-schema-master-${MERGE_BASE}" >> $GITHUB_OUTPUT
+ # Shared migration-set key. Must match ci-backend.yml's save-side computation byte for byte or it never hits.
+ 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_OUTPUT
fi
- name: Restore schema cache from master
@@ -426,7 +443,10 @@ jobs:
uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
with:
path: schema.sql.gz
- key: ${{ steps.schema-key.outputs.key }}
+ # Prefer the shared content key; fall back to the exact merge-base SHA key.
+ key: ${{ steps.schema-key.outputs.migrations_key || steps.schema-key.outputs.key }}
+ restore-keys: |
+ ${{ steps.schema-key.outputs.key }}
- name: Prime posthog from cached schema
# No-op on cache miss; manage.py migrate below falls back to a full migrate.