: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(teams): delete workflow batch jobs before the team row (#122098)
Yasen689b490a1266
diff --git a/posthog/models/team/util.py b/posthog/models/team/util.pyindex 2db55eba9cd4..6d3f70ee2d7f 100644--- a/posthog/models/team/util.py+++ b/posthog/models/team/util.py@@ -358,11 +358,15 @@ def delete_team_records(team_ids: list[int]) -> None:from posthog.models.team import Team+ from products.workflows.backend.facade.batch_jobs import delete_batch_jobs_for_teams+with transaction.atomic():# nosemgrep: hot-parent-row-select-for-update -- Team deletion must block concurrent child inserts.teams = list(Team.objects.select_for_update().filter(id__in=team_ids))for team in teams:queue_training_deletion(team.pk, "team")+ # The cascade skips batch jobs (DO_NOTHING), so delete them here, in the step a stuck deletion retries.+ delete_batch_jobs_for_teams(team_ids)Team.objects.filter(id__in=team_ids).delete()diff --git a/products/workflows/backend/facade/batch_jobs.py b/products/workflows/backend/facade/batch_jobs.pyindex b1a23c273ada..19d55c1c6840 100644--- a/products/workflows/backend/facade/batch_jobs.py+++ b/products/workflows/backend/facade/batch_jobs.py@@ -2,6 +2,7 @@from products.workflows.backend.services.batch_jobs import (create_batch_job,+ delete_batch_jobs_for_teams,get_batch_job,hog_flow_ids_with_broadcast_status,list_batch_jobs,@@ -10,6 +11,7 @@ from products.workflows.backend.services.batch_jobs import (__all__ = ["create_batch_job",+ "delete_batch_jobs_for_teams","get_batch_job","hog_flow_ids_with_broadcast_status","list_batch_jobs",diff --git a/products/workflows/backend/services/batch_jobs.py b/products/workflows/backend/services/batch_jobs.pyindex 07f261384a2d..d11bff76624c 100644--- a/products/workflows/backend/services/batch_jobs.py+++ b/products/workflows/backend/services/batch_jobs.py@@ -132,6 +132,12 @@ def hog_flow_ids_with_broadcast_status(*, team_id: int, statuses: Collection[strreturn list(queryset.filter(combined).values_list("id", flat=True))+def delete_batch_jobs_for_teams(team_ids: Collection[int], batch_size: int = 1000) -> None:+ jobs = HogFlowBatchJob.objects.filter(team_id__in=team_ids)+ while batch_ids := list(jobs.values_list("id", flat=True)[:batch_size]):+ jobs.filter(id__in=batch_ids).delete()++REALTIME_COHORT_RESUMES = Counter("workflows_realtime_cohort_resume_total","Batch runs woken after their realtime cohort finished calculating, by the CDP API's outcome.",diff --git a/products/workflows/backend/test/test_hog_flow_batch_job.py b/products/workflows/backend/test/test_hog_flow_batch_job.pyindex f4e4dc687cff..db9ddbd3bb8f 100644--- a/products/workflows/backend/test/test_hog_flow_batch_job.py+++ b/products/workflows/backend/test/test_hog_flow_batch_job.py@@ -4,6 +4,7 @@ from datetime import UTC, datetimeimport pytestfrom unittest.mock import MagicMock, patch+from django.db import connectionfrom django.db.models.signals import post_savefrom django.test import TestCase, override_settings@@ -11,6 +12,8 @@ import requestsfrom celery.exceptions import Retryfrom parameterized import parameterized+from posthog.models.team import Team+from posthog.models.team.util import delete_team_recordsfrom posthog.models.user import Userfrom products.cohorts.backend.facade.ready_signals import realtime_cohort_ready@@ -70,6 +73,18 @@ class TestHogFlowBatchJob(TestCase):assert str(batch_job) == f"HogFlow batch run {batch_job.id}"mock_create_invocation.assert_called_once()+ @patch(+ "products.workflows.backend.models.hog_flow_batch_job.hog_flow_batch_job.create_batch_hog_flow_job_invocation"+ )+ def test_batch_jobs_do_not_block_team_deletion(self, _mock_create_invocation):+ HogFlowBatchJob.objects.create(team=self.team, hog_flow=self.hog_flow)++ delete_team_records([self.team.id])+ # Deferred foreign keys fail at COMMIT, which a TestCase never reaches.+ connection.check_constraints()++ assert not Team.objects.filter(id=self.team.id).exists()+@parameterized.expand([("single_realtime_cohort", "realtime", None, True),