: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
feat(data-deletion): link the last dagster run from deletion requests (#70638)
Paweł Szczur1507f7773edd
diff --git a/posthog/admin/admins/data_deletion_request_admin.py b/posthog/admin/admins/data_deletion_request_admin.pyindex d243436e4f6d..87a55e91283b 100644--- a/posthog/admin/admins/data_deletion_request_admin.py+++ b/posthog/admin/admins/data_deletion_request_admin.py@@ -1,6 +1,7 @@import astfrom django import forms+from django.conf import settingsfrom django.contrib import admin, messagesfrom django.http import HttpResponseRedirectfrom django.template.response import TemplateResponse@@ -67,6 +68,20 @@ EDITABLE_FIELDS = ("person_drop_recordings",)+# Dagster Cloud deployment slug per PostHog cloud environment. The admin runs on web pods, which+# don't carry DAGSTER_DOMAIN (only Dagster pods do), so the run URL is derived from CLOUD_DEPLOYMENT.+DAGSTER_DEPLOYMENT_BY_CLOUD = {"US": "prod-us", "EU": "prod-eu", "DEV": "dev"}+++def dagster_run_url(run_id: str) -> str | None:+ """Link to a Dagster run, or None when the deployment can't be identified (local/self-hosted)."""+ if settings.DAGSTER_DOMAIN:+ return f"https://{settings.DAGSTER_DOMAIN}/runs/{run_id}"+ deployment = DAGSTER_DEPLOYMENT_BY_CLOUD.get((settings.CLOUD_DEPLOYMENT or "").upper())+ if deployment:+ return f"https://posthog.dagster.cloud/{deployment}/runs/{run_id}"+ return None+# ---------------------------------------------------------------------------# Custom widget + field for ArrayField editing@@ -453,6 +468,7 @@ class DataDeletionRequestAdmin(admin.ModelAdmin):"attempt_count","first_executed_at","last_executed_at",+ "last_dagster_run","rendered_count_query",)ordering = ("-created_at",)@@ -524,6 +540,7 @@ class DataDeletionRequestAdmin(admin.ModelAdmin):"attempt_count","first_executed_at","last_executed_at",+ "last_dagster_run",),},),@@ -557,6 +574,16 @@ class DataDeletionRequestAdmin(admin.ModelAdmin):created += 1messages.success(request, f"Duplicated {created} request(s) as new draft(s).")+ @admin.display(description="Last Dagster run")+ def last_dagster_run(self, obj: DataDeletionRequest) -> str:+ """Link to the Dagster run of the latest execution attempt, for debugging in-flight requests."""+ if not obj.last_dagster_run_id:+ return "—"+ url = dagster_run_url(obj.last_dagster_run_id)+ if url is None:+ return obj.last_dagster_run_id+ return format_html('<a href="{}" target="_blank" rel="noopener">{}</a>', url, obj.last_dagster_run_id)+@admin.display(description="Count query (ready to paste)")def rendered_count_query(self, obj: DataDeletionRequest) -> str:"""Show the fully-substituted ClickHouse COUNT query operators can copy/paste."""diff --git a/posthog/admin/test_data_deletion_request_admin.py b/posthog/admin/test_data_deletion_request_admin.pyindex 1126100b53ce..00f07584ad8b 100644--- a/posthog/admin/test_data_deletion_request_admin.py+++ b/posthog/admin/test_data_deletion_request_admin.py@@ -8,12 +8,12 @@ from django.contrib.admin.sites import AdminSitefrom django.contrib.auth.models import Groupfrom django.contrib.messages import get_messagesfrom django.contrib.messages.storage.fallback import FallbackStorage-from django.test import RequestFactory, override_settings+from django.test import RequestFactory, SimpleTestCase, override_settingsfrom django.utils import timezonefrom parameterized import parameterized-from posthog.admin.admins.data_deletion_request_admin import EDITABLE_FIELDS, DataDeletionRequestAdmin+from posthog.admin.admins.data_deletion_request_admin import EDITABLE_FIELDS, DataDeletionRequestAdmin, dagster_run_urlfrom posthog.models.data_deletion_request import DataDeletionRequest, ExecutionMode, RequestStatus, RequestType@@ -883,3 +883,38 @@ class TestDataDeletionRequestAdminDuplicate(BaseTest):else:# No original notes — the copy note stands alone, no trailing separator.self.assertFalse(copy.notes.endswith("\n"))+++class TestDagsterRunLink(SimpleTestCase):+ RUN_ID = "f5f3a611-4be8-48bc-9b29-ff8b06d01189"++ @parameterized.expand(+ [+ ("cloud_us", None, "US", f"https://posthog.dagster.cloud/prod-us/runs/{RUN_ID}"),+ ("cloud_eu", None, "EU", f"https://posthog.dagster.cloud/prod-eu/runs/{RUN_ID}"),+ ("dev", None, "DEV", f"https://posthog.dagster.cloud/dev/runs/{RUN_ID}"),+ ("lowercase_deployment", None, "eu", f"https://posthog.dagster.cloud/prod-eu/runs/{RUN_ID}"),+ ("domain_takes_precedence", "dagster.example.com", "EU", f"https://dagster.example.com/runs/{RUN_ID}"),+ ("local", None, None, None),+ ("unknown_deployment", None, "E2E", None),+ ]+ )+ def test_dagster_run_url(self, _name, dagster_domain, cloud_deployment, expected):+ with override_settings(DAGSTER_DOMAIN=dagster_domain, CLOUD_DEPLOYMENT=cloud_deployment):+ self.assertEqual(dagster_run_url(self.RUN_ID), expected)++ @override_settings(DAGSTER_DOMAIN=None, CLOUD_DEPLOYMENT="EU")+ def test_admin_renders_run_id_as_link(self):+ admin_obj = DataDeletionRequestAdmin(DataDeletionRequest, AdminSite())+ rendered = admin_obj.last_dagster_run(DataDeletionRequest(last_dagster_run_id=self.RUN_ID))+ self.assertIn(f'href="https://posthog.dagster.cloud/prod-eu/runs/{self.RUN_ID}"', rendered)++ @override_settings(DAGSTER_DOMAIN=None, CLOUD_DEPLOYMENT=None)+ def test_admin_renders_bare_run_id_when_deployment_unknown(self):+ admin_obj = DataDeletionRequestAdmin(DataDeletionRequest, AdminSite())+ rendered = admin_obj.last_dagster_run(DataDeletionRequest(last_dagster_run_id=self.RUN_ID))+ self.assertEqual(rendered, self.RUN_ID)++ def test_admin_renders_placeholder_when_never_executed(self):+ admin_obj = DataDeletionRequestAdmin(DataDeletionRequest, AdminSite())+ self.assertEqual(admin_obj.last_dagster_run(DataDeletionRequest()), "—")diff --git a/posthog/dags/data_deletion_requests.py b/posthog/dags/data_deletion_requests.pyindex 986112a76e3a..9dac7d3e88b5 100644--- a/posthog/dags/data_deletion_requests.py+++ b/posthog/dags/data_deletion_requests.py@@ -88,13 +88,15 @@ class PersonRemovalContext:# ----------------------------------------------------------------------------def _record_execution_attempt(request: DataDeletionRequest) -> None:+def _record_execution_attempt(request: DataDeletionRequest, run_id: str) -> None:"""Mark the request IN_PROGRESS and update execution-tracking fields.Called from inside the ``select_for_update`` block of each ``load_*`` op so- the counter and timestamps are bumped exactly once per APPROVED → IN_PROGRESS- transition. ``first_executed_at`` is preserved across retries; ``attempt_count``- counts every actual execution attempt (not Retry button clicks).+ the counter, timestamps and Dagster run id are set exactly once per+ APPROVED → IN_PROGRESS transition. ``first_executed_at`` is preserved across+ retries; ``attempt_count`` counts every actual execution attempt (not Retry+ button clicks). ``last_dagster_run_id`` always points at the newest run, so an+ operator debugging a stuck or failed request can jump straight to its logs."""from django.utils import timezone@@ -102,7 +104,8 @@ def _record_execution_attempt(request: DataDeletionRequest) -> None:request.status = RequestStatus.IN_PROGRESSrequest.attempt_count = (request.attempt_count or 0) + 1request.last_executed_at = now- update_fields = ["status", "updated_at", "attempt_count", "last_executed_at"]+ request.last_dagster_run_id = run_id+ update_fields = ["status", "updated_at", "attempt_count", "last_executed_at", "last_dagster_run_id"]if request.first_executed_at is None:request.first_executed_at = nowupdate_fields.append("first_executed_at")@@ -354,7 +357,7 @@ def load_deletion_request(f"Request {config.request_id} is not an approved event_removal request.",)- _record_execution_attempt(request)+ _record_execution_attempt(request, context.run_id)events_desc = "<all events>" if request.delete_all_events else f"{request.events}"context.log.info(@@ -515,7 +518,7 @@ def load_property_removal_request(f"Request {config.request_id} has no properties or person_properties specified.",)- _record_execution_attempt(request)+ _record_execution_attempt(request, context.run_id)# Set once and reused verbatim by every retry: all attempts must agree on which# inserted_at value identifies cleaned re-inserts, or re-runs duplicate them.@@ -934,7 +937,7 @@ def load_person_removal_request("they are mutually exclusive.")- _record_execution_attempt(request)+ _record_execution_attempt(request, context.run_id)# The fields are nullable on the model (NULL for non-person_removal rows), but# PersonRemovalContext and the downstream `if not drop_x` consumers want plain bools.diff --git a/posthog/dags/tests/test_data_deletion_requests.py b/posthog/dags/tests/test_data_deletion_requests.pyindex 9ca84129ba7e..ebb19a3f987c 100644--- a/posthog/dags/tests/test_data_deletion_requests.py+++ b/posthog/dags/tests/test_data_deletion_requests.py@@ -116,6 +116,7 @@ def test_load_deletion_request_transitions_to_in_progress():assert request.attempt_count == 1assert request.first_executed_at is not Noneassert request.last_executed_at == request.first_executed_at+ assert request.last_dagster_run_id == context.run_id@pytest.mark.django_db@@ -634,6 +635,7 @@ def test_load_property_removal_request_transitions_to_in_progress():request.refresh_from_db()assert request.status == RequestStatus.IN_PROGRESS+ assert request.last_dagster_run_id == context.run_id@pytest.mark.django_db@@ -1701,6 +1703,7 @@ def test_load_person_removal_request_transitions_to_in_progress():assert result.drop_recordings is Falserequest.refresh_from_db()assert request.status == RequestStatus.IN_PROGRESS+ assert request.last_dagster_run_id == ctx.run_id@pytest.mark.django_dbdiff --git a/posthog/migrations/1254_datadeletionrequest_last_dagster_run_id.py b/posthog/migrations/1254_datadeletionrequest_last_dagster_run_id.pynew file mode 100644index 000000000000..f76dfe82c42c--- /dev/null+++ b/posthog/migrations/1254_datadeletionrequest_last_dagster_run_id.py@@ -0,0 +1,22 @@+# Generated by Django 5.2.14 on 2026-07-14 09:33++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("posthog", "1253_oauth_application_github_grants_rate_limit"),+ ]++ operations = [+ migrations.AddField(+ model_name="datadeletionrequest",+ name="last_dagster_run_id",+ field=models.CharField(+ blank=True,+ help_text="Dagster run ID of the most recent execution attempt (set on every APPROVED → IN_PROGRESS transition). Rendered as a link to the Dagster run in the admin.",+ max_length=255,+ null=True,+ ),+ ),+ ]diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txtindex ab59c5775002..281e094313eb 100644--- a/posthog/migrations/max_migration.txt+++ b/posthog/migrations/max_migration.txt@@ -1 +1 @@-1253_oauth_application_github_grants_rate_limit+1254_datadeletionrequest_last_dagster_run_iddiff --git a/posthog/models/data_deletion_request.py b/posthog/models/data_deletion_request.pyindex 6bc81e7c80b2..b12dd8ea6732 100644--- a/posthog/models/data_deletion_request.py+++ b/posthog/models/data_deletion_request.py@@ -369,6 +369,16 @@ class DataDeletionRequest(UUIDModel):blank=True,help_text="When execution was most recently attempted (updated on every APPROVED → IN_PROGRESS transition).",)+ last_dagster_run_id = models.CharField(+ # Dagster run ids are UUIDs, but keep headroom: this is written by the same save() that marks+ # the request IN_PROGRESS, so an over-long id would fail the whole deletion job over a field+ # that only exists to make debugging easier.+ max_length=255,+ null=True,+ blank=True,+ help_text="Dagster run ID of the most recent execution attempt (set on every APPROVED → IN_PROGRESS "+ "transition). Rendered as a link to the Dagster run in the admin.",+ )property_removal_marker = models.DateTimeField(null=True,blank=True,