Skip to content

: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.

feat(dashboards): extend dashboard tile exclusivity check for widgets (3/11) (#60491)

Matt Pua3944be16d304
diff --git a/products/dashboards/backend/migrations/0009_dashboardtile_check_4way.py b/products/dashboards/backend/migrations/0009_dashboardtile_check_4way.py
new file mode 100644
index 000000000000..11902c08d3ef
--- /dev/null
+++ b/products/dashboards/backend/migrations/0009_dashboardtile_check_4way.py
@@ -0,0 +1,56 @@
+# Generated manually for dashboard widget entity
+
+from django.db import migrations, models
+
+import posthog.models.utils
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("dashboards", "0008_dashboardtile_widget_id"),
+ ]
+
+ operations = [
+ migrations.SeparateDatabaseAndState(
+ state_operations=[
+ migrations.RemoveConstraint(
+ model_name="dashboardtile",
+ name="dash_tile_exactly_one_related_object",
+ ),
+ migrations.AddConstraint(
+ model_name="dashboardtile",
+ constraint=models.CheckConstraint(
+ condition=posthog.models.utils.build_unique_relationship_check(
+ ("insight", "text", "button_tile", "widget")
+ ),
+ name="dash_tile_exactly_one_related_object",
+ ),
+ ),
+ ],
+ database_operations=[
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE "posthog_dashboardtile" DROP CONSTRAINT IF EXISTS "dash_tile_exactly_one_related_object";
+ ALTER TABLE "posthog_dashboardtile" ADD CONSTRAINT "dash_tile_exactly_one_related_object" -- existing-table-constraint-ignore
+ CHECK (
+ ("insight_id" IS NOT NULL AND "text_id" IS NULL AND "button_tile_id" IS NULL AND "widget_id" IS NULL)
+ OR ("insight_id" IS NULL AND "text_id" IS NOT NULL AND "button_tile_id" IS NULL AND "widget_id" IS NULL)
+ OR ("insight_id" IS NULL AND "text_id" IS NULL AND "button_tile_id" IS NOT NULL AND "widget_id" IS NULL)
+ OR ("insight_id" IS NULL AND "text_id" IS NULL AND "button_tile_id" IS NULL AND "widget_id" IS NOT NULL)
+ )
+ NOT VALID;
+ """,
+ reverse_sql="""
+ ALTER TABLE "posthog_dashboardtile" DROP CONSTRAINT IF EXISTS "dash_tile_exactly_one_related_object";
+ ALTER TABLE "posthog_dashboardtile" ADD CONSTRAINT "dash_tile_exactly_one_related_object" -- existing-table-constraint-ignore
+ CHECK (
+ ("insight_id" IS NOT NULL AND "text_id" IS NULL AND "button_tile_id" IS NULL)
+ OR ("insight_id" IS NULL AND "text_id" IS NOT NULL AND "button_tile_id" IS NULL)
+ OR ("insight_id" IS NULL AND "text_id" IS NULL AND "button_tile_id" IS NOT NULL)
+ )
+ NOT VALID;
+ """,
+ ),
+ ],
+ ),
+ ]
diff --git a/products/dashboards/backend/migrations/max_migration.txt b/products/dashboards/backend/migrations/max_migration.txt
index 9473481dc3a2..56138bf2ddf4 100644
--- a/products/dashboards/backend/migrations/max_migration.txt
+++ b/products/dashboards/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0008_dashboardtile_widget_id
+0009_dashboardtile_check_4way
diff --git a/products/dashboards/backend/models/dashboard_tile.py b/products/dashboards/backend/models/dashboard_tile.py
index f94dd35b6ea0..eaabcc477790 100644
--- a/products/dashboards/backend/models/dashboard_tile.py
+++ b/products/dashboards/backend/models/dashboard_tile.py
@@ -129,7 +129,7 @@ class DashboardTile(models.Model):
condition=Q(("button_tile__isnull", False)),
),
models.CheckConstraint(
- condition=build_unique_relationship_check(("insight", "text", "button_tile")),
+ condition=build_unique_relationship_check(("insight", "text", "button_tile", "widget")),
name="dash_tile_exactly_one_related_object",
),
]
@@ -174,10 +174,11 @@ class DashboardTile(models.Model):
def clean(self):
super().clean()
- # Widget exclusivity lands with the 4-way DB check in migration 0009 (PR #60491).
- related_fields = sum(map(bool, [getattr(self, o_field) for o_field in ("insight", "text", "button_tile")]))
+ related_fields = sum(
+ map(bool, [getattr(self, o_field) for o_field in ("insight", "text", "button_tile", "widget")])
+ )
if related_fields != 1:
- raise ValidationError("Can only set exactly one of insight, text, or button_tile for this tile")
+ raise ValidationError("Can only set exactly one of insight, text, button_tile, or widget for this tile")
if self.insight is None and (
self.filters_hash is not None