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(mcp-analytics): drop MCPSession backfill columns (#60397)

Georgis Andonis6c6782711649
diff --git a/products/mcp_analytics/backend/migrations/0008_drop_mcpsession_backfill_columns.py b/products/mcp_analytics/backend/migrations/0008_drop_mcpsession_backfill_columns.py
new file mode 100644
index 000000000000..ce70056c76eb
--- /dev/null
+++ b/products/mcp_analytics/backend/migrations/0008_drop_mcpsession_backfill_columns.py
@@ -0,0 +1,47 @@
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ # Phase 2 of a two-phase column drop (see safe-django-migrations.md "Dropping Columns").
+ # Migration 0007 removed these fields from Django's model state; this one performs the
+ # actual DROP COLUMN now that no deployed code references them. It is database-only —
+ # the model state is already correct, so there are no state operations and a plain
+ # RemoveField would fail (the fields no longer exist in state).
+ #
+ # Dropping session_end cascades the (team, -session_end) index, but we drop it
+ # explicitly first so the intent is obvious. DROP COLUMN takes a brief ACCESS EXCLUSIVE
+ # lock and is metadata-only (no table rewrite); the table is small and low-traffic.
+
+ dependencies = [
+ ("mcp_analytics", "0007_alter_mcpsession_options_and_more"),
+ ]
+
+ operations = [
+ migrations.RunSQL(
+ sql="""
+ DROP INDEX IF EXISTS posthog_mcp_team_id_b561f4_idx;
+ ALTER TABLE posthog_mcp_session
+ DROP COLUMN IF EXISTS session_start,
+ DROP COLUMN IF EXISTS session_end,
+ DROP COLUMN IF EXISTS duration_seconds,
+ DROP COLUMN IF EXISTS tools_used,
+ DROP COLUMN IF EXISTS tool_call_count,
+ DROP COLUMN IF EXISTS distinct_id,
+ DROP COLUMN IF EXISTS mcp_client_name;
+ """,
+ # Structural restore only — the dropped data is not recoverable. Re-adds the
+ # columns as they stood just before the drop (all nullable) and recreates the index.
+ reverse_sql="""
+ ALTER TABLE posthog_mcp_session
+ ADD COLUMN IF NOT EXISTS session_start timestamptz NULL,
+ ADD COLUMN IF NOT EXISTS session_end timestamptz NULL,
+ ADD COLUMN IF NOT EXISTS duration_seconds integer NULL,
+ ADD COLUMN IF NOT EXISTS tools_used varchar(200)[] NULL,
+ ADD COLUMN IF NOT EXISTS tool_call_count integer NULL,
+ ADD COLUMN IF NOT EXISTS distinct_id varchar(400) NULL,
+ ADD COLUMN IF NOT EXISTS mcp_client_name varchar(200) NULL;
+ CREATE INDEX IF NOT EXISTS posthog_mcp_team_id_b561f4_idx
+ ON posthog_mcp_session (team_id, session_end DESC);
+ """,
+ ),
+ ]
diff --git a/products/mcp_analytics/backend/migrations/max_migration.txt b/products/mcp_analytics/backend/migrations/max_migration.txt
index 0cce200ea386..09ecb97d2440 100644
--- a/products/mcp_analytics/backend/migrations/max_migration.txt
+++ b/products/mcp_analytics/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0007_alter_mcpsession_options_and_more
+0008_drop_mcpsession_backfill_columns