-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-setup.sql
More file actions
77 lines (71 loc) · 2.72 KB
/
Copy pathsupabase-setup.sql
File metadata and controls
77 lines (71 loc) · 2.72 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-- ============================================================
-- JRS One-Minute Challenge — Supabase setup
-- Run this once in your Supabase project:
-- Dashboard -> SQL Editor -> New query -> paste -> Run
-- Project: https://pjzxkeviouofdseagvpf.supabase.co
-- ============================================================
-- 1) Submissions table
create table if not exists public.omc_responses (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now(),
participant_id text,
answer text check (answer in ('Yes','Partially','No')),
why text,
role text,
first_name text,
last_name text,
email text,
consent_updates boolean default false,
consent_aggregate boolean default false,
consent_recognition boolean default false,
referrer text,
utm_source text,
utm_medium text,
utm_campaign text,
session_source text,
device_type text,
completion_status text
);
-- 2) Lock the table down with Row Level Security
alter table public.omc_responses enable row level security;
-- Allow anonymous visitors to INSERT their own submission only.
-- (No SELECT policy for anon => raw rows, including names/emails, are NOT publicly readable.)
drop policy if exists "anon can insert submissions" on public.omc_responses;
create policy "anon can insert submissions"
on public.omc_responses
for insert
to anon
with check (true);
-- 3) Public aggregate function (no personal data leaves the database).
-- SECURITY DEFINER lets it read the table while anon cannot read rows directly.
create or replace function public.omc_results()
returns json
language sql
security definer
set search_path = public
as $$
select json_build_object(
'min_sample_for_comparison', 5,
'participants', count(*),
'reviews_completed', count(*) filter (where completion_status = 'completed'),
'distribution', json_build_object(
'Yes', count(*) filter (where answer = 'Yes'),
'Partially', count(*) filter (where answer = 'Partially'),
'No', count(*) filter (where answer = 'No')
),
'roles', coalesce((
select json_object_agg(role, c)
from (
select role, count(*) c
from public.omc_responses
where role is not null and role <> ''
group by role
) t
), '{}'::json),
'updated', to_char(now(), 'YYYY-MM-DD')
)
from public.omc_responses;
$$;
grant execute on function public.omc_results() to anon;
-- Done. The site reads aggregates via: POST /rest/v1/rpc/omc_results
-- and writes submissions via: POST /rest/v1/omc_responses