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(replay): drop two retired session summary tables (#92997)

posthog[bot]78f9ca1afe93
diff --git a/posthog/models/team/util.py b/posthog/models/team/util.py
index 75ecb3485357..089321e54ecd 100644
--- a/posthog/models/team/util.py
+++ b/posthog/models/team/util.py
@@ -39,17 +39,8 @@ TEAM_DELETE_BATCH_SIZE = 2000
# activity bound.
TEAM_DELETE_RPC_TIMEOUT_SECONDS = 30 * 60
-# The retired session-summary tables. products/replay/backend/migrations/0002_remove_session_summary_models.py
-# dropped their models from Django state only, so both the tables and their foreign keys on
-# posthog_team still exist in Postgres. Django's cascade cannot see them any more, and the
-# constraints are DEFERRABLE INITIALLY DEFERRED, so a leftover row fails the team delete at COMMIT
-# with an IntegrityError instead of at the DELETE statement. All three tables are dead: no Django
-# model reads or writes them. This list goes away with the migration that drops them.
-RETIRED_SESSION_SUMMARY_TABLES = (
- "ee_group_session_summary",
- "ee_single_session_summary",
- "ee_teamsessionsummariesconfig",
-)
+# Out of Django state since replay/0002, so the Team cascade cannot reach it. Delete with the table.
+RETIRED_SESSION_SUMMARY_TABLES = ("ee_single_session_summary",)
actions_that_require_current_team = [
"rotate_secret_token",
diff --git a/products/replay/backend/migrations/0005_drop_retired_session_summary_tables.py b/products/replay/backend/migrations/0005_drop_retired_session_summary_tables.py
new file mode 100644
index 000000000000..46b78e89586b
--- /dev/null
+++ b/products/replay/backend/migrations/0005_drop_retired_session_summary_tables.py
@@ -0,0 +1,38 @@
+from django.db import migrations
+
+from posthog.migration_helpers import DropForeignKey
+
+
+class Migration(migrations.Migration):
+ """Phase 2 of the two-phase drop for the retired session-summarization tables.
+
+ 0002 removed the models from Django state only. `ee_single_session_summary` stays because
+ nodejs/src/session-replay/recording-api/recording-service.ts still deletes from it, but its
+ last foreign key goes: Django cannot cascade into a table it no longer sees, so a leftover row
+ fails the team delete at COMMIT. 0004 already dropped the keys to posthog_user.
+
+ The drops stay in separate operations because the risk analyzer validates only the first
+ DROP TABLE per statement.
+ """
+
+ dependencies = [
+ ("replay", "0004_drop_orphaned_session_summary_user_fks"),
+ ]
+
+ operations = [
+ migrations.RunSQL(
+ sql="""
+ SET LOCAL lock_timeout = '5s';
+ DROP TABLE IF EXISTS "ee_group_session_summary";
+ """,
+ reverse_sql=migrations.RunSQL.noop,
+ ),
+ migrations.RunSQL(
+ sql="""
+ SET LOCAL lock_timeout = '5s';
+ DROP TABLE IF EXISTS "ee_teamsessionsummariesconfig";
+ """,
+ reverse_sql=migrations.RunSQL.noop,
+ ),
+ DropForeignKey("ee_single_session_summary", to_table="posthog_team"),
+ ]
diff --git a/products/replay/backend/migrations/max_migration.txt b/products/replay/backend/migrations/max_migration.txt
index 854ff4f1ef20..ca47449e18a8 100644
--- a/products/replay/backend/migrations/max_migration.txt
+++ b/products/replay/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0004_drop_orphaned_session_summary_user_fks
+0005_drop_retired_session_summary_tables