: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(devex): drop the retired tables and keys blocking tenant deletion (#99917)
Julian Beza5db0ea6c51a
diff --git a/posthog/migrations/1353_drop_retired_ducklake_tables.py b/posthog/migrations/1353_drop_retired_ducklake_tables.pynew file mode 100644index 000000000000..37c18d9581e0--- /dev/null+++ b/posthog/migrations/1353_drop_retired_ducklake_tables.py@@ -0,0 +1,38 @@+from django.db import migrations+++class Migration(migrations.Migration):+ """Drop the ducklake tables 1242 left behind.++ 1242 took DuckLakeCatalog and DuckLakeBackfill out of Django state and kept their tables, so+ old workers could still read them through a rolling deploy. The follow-up it names never+ landed. The tables are still here, still holding the connection details for a warehouse that+ moved onto DuckgresServer in 1241, and still carrying foreign keys to posthog_team,+ posthog_organization and posthog_user.++ Dropping only those keys would be worse than leaving them. A team or organization delete+ fails at COMMIT today, because the constraints are DEFERRABLE INITIALLY DEFERRED with NO+ ACTION and Django no longer cascades into a relation it cannot see. Take the keys away on+ their own and the delete succeeds while the row survives, so a deleted tenant leaves its+ database host, username, password and cross-account role behind with nothing pointing at+ them. The tables have to go with the keys.++ DROP TABLE takes ACCESS EXCLUSIVE on every table these foreign keys reference, and two of+ those are hot. The statement runs under a short lock_timeout so it fails fast and bin/migrate+ retries, rather than queueing that lock for the whole MIGRATE_LOCK_TIMEOUT window while every+ query arriving behind it waits.+ """++ dependencies = [+ ("posthog", "1352_email_lookup_indexes"),+ ]++ operations = [+ migrations.RunSQL(+ sql="""+ SET LOCAL lock_timeout = '2s';+ DROP TABLE IF EXISTS posthog_ducklakecatalog, posthog_ducklakebackfill;+ """,+ reverse_sql=migrations.RunSQL.noop,+ ),+ ]diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txtindex 7cbe83dbe09c..9e026dfc392d 100644--- a/posthog/migrations/max_migration.txt+++ b/posthog/migrations/max_migration.txt@@ -1 +1 @@-1352_email_lookup_indexes+1353_drop_retired_ducklake_tablesdiff --git a/products/endpoints/backend/migrations/0034_drop_orphaned_endpoint_saved_query_fk.py b/products/endpoints/backend/migrations/0034_drop_orphaned_endpoint_saved_query_fk.pynew file mode 100644index 000000000000..41704a4c9b47--- /dev/null+++ b/products/endpoints/backend/migrations/0034_drop_orphaned_endpoint_saved_query_fk.py@@ -0,0 +1,27 @@+from django.db import migrations++from posthog.migration_helpers import DropForeignKey+++class Migration(migrations.Migration):+ """Drop the foreign key on endpoints_endpoint.saved_query_id, which nothing owns any more.++ 0012 took `saved_query` out of Django state with database_operations=[], which left the+ column for rollback safety. That was the intent, and it is correct for a column. The+ foreign key to posthog_datawarehousesavedquery stayed with it, and no later migration+ removes it.++ A parent delete stops cascading into a relation Django cannot see. The constraint is+ DEFERRABLE INITIALLY DEFERRED with NO ACTION, so deleting a saved query finishes its+ cascade and Postgres then rejects the transaction at COMMIT.++ The column stays. Only the constraint goes.+ """++ dependencies = [+ ("endpoints", "0033_alter_endpointversion_saved_query"),+ ]++ operations = [+ DropForeignKey("endpoints_endpoint", column="saved_query_id"),+ ]diff --git a/products/endpoints/backend/migrations/max_migration.txt b/products/endpoints/backend/migrations/max_migration.txtindex ec353b94322e..c367bab5367e 100644--- a/products/endpoints/backend/migrations/max_migration.txt+++ b/products/endpoints/backend/migrations/max_migration.txt@@ -1 +1 @@-0033_alter_endpointversion_saved_query+0034_drop_orphaned_endpoint_saved_query_fkdiff --git a/products/managed_warehouse/backend/migrations/0007_drop_orphaned_duckgresserver_team_fk.py b/products/managed_warehouse/backend/migrations/0007_drop_orphaned_duckgresserver_team_fk.pynew file mode 100644index 000000000000..cb1048981ef0--- /dev/null+++ b/products/managed_warehouse/backend/migrations/0007_drop_orphaned_duckgresserver_team_fk.py@@ -0,0 +1,28 @@+from django.db import migrations++from posthog.migration_helpers import DropForeignKey+++class Migration(migrations.Migration):+ """Drop the foreign key on posthog_duckgresserver.team_id, which nothing owns any more.++ posthog/migrations/1242 took the `team` field out of Django state and left the column, so+ old releases could keep reading it. The foreign key to posthog_team stayed too, and no+ later migration removes it. The model has since moved to this app and scopes by+ organization instead, so no field names that column.++ A parent delete stops cascading into a relation Django cannot see. The constraint is+ DEFERRABLE INITIALLY DEFERRED with NO ACTION, so a team delete finishes its whole cascade+ and Postgres then rejects the transaction at COMMIT. The delete can never succeed while+ the team has rows here.++ The column stays. Only the constraint goes.+ """++ dependencies = [+ ("managed_warehouse", "0006_alter_managedwarehousesourcejob_created_by"),+ ]++ operations = [+ DropForeignKey("posthog_duckgresserver", column="team_id"),+ ]diff --git a/products/managed_warehouse/backend/migrations/max_migration.txt b/products/managed_warehouse/backend/migrations/max_migration.txtindex fd1b8dca9170..6bc11bbdfe54 100644--- a/products/managed_warehouse/backend/migrations/max_migration.txt+++ b/products/managed_warehouse/backend/migrations/max_migration.txt@@ -1 +1 @@-0006_alter_managedwarehousesourcejob_created_by+0007_drop_orphaned_duckgresserver_team_fkdiff --git a/products/mcp_store/backend/migrations/0032_drop_orphaned_mcpserver_fks.py b/products/mcp_store/backend/migrations/0032_drop_orphaned_mcpserver_fks.pynew file mode 100644index 000000000000..caac4e69aa8f--- /dev/null+++ b/products/mcp_store/backend/migrations/0032_drop_orphaned_mcpserver_fks.py@@ -0,0 +1,29 @@+from django.db import migrations++from posthog.migration_helpers import DropForeignKey+++class Migration(migrations.Migration):+ """Drop the two foreign keys pointing at mcp_store_mcpserver, which nothing owns any more.++ 0007 took the `server` fields out of Django state with database_operations=[], and 0008+ took the MCPServer model out of state as well. Both left their tables and columns in+ place. The foreign keys stayed, and no later migration removes them.++ 0008 reasoned that leaving the objects was harmless because nothing reads or writes them.+ That holds for the columns. It does not hold for the constraints: a foreign key fires when+ the parent row is deleted, not when anyone reads it. Nothing deletes these parent rows+ today, because MCPServer is out of Django state, so this is a trap rather than a live+ fault. Any later cleanup of mcp_store_mcpserver would spring it.++ The columns stay. Only the constraints go.+ """++ dependencies = [+ ("mcp_store", "0031_alter_mcpoauthstate_team_and_more"),+ ]++ operations = [+ DropForeignKey("mcp_store_mcpserverinstallation", column="server_id"),+ DropForeignKey("mcp_store_mcpoauthstate", column="server_id"),+ ]diff --git a/products/mcp_store/backend/migrations/max_migration.txt b/products/mcp_store/backend/migrations/max_migration.txtindex 76aa8a804802..8b2e857e2d8d 100644--- a/products/mcp_store/backend/migrations/max_migration.txt+++ b/products/mcp_store/backend/migrations/max_migration.txt@@ -1 +1 @@-0031_alter_mcpoauthstate_team_and_more+0032_drop_orphaned_mcpserver_fksdiff --git a/products/replay/backend/migrations/0004_drop_orphaned_session_summary_user_fks.py b/products/replay/backend/migrations/0004_drop_orphaned_session_summary_user_fks.pynew file mode 100644index 000000000000..39efabfa93fa--- /dev/null+++ b/products/replay/backend/migrations/0004_drop_orphaned_session_summary_user_fks.py@@ -0,0 +1,30 @@+from django.db import migrations++from posthog.migration_helpers import DropForeignKey+++class Migration(migrations.Migration):+ """Drop the created_by foreign keys on the retired session-summary tables.++ 0002 took the session-summarization models out of Django state and kept the tables,+ because their rows are the last copy of every generated summary. The table drop it names+ waits on the recording service in nodejs/, which still deletes from+ ee_single_session_summary, so it cannot land yet.++ Team deletion already works: _delete_retired_session_summaries_for_teams in+ posthog/models/team/util.py clears these rows before the Team cascade reaches them. That+ helper filters on team_id and runs on team deletion only, so the keys to posthog_user stay+ live. Deleting a user runs the whole cascade and Postgres then rejects the transaction at+ COMMIT, which is what an erasure request does.++ Only the two user keys go here. The team keys stay under the helper until the tables drop.+ """++ dependencies = [+ ("replay", "0003_drop_exception_event_ids_gin_index"),+ ]++ operations = [+ DropForeignKey("ee_single_session_summary", column="created_by_id"),+ DropForeignKey("ee_group_session_summary", column="created_by_id"),+ ]diff --git a/products/replay/backend/migrations/max_migration.txt b/products/replay/backend/migrations/max_migration.txtindex 6145e2760057..854ff4f1ef20 100644--- a/products/replay/backend/migrations/max_migration.txt+++ b/products/replay/backend/migrations/max_migration.txt@@ -1 +1 @@-0003_drop_exception_event_ids_gin_index+0004_drop_orphaned_session_summary_user_fksdiff --git a/products/tasks/backend/migrations/0124_drop_retired_code_home_tables.py b/products/tasks/backend/migrations/0124_drop_retired_code_home_tables.pynew file mode 100644index 000000000000..922be00ff4ad--- /dev/null+++ b/products/tasks/backend/migrations/0124_drop_retired_code_home_tables.py@@ -0,0 +1,38 @@+from django.db import migrations+++class Migration(migrations.Migration):+ """Drop the Code Home tables 0069 left behind.++ 0069 took CodeWorkstream, CodePrSnapshot and CodeWorkflowConfig out of Django state and kept+ their tables for one deploy cycle. It names a follow-up RunSQL migration to drop them that+ never landed, so the tables still hold repository paths, pull request snapshots and workflow+ configuration, and still carry foreign keys to posthog_team, posthog_user and+ posthog_integration.++ Dropping only those keys would be worse than leaving them. A team delete fails at COMMIT+ today, because the constraints are DEFERRABLE INITIALLY DEFERRED with NO ACTION and Django no+ longer cascades into a relation it cannot see. Take the keys away on their own and the delete+ succeeds while the rows survive, so a deleted tenant leaves its data behind with nothing+ pointing at it. The user keys are worse still, because the pre-delete cleanup in+ posthog/models/team/util.py is keyed on team_id and would never reach them.++ DROP TABLE takes ACCESS EXCLUSIVE on every table these foreign keys reference, and two of+ those are hot. The statement runs under a short lock_timeout so it fails fast and bin/migrate+ retries, rather than queueing that lock for the whole MIGRATE_LOCK_TIMEOUT window while every+ query arriving behind it waits.+ """++ dependencies = [+ ("tasks", "0123_alter_sandboxsnapshot_integration"),+ ]++ operations = [+ migrations.RunSQL(+ sql="""+ SET LOCAL lock_timeout = '2s';+ DROP TABLE IF EXISTS posthog_code_workstream, posthog_code_pr_snapshot, posthog_code_workflow_config;+ """,+ reverse_sql=migrations.RunSQL.noop,+ ),+ ]diff --git a/products/tasks/backend/migrations/max_migration.txt b/products/tasks/backend/migrations/max_migration.txtindex b8b362186932..1d9310d5e057 100644--- a/products/tasks/backend/migrations/max_migration.txt+++ b/products/tasks/backend/migrations/max_migration.txt@@ -1 +1 @@-0123_alter_sandboxsnapshot_integration+0124_drop_retired_code_home_tables