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): add concurrent indexes for dashboard widget tiles (5/11) (#60493)

Matt Pua335f51b58d6d
diff --git a/products/dashboards/backend/migrations/0011_dashboardtile_widget_id_idx.py b/products/dashboards/backend/migrations/0011_dashboardtile_widget_id_idx.py
new file mode 100644
index 000000000000..46475af9e5db
--- /dev/null
+++ b/products/dashboards/backend/migrations/0011_dashboardtile_widget_id_idx.py
@@ -0,0 +1,43 @@
+# Generated manually for dashboard widget entity
+
+from django.db import migrations, models
+from django.db.models import Q
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("dashboards", "0010_validate_dashboardtile_check"),
+ ]
+
+ operations = [
+ migrations.SeparateDatabaseAndState(
+ state_operations=[
+ migrations.AddConstraint(
+ model_name="dashboardtile",
+ constraint=models.UniqueConstraint(
+ fields=["dashboard", "widget"],
+ name="unique_dashboard_widget",
+ condition=Q(("widget__isnull", False)),
+ ),
+ ),
+ ],
+ ),
+ migrations.RunSQL(
+ sql=(
+ 'CREATE INDEX CONCURRENTLY IF NOT EXISTS "posthog_dashboardtile_widget_id_idx" '
+ 'ON "posthog_dashboardtile" ("widget_id") '
+ 'WHERE "widget_id" IS NOT NULL'
+ ),
+ reverse_sql='DROP INDEX CONCURRENTLY IF EXISTS "posthog_dashboardtile_widget_id_idx"',
+ ),
+ migrations.RunSQL(
+ sql=(
+ 'CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "unique_dashboard_widget" '
+ 'ON "posthog_dashboardtile" ("dashboard_id", "widget_id") '
+ 'WHERE "widget_id" IS NOT NULL'
+ ),
+ reverse_sql='DROP INDEX CONCURRENTLY IF EXISTS "unique_dashboard_widget"',
+ ),
+ ]
diff --git a/products/dashboards/backend/migrations/max_migration.txt b/products/dashboards/backend/migrations/max_migration.txt
index b95ffc84269a..bb4a14248aa7 100644
--- a/products/dashboards/backend/migrations/max_migration.txt
+++ b/products/dashboards/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0010_validate_dashboardtile_check
+0011_dashboardtile_widget_id_idx
diff --git a/products/dashboards/backend/models/dashboard_tile.py b/products/dashboards/backend/models/dashboard_tile.py
index eaabcc477790..75e10b161c42 100644
--- a/products/dashboards/backend/models/dashboard_tile.py
+++ b/products/dashboards/backend/models/dashboard_tile.py
@@ -128,6 +128,11 @@ class DashboardTile(models.Model):
name="unique_dashboard_button_tile",
condition=Q(("button_tile__isnull", False)),
),
+ UniqueConstraint(
+ fields=["dashboard", "widget"],
+ name="unique_dashboard_widget",
+ condition=Q(("widget__isnull", False)),
+ ),
models.CheckConstraint(
condition=build_unique_relationship_check(("insight", "text", "button_tile", "widget")),
name="dash_tile_exactly_one_related_object",