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.

fix(ci): key the schema cache on product db routing (#74020)

Raúl Negrón-Otero6ded1cb54201
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.yml
index 86a5e8bbe837..882b14cbb13c 100644
--- a/.depot/workflows/ci-backend.yml
+++ b/.depot/workflows/ci-backend.yml
@@ -114,7 +114,7 @@ env:
# (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, ci-dagster.yml, and ci-mcp.yml restore by the same key; keep their copies in sync when bumping.
- SCHEMA_CACHE_EPOCH: v1
+ SCHEMA_CACHE_EPOCH: v2
SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only
DATABASE_URL: 'postgres://posthog:posthog@localhost:5432/posthog'
REDIS_URL: 'redis://localhost'
@@ -499,6 +499,18 @@ jobs:
echo "::notice::merge-base not found (branch too stale?) — schema cache will be skipped"
exit 0
fi
+ # Routing decides which app labels reach the dump, and the consumers replay them
+ # from the checked-out copy, so these inputs belong in the key below. A PR that
+ # edits any of them matches no master dump, the merge-base one included: drop
+ # both keys and let migrations run from scratch.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' "$MERGE_BASE" -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ DB_ROUTING_HEAD=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ if [ "$DB_ROUTING" != "$DB_ROUTING_HEAD" ]; then
+ echo "key=" >> $GITHUB_OUTPUT
+ echo "migrations_key=" >> $GITHUB_OUTPUT
+ echo "::notice::db routing config or router differs from the merge base, so the schema cache is 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
@@ -516,7 +528,7 @@ jobs:
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)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | sha256sum | cut -c1-40)
echo "migrations_key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT
fi
# Shadow trial: canonical threads a setup-action app token into
@@ -1125,12 +1137,14 @@ jobs:
| grep -E '/migrations/[^/]+\.py$|^[0-9a-f]+ uv\.lock$' \
| grep -vE '/(clickhouse|async_migrations)/migrations/' \
| LC_ALL=C sort) || true
+ # Routing decides which app labels are in the dump, so these inputs belong in the key.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
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
else
- MIG_HASH=$(printf '%s\n%s' "$MIG_FILES" "$PG_IMAGE" | sha256sum | cut -c1-40)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | sha256sum | cut -c1-40)
echo "key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT
fi
- name: Restore master schema dump from cache
@@ -1230,13 +1244,15 @@ jobs:
| grep -E '/migrations/[^/]+\.py$|^[0-9a-f]+ uv\.lock$' \
| grep -vE '/(clickhouse|async_migrations)/migrations/' \
| LC_ALL=C sort) || true
+ # Routing decides which app labels are in the dump, so these inputs belong in the key.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
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)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | 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)
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index 717df7faf0da..52a85a62476e 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -40,7 +40,7 @@ env:
# (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, ci-dagster.yml, and ci-mcp.yml restore by the same key; keep their copies in sync when bumping.
- SCHEMA_CACHE_EPOCH: v1
+ SCHEMA_CACHE_EPOCH: v2
SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only
DATABASE_URL: 'postgres://posthog:posthog@localhost:5432/posthog'
REDIS_URL: 'redis://localhost'
@@ -407,6 +407,18 @@ jobs:
echo "::notice::merge-base not found (branch too stale?) — schema cache will be skipped"
exit 0
fi
+ # Routing decides which app labels reach the dump, and the consumers replay them
+ # from the checked-out copy, so these inputs belong in the key below. A PR that
+ # edits any of them matches no master dump, the merge-base one included: drop
+ # both keys and let migrations run from scratch.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' "$MERGE_BASE" -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ DB_ROUTING_HEAD=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ if [ "$DB_ROUTING" != "$DB_ROUTING_HEAD" ]; then
+ echo "key=" >> $GITHUB_OUTPUT
+ echo "migrations_key=" >> $GITHUB_OUTPUT
+ echo "::notice::db routing config or router differs from the merge base, so the schema cache is 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
@@ -424,7 +436,7 @@ jobs:
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)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | sha256sum | cut -c1-40)
echo "migrations_key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT
fi
@@ -1424,12 +1436,14 @@ jobs:
| grep -E '/migrations/[^/]+\.py$|^[0-9a-f]+ uv\.lock$' \
| grep -vE '/(clickhouse|async_migrations)/migrations/' \
| LC_ALL=C sort) || true
+ # Routing decides which app labels are in the dump, so these inputs belong in the key.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
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
else
- MIG_HASH=$(printf '%s\n%s' "$MIG_FILES" "$PG_IMAGE" | sha256sum | cut -c1-40)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | sha256sum | cut -c1-40)
echo "key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT
fi
@@ -1839,13 +1853,15 @@ jobs:
| grep -E '/migrations/[^/]+\.py$|^[0-9a-f]+ uv\.lock$' \
| grep -vE '/(clickhouse|async_migrations)/migrations/' \
| LC_ALL=C sort) || true
+ # Routing decides which app labels are in the dump, so these inputs belong in the key.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
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)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | sha256sum | cut -c1-40)
echo "key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT
fi
diff --git a/.github/workflows/ci-dagster.yml b/.github/workflows/ci-dagster.yml
index 8bff9b8892dc..3debf6db8da1 100644
--- a/.github/workflows/ci-dagster.yml
+++ b/.github/workflows/ci-dagster.yml
@@ -16,7 +16,7 @@ permissions:
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
+ SCHEMA_CACHE_EPOCH: v2
SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only
DATABASE_URL: 'postgres://posthog:posthog@localhost:5432/posthog'
REDIS_URL: 'redis://localhost'
@@ -248,6 +248,18 @@ jobs:
echo "::notice::merge-base not found (branch too stale?) — schema cache will be skipped"
exit 0
fi
+ # Routing decides which app labels reach the dump, and the consumers replay them
+ # from the checked-out copy, so these inputs belong in the key below. A PR that
+ # edits any of them matches no master dump, the merge-base one included: drop
+ # both keys and let migrations run from scratch.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' "$MERGE_BASE" -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ DB_ROUTING_HEAD=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ if [ "$DB_ROUTING" != "$DB_ROUTING_HEAD" ]; then
+ echo "key=" >> $GITHUB_OUTPUT
+ echo "migrations_key=" >> $GITHUB_OUTPUT
+ echo "::notice::db routing config or router differs from the merge base, so the schema cache is 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" \
@@ -260,7 +272,7 @@ jobs:
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)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | sha256sum | cut -c1-40)
echo "migrations_key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT
fi
diff --git a/.github/workflows/ci-e2e-playwright.yml b/.github/workflows/ci-e2e-playwright.yml
index 053f0a395d22..cc0ea7d695d8 100644
--- a/.github/workflows/ci-e2e-playwright.yml
+++ b/.github/workflows/ci-e2e-playwright.yml
@@ -280,7 +280,7 @@ jobs:
IS_PUSH: ${{ github.event_name == 'push' }}
# 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
+ SCHEMA_CACHE_EPOCH: v2
run: |
if [ "$IS_PUSH" = "true" ]; then
# Pushed commit is checked out directly — no merge commit, no base ref.
@@ -296,6 +296,17 @@ jobs:
echo "::notice::merge-base not found (branch too stale?) — schema cache will be skipped"
exit 0
fi
+ # The consumers replay the routed labels from the checked-out copy, so a PR
+ # that edits any of them matches no master dump, the merge-base one included:
+ # drop both keys and let migrations run from scratch.
+ DB_ROUTING_BASE=$(git ls-tree -r --format='%(objectname) %(path)' "$MERGE_BASE" -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ DB_ROUTING_HEAD=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ if [ "$DB_ROUTING_BASE" != "$DB_ROUTING_HEAD" ]; then
+ echo "key=" >> $GITHUB_OUTPUT
+ echo "migrations_key=" >> $GITHUB_OUTPUT
+ echo "::notice::db routing config or router differs from the merge base, so the schema cache is skipped"
+ exit 0
+ fi
echo "key=posthog-schema-master-${MERGE_BASE}" >> $GITHUB_OUTPUT
REF="$MERGE_BASE"
fi
@@ -308,13 +319,15 @@ jobs:
| grep -E '/migrations/[^/]+\.py$|^[0-9a-f]+ uv\.lock$' \
| grep -vE '/(clickhouse|async_migrations)/migrations/' \
| LC_ALL=C sort) || true
+ # Routing decides which app labels are in the dump, so these inputs belong in the key.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' "$REF" -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
PG_IMAGE=$(git show "${REF}: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)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | sha256sum | cut -c1-40)
echo "migrations_key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT
fi
diff --git a/.github/workflows/ci-mcp.yml b/.github/workflows/ci-mcp.yml
index c10d6b6cf6bb..3c4e5df0cf88 100644
--- a/.github/workflows/ci-mcp.yml
+++ b/.github/workflows/ci-mcp.yml
@@ -21,7 +21,7 @@ permissions:
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
+ SCHEMA_CACHE_EPOCH: v2
RUNS_ON_INTERNAL_PR: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false }}
jobs:
@@ -477,6 +477,18 @@ jobs:
echo "::notice::merge-base not found (branch too stale?) — schema cache will be skipped"
exit 0
fi
+ # Routing decides which app labels reach the dump, and the prime step below
+ # replays them from the checked-out copy, so these inputs belong in the key. A PR
+ # that edits any of them matches no master dump, the merge-base one included:
+ # drop both keys and let migrations run from scratch.
+ DB_ROUTING=$(git ls-tree -r --format='%(objectname) %(path)' "$MERGE_BASE" -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ DB_ROUTING_HEAD=$(git ls-tree -r --format='%(objectname) %(path)' HEAD -- products/db_routing.yaml posthog/product_db_config.py posthog/product_db_router.py)
+ if [ "$DB_ROUTING" != "$DB_ROUTING_HEAD" ]; then
+ echo "key=" >> $GITHUB_OUTPUT
+ echo "migrations_key=" >> $GITHUB_OUTPUT
+ echo "::notice::db routing config or router differs from the merge base, so the schema cache is 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" \
@@ -489,7 +501,7 @@ jobs:
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)
+ MIG_HASH=$(printf '%s\n%s\n%s' "$MIG_FILES" "$PG_IMAGE" "$DB_ROUTING" | sha256sum | cut -c1-40)
echo "migrations_key=posthog-schema-mig-${SCHEMA_CACHE_EPOCH}-${MIG_HASH}" >> $GITHUB_OUTPUT
fi