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.

chore(clickhouse-migrations): strengthen migration skill with safety rules (#62178)

Paweł Szczur135e33eb6823
diff --git a/.agents/skills/clickhouse-migrations/SKILL.md b/.agents/skills/clickhouse-migrations/SKILL.md
index 83abec0422d7..d540c464d538 100644
--- a/.agents/skills/clickhouse-migrations/SKILL.md
+++ b/.agents/skills/clickhouse-migrations/SKILL.md
@@ -49,6 +49,45 @@ Distributed engine:
- If a function generating SQL has on_cluster param, always set `on_cluster=False`
- Use `sharded=True` when altering sharded tables
- Use `is_alter_on_replicated_table=True` when altering non-sharded replicated tables
+- **Never write a `DROP COLUMN` migration yourself** — `DROP COLUMN` can get stuck in ClickHouse and block releases. Column removal is a two-step process: (1) the ClickHouse team drops the column directly on the cluster, then (2) you write a migration with the matching `DROP COLUMN` so the codebase schema stays in sync. Never initiate the drop from a migration without the ClickHouse team having done step 1 first.
+- **Never drop or recreate `kafka_events_json_ws` or `events_json_ws_mv`** — these tables are a no-go zone. The MV definition differs significantly between US prod, EU prod, and dev (dozens of environment-specific `mat_*` columns) and those differences are **not reflected in the repo**. Dropping and recreating from repo SQL would destroy the environment-specific schema and break event ingestion. Any change must go through the ClickHouse team.
+
+### PR scope
+
+A PR that contains a ClickHouse migration **must be migration-only**. Do not mix migration files with feature code, API changes, model changes, or frontend changes in the same PR. Migration-related files are:
+
+- The migration file itself (`posthog/clickhouse/migrations/0NNN_*.py`)
+- SQL definition files the migration depends on (e.g. `posthog/clickhouse/sql/*.py`, table engine helpers)
+- Tests that directly exercise the migration or the SQL definitions it touches
+
+If you need both a schema change and application code that uses the new schema, ship the migration first in its own PR and merge it before the application-code PR.
+
+### Local setup parity
+
+**No table should exist only in the cloud.** Every table created via migration must also exist in a local dev environment.
+
+Some migrations are cloud-guarded and skipped in local/hobby dev:
+
+```python
+operations = (
+ []
+ if settings.CLOUD_DEPLOYMENT not in ("US", "EU", "DEV")
+ else [...]
+)
+```
+
+If you create a new table inside such a guard, you must also add its SQL function to `posthog/clickhouse/schema.py` in the appropriate tuple so the table gets created locally:
+
+| Table type | Tuple in `schema.py` |
+| ---------------------- | ---------------------------------- |
+| MergeTree / base table | `CREATE_MERGETREE_TABLE_QUERIES` |
+| Distributed / writable | `CREATE_DISTRIBUTED_TABLE_QUERIES` |
+| Kafka consumer | `CREATE_KAFKA_TABLE_QUERIES` |
+| Materialized view | `CREATE_MV_TABLE_QUERIES` |
+| Non-materialized view | `CREATE_VIEW_QUERIES` |
+| Dictionary | `CREATE_DICTIONARY_QUERIES` |
+
+The only exception is tables whose definition intentionally differs per environment and is not tracked in the repo (e.g. the no-go zone `events_json_ws_mv` table).
### Testing
diff --git a/posthog/clickhouse/migrations/AGENTS.md b/posthog/clickhouse/migrations/AGENTS.md
index f84741b50e29..570c611de1c4 100644
--- a/posthog/clickhouse/migrations/AGENTS.md
+++ b/posthog/clickhouse/migrations/AGENTS.md
@@ -49,6 +49,36 @@ The way this is done:
If a destination table is non-sharded, we pick only one node as data for the Distributed table.
+# Local setup parity
+
+No table should exist only in the cloud. Every table created via migration must also exist
+in a local dev environment.
+
+Some migrations are cloud-guarded and skipped in local/hobby dev:
+
+```python
+operations = (
+ []
+ if settings.CLOUD_DEPLOYMENT not in ("US", "EU", "DEV")
+ else [...]
+)
+```
+
+If you create a new table inside such a guard, also add its SQL function to
+`posthog/clickhouse/schema.py` in the appropriate tuple so the table is created locally:
+
+| Table type | Tuple in `schema.py` |
+| ---------------------- | ---------------------------------- |
+| MergeTree / base table | `CREATE_MERGETREE_TABLE_QUERIES` |
+| Distributed / writable | `CREATE_DISTRIBUTED_TABLE_QUERIES` |
+| Kafka consumer | `CREATE_KAFKA_TABLE_QUERIES` |
+| Materialized view | `CREATE_MV_TABLE_QUERIES` |
+| Non-materialized view | `CREATE_VIEW_QUERIES` |
+| Dictionary | `CREATE_DICTIONARY_QUERIES` |
+
+The only exception is tables whose definition intentionally differs per environment and is
+not tracked in the repo (e.g. the no-go zone `events_json_ws_mv` table above).
+
# Migration basics
## run_sql_with_exceptions function
@@ -125,6 +155,15 @@ engine=Distributed(
> [!CAUTION]
> Do not use `ON CLUSTER` clause, it causes issues and is incompatible with our migration setup.
+> [!CAUTION]
+> Never write a `DROP COLUMN` migration on your own. `DROP COLUMN` can get stuck in ClickHouse
+> and block releases. Column removal follows a two-step process:
+>
+> 1. The ClickHouse team drops the column directly on the cluster.
+> 2. You write a migration with the matching `DROP COLUMN` to keep the codebase schema in sync.
+>
+> Do not initiate step 2 without confirmation that step 1 has been completed.
+
> [!INFO]
> Always use `IF EXISTS` or `IF NOT EXISTS` clauses:
>
@@ -133,8 +172,24 @@ engine=Distributed(
> `ALTER TABLE IF EXISTS`
>
> `ALTER TABLE IF EXISTS ADD COLUMN`
+
+> [!CAUTION]
+> Never drop or recreate `kafka_events_json_ws` or `events_json_ws_mv`. These tables are a
+> no-go zone. The MV definition differs between US prod, EU prod, and dev (dozens of
+> environment-specific `mat_*` materialized columns) and those differences are **not reflected
+> in the repo**. Dropping and recreating from repo SQL would destroy the environment-specific
+> schema and break event ingestion. Any change must go through the ClickHouse team.
+
+> [!INFO]
+> A PR containing a ClickHouse migration must be migration-only. Do not mix it with feature code,
+> API changes, model changes, or frontend changes. Migration-related files are:
+>
+> - The migration file itself (`posthog/clickhouse/migrations/0NNN_*.py`)
+> - SQL definition files the migration depends on (e.g. `posthog/clickhouse/sql/*.py`)
+> - Tests that directly exercise the migration or the SQL definitions it touches
>
-> `ALTER TABLE IF EXISTS DROP COLUMN`
+> If application code needs the new schema, ship the migration PR first and merge it before
+> the application-code PR.
# CREATE / DROP patterns