-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-vertical-runs.sql
More file actions
38 lines (33 loc) · 1.17 KB
/
Copy pathsupabase-vertical-runs.sql
File metadata and controls
38 lines (33 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- Vertical run tracking: records when a campaign was run for a specific vertical
CREATE TABLE IF NOT EXISTS vertical_runs (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
client_id uuid NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
vertical text NOT NULL,
date_started date NOT NULL,
geography text,
lead_count integer,
notes text,
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now()
);
-- Index for fast lookups by client + vertical
CREATE INDEX IF NOT EXISTS idx_vertical_runs_client_vertical ON vertical_runs(client_id, vertical);
-- RLS
ALTER TABLE vertical_runs ENABLE ROW LEVEL SECURITY;
-- Admin full access
CREATE POLICY "Admin full access to vertical_runs"
ON vertical_runs FOR ALL
USING (
EXISTS (SELECT 1 FROM profiles WHERE profiles.id = auth.uid() AND profiles.role = 'admin')
);
-- Client users can view vertical runs for their clients
CREATE POLICY "Client users can view vertical_runs"
ON vertical_runs FOR SELECT
USING (
EXISTS (
SELECT 1 FROM client_users
WHERE client_users.client_id = vertical_runs.client_id
AND client_users.user_id = auth.uid()
)
);
-- Service role bypasses RLS