: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(team-deletion): delete widget tiles before deleting the team (#89883)
Yasen7eced17f1684
diff --git a/posthog/models/team/util.py b/posthog/models/team/util.pyindex 4f76033397f3..d6fa5c3e02af 100644--- a/posthog/models/team/util.py+++ b/posthog/models/team/util.py@@ -12,6 +12,7 @@ from posthog.models.async_migration import is_async_migration_completefrom posthog.temporal.common.client import sync_connectfrom products.batch_exports.backend.service import BatchExportServiceScheduleNotFound, batch_export_delete_schedule+from products.dashboards.backend.models.dashboard_tile import DashboardTilelogger = structlog.get_logger(__name__)@@ -93,6 +94,9 @@ def _delete_misc_small_tables_for_teams(team_ids: list[int]) -> None:# DataWarehouseSavedQuery, which has PROTECT on delete._raw_delete_batch(Edge.objects.filter(team_id__in=team_ids))_raw_delete_batch(Node.objects.filter(team_id__in=team_ids))+ # DashboardTile has PROTECT on its widget, which the Team cascade deletes, so tiles go first.+ # _base_manager because the default manager hides soft-deleted tiles, which protect it too.+ _raw_delete_batch(DashboardTile._base_manager.filter(team_id__in=team_ids, widget__isnull=False))_raw_delete_batch(FileSystemViewLog.objects.filter(team_id__in=team_ids))_raw_delete_batch(EarlyAccessFeature.objects.filter(team_id__in=team_ids))_raw_delete_batch(error_tracking_fingerprint.objects.filter(team_id__in=team_ids))diff --git a/posthog/temporal/tests/delete_teams/test_protected_fk_predelete.py b/posthog/temporal/tests/delete_teams/test_protected_fk_predelete.pynew file mode 100644index 000000000000..0bad6e329fc5--- /dev/null+++ b/posthog/temporal/tests/delete_teams/test_protected_fk_predelete.py@@ -0,0 +1,40 @@+import pytest++from django.db.models.deletion import ProtectedError++from posthog.models.organization import Organization+from posthog.models.team import Team+from posthog.models.team.util import _delete_misc_small_tables_for_teams, delete_team_records++from products.dashboards.backend.models.dashboard import Dashboard+from products.dashboards.backend.models.dashboard_tile import DashboardTile+from products.dashboards.backend.models.dashboard_widget import DashboardWidget++# A plain django_db is enough: PROTECT raises while the cascade is being collected, not at COMMIT,+# so the rollback-based TestCase sees it.+pytestmark = [pytest.mark.django_db]+++def _team_with_widget_on_a_tile(tile_deleted: bool) -> int:+ _, _, team = Organization.objects.bootstrap(None)+ dashboard = Dashboard.objects.create(team=team, name="dashboard")+ widget = DashboardWidget.objects.for_team(team.id).create(team=team, widget_type="usage")+ DashboardTile.objects.create(dashboard=dashboard, widget=widget, deleted=tile_deleted or None)+ return team.id+++def test_team_cascade_is_blocked_by_a_widget_on_a_tile():+ team_id = _team_with_widget_on_a_tile(tile_deleted=False)++ with pytest.raises(ProtectedError):+ delete_team_records([team_id])++[email protected]("tile_deleted", [False, True])+def test_predelete_unblocks_team_deletion(tile_deleted: bool):+ team_id = _team_with_widget_on_a_tile(tile_deleted=tile_deleted)++ _delete_misc_small_tables_for_teams([team_id])+ delete_team_records([team_id])++ assert not Team.objects.filter(id=team_id).exists()