|
| 1 | +-- 0001_create_dashboard_tables.sql |
| 2 | +-- Creates the customizable-dashboard tables in the plugin schema. |
| 3 | +-- Run with search_path = plugin_data_com_paca_dashboard, public. |
| 4 | +-- |
| 5 | +-- Three dashboard scopes share these two tables: |
| 6 | +-- 'project' — exactly one row per project (the project dashboard page). |
| 7 | +-- 'admin' — exactly one row total, project_id NULL (the admin dashboard page). |
| 8 | +-- 'integration' — any number of rows per project, user-created from the |
| 9 | +-- "Add view" popover on Integration pages (backlog/sprint/timeline). |
| 10 | +-- |
| 11 | +-- Partial unique indexes enforce the "exactly one" constraint for the |
| 12 | +-- project/admin scopes at the database level rather than relying on |
| 13 | +-- application logic alone. |
| 14 | + |
| 15 | +CREATE TABLE IF NOT EXISTS dashboard_views ( |
| 16 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 17 | + project_id UUID REFERENCES projects(id) ON DELETE CASCADE, |
| 18 | + scope TEXT NOT NULL CHECK (scope IN ('project', 'admin', 'integration')), |
| 19 | + name TEXT NOT NULL DEFAULT 'Dashboard', |
| 20 | + created_by UUID REFERENCES project_members(id) ON DELETE SET NULL, |
| 21 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 22 | + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 23 | + |
| 24 | + CONSTRAINT dashboard_views_project_scope_requires_project_id |
| 25 | + CHECK ( |
| 26 | + (scope = 'admin' AND project_id IS NULL) OR |
| 27 | + (scope IN ('project', 'integration') AND project_id IS NOT NULL) |
| 28 | + ) |
| 29 | +); |
| 30 | + |
| 31 | +-- At most one 'project'-scope dashboard per project. |
| 32 | +CREATE UNIQUE INDEX IF NOT EXISTS uq_dashboard_views_one_project_scope |
| 33 | + ON dashboard_views (project_id) |
| 34 | + WHERE scope = 'project'; |
| 35 | + |
| 36 | +-- At most one 'admin'-scope dashboard, instance-wide. |
| 37 | +CREATE UNIQUE INDEX IF NOT EXISTS uq_dashboard_views_one_admin_scope |
| 38 | + ON dashboard_views ((1)) |
| 39 | + WHERE scope = 'admin'; |
| 40 | + |
| 41 | +CREATE INDEX IF NOT EXISTS idx_dashboard_views_project_scope |
| 42 | + ON dashboard_views (project_id, scope); |
| 43 | + |
| 44 | +CREATE TABLE IF NOT EXISTS dashboard_panels ( |
| 45 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 46 | + dashboard_view_id UUID NOT NULL REFERENCES dashboard_views(id) ON DELETE CASCADE, |
| 47 | + type TEXT NOT NULL CHECK (type IN ('chart', 'table', 'text')), |
| 48 | + title TEXT NOT NULL DEFAULT '', |
| 49 | + -- SQL query text for 'chart'/'table' panels; NULL for 'text' panels. |
| 50 | + -- See query_guard.go for the safety model enforced before execution: |
| 51 | + -- must be a single read-only SELECT/WITH statement, must reference the |
| 52 | + -- {{project_id}} placeholder (for project/integration-scoped panels), |
| 53 | + -- and gets a LIMIT appended if it doesn't declare one. Sensitive |
| 54 | + -- columns it happens to read (core platform secrets, or another |
| 55 | + -- plugin's own declared-sensitive fields) come back masked from the |
| 56 | + -- host itself, not from any table restriction here. |
| 57 | + query TEXT, |
| 58 | + -- Chart rendering hint ('bar' | 'line' | 'donut'); NULL for table/text. |
| 59 | + chart_type TEXT, |
| 60 | + -- Static markdown body for 'text' panels; NULL for chart/table. |
| 61 | + content TEXT, |
| 62 | + -- Free-form visualization config (axis/series field mapping, colors, |
| 63 | + -- column order for table panels, etc.) — validated shape lives in the |
| 64 | + -- frontend; the backend treats it as an opaque JSON blob. |
| 65 | + viz_config JSONB NOT NULL DEFAULT '{}', |
| 66 | + pos_x INTEGER NOT NULL DEFAULT 0, |
| 67 | + pos_y INTEGER NOT NULL DEFAULT 0, |
| 68 | + width INTEGER NOT NULL DEFAULT 4, |
| 69 | + height INTEGER NOT NULL DEFAULT 3, |
| 70 | + created_by UUID REFERENCES project_members(id) ON DELETE SET NULL, |
| 71 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 72 | + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() |
| 73 | +); |
| 74 | + |
| 75 | +CREATE INDEX IF NOT EXISTS idx_dashboard_panels_view_id |
| 76 | + ON dashboard_panels (dashboard_view_id); |
0 commit comments