: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(subscriptions): require insight or dashboard on create (#60687)
Vasco de Krijgeraa53c2ae55ba
diff --git a/ee/api/subscription.py b/ee/api/subscription.pyindex 3fbe6602ab66..00574465bcb8 100644--- a/ee/api/subscription.py+++ b/ee/api/subscription.py@@ -201,8 +201,8 @@ class SubscriptionSerializer(serializers.ModelSerializer):if msg:raise ValidationError({"subscription": [msg]})- if not self.initial_data:- # Create+ if self.instance is None:+ # Create: a subscription must export an insight or a dashboard.if not attrs.get("dashboard") and not attrs.get("insight"):raise ValidationError("Either dashboard or insight is required for an export.")diff --git a/ee/api/test/test_subscription.py b/ee/api/test/test_subscription.pyindex ad2ee43b26a0..637f602b5d34 100644--- a/ee/api/test/test_subscription.py+++ b/ee/api/test/test_subscription.py@@ -121,6 +121,27 @@ class TestSubscriptionTemporal(APILicensedTest):assert activity_inputs.subscription_id == data["id"]assert activity_inputs.invite_message == "hey there!"+ def test_cannot_create_subscription_without_insight_or_dashboard(self):+ response = self.client.post(+ f"/api/projects/{self.team.id}/subscriptions",+ {+ "target_type": "email",+ "target_value": "[email protected]",+ "frequency": "weekly",+ "interval": 1,+ "start_date": "2022-01-01T00:00:00",+ "title": "No content source",+ },+ )+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ assert "Either dashboard or insight is required" in str(response.json())++ def test_can_update_subscription_without_resending_relation(self):+ sub_id = self._create_subscription().json()["id"]+ response = self.client.patch(f"/api/projects/{self.team.id}/subscriptions/{sub_id}", {"title": "Updated title"})+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response.json()["title"], "Updated title")+def test_can_create_new_subscription_without_invite_message(self):response = self._create_subscription(invite_message=None)self.assertEqual(response.status_code, status.HTTP_201_CREATED)diff --git a/posthog/migrations/1191_delete_relationless_subscriptions.py b/posthog/migrations/1191_delete_relationless_subscriptions.pynew file mode 100644index 000000000000..5979896f6c33--- /dev/null+++ b/posthog/migrations/1191_delete_relationless_subscriptions.py@@ -0,0 +1,21 @@+from django.db import migrations+++def delete_relationless_subscriptions(apps, schema_editor):+ # A subscription with neither an insight nor a dashboard has no content to export, can+ # never deliver, and has no UI entry point (subscriptions are only reachable via their+ # insight/dashboard). Such rows slipped in via a create-time guard that never fired —+ # purge them. SubscriptionDelivery rows cascade away with them.+ Subscription = apps.get_model("posthog", "Subscription")+ Subscription.objects.filter(insight__isnull=True, dashboard__isnull=True).delete()+++class Migration(migrations.Migration):+ dependencies = [+ ("posthog", "1190_team_llm_gateway_metadata"),+ ]++ operations = [+ # Data-only, irreversible: deleted rows are unrecoverable, so the reverse is a no-op.+ migrations.RunPython(delete_relationless_subscriptions, reverse_code=migrations.RunPython.noop),+ ]diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txtindex 4ddc9670a5b8..b34f5af4145d 100644--- a/posthog/migrations/max_migration.txt+++ b/posthog/migrations/max_migration.txt@@ -1 +1 @@-1190_team_llm_gateway_metadata+1191_delete_relationless_subscriptions