-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path20260524180000_add_event_submission_workflow.sql
More file actions
104 lines (92 loc) · 4.02 KB
/
Copy path20260524180000_add_event_submission_workflow.sql
File metadata and controls
104 lines (92 loc) · 4.02 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
begin;
-- ============================================================================
-- Submission / approval workflow for events
-- ----------------------------------------------------------------------------
-- Adds a moderation queue to public.events so that non-admin users can submit
-- events from a public form, and admins review them before they show up on
-- the site. Existing rows are backfilled to status='approved' so nothing
-- disappears from the public surface on deploy.
-- ============================================================================
alter table public.events
add column if not exists status text not null default 'pending',
add column if not exists submitted_by uuid references auth.users(id) on delete set null,
add column if not exists submitter_name text,
add column if not exists submitter_email text,
add column if not exists submitted_at timestamp with time zone not null default now(),
add column if not exists reviewed_by uuid references auth.users(id) on delete set null,
add column if not exists reviewed_at timestamp with time zone,
add column if not exists rejection_reason text;
do $$
begin
if not exists (
select 1 from pg_constraint where conname = 'events_status_check'
) then
alter table public.events
add constraint events_status_check
check (status in ('pending', 'approved', 'rejected'));
end if;
end $$;
-- Backfill: every event that existed before this migration was admin-created
-- and immediately published, so mark them approved and set reviewed_at to the
-- backfilled submitted_at so the audit trail is internally consistent.
--
-- The where-clause matches every pre-existing row by construction (the three
-- new columns it tests are being added by this same migration with NULL
-- defaults). It reads as a filter but is really a safety belt: if the
-- migration is ever re-run partially or in a weird state, we won't clobber
-- real submissions someone else has inserted in the meantime.
update public.events
set
status = 'approved',
reviewed_at = coalesce(reviewed_at, submitted_at)
where status = 'pending'
and submitted_by is null
and submitter_email is null;
create index if not exists events_status_idx
on public.events (status);
create index if not exists events_submitted_at_idx
on public.events (submitted_at desc);
-- ============================================================================
-- Row-level security
-- ----------------------------------------------------------------------------
-- Public reads only see approved events. Authenticated users see their own
-- submissions (any status) so a future "my submissions" view can render
-- without extra plumbing. Admins manage everything.
-- ============================================================================
alter table public.events enable row level security;
drop policy if exists "Events are publicly readable" on public.events;
drop policy if exists "Public can read events" on public.events;
drop policy if exists "Public read events" on public.events;
drop policy if exists "Approved events are publicly readable" on public.events;
create policy "Approved events are publicly readable"
on public.events
for select
using (status = 'approved');
drop policy if exists "Users can read their own event submissions" on public.events;
create policy "Users can read their own event submissions"
on public.events
for select
to authenticated
using (submitted_by = auth.uid());
drop policy if exists "Admins can manage events" on public.events;
create policy "Admins can manage events"
on public.events
for all
to authenticated
using (
exists (
select 1
from public.profiles
where profiles.id = auth.uid()
and profiles.role = 'admin'
)
)
with check (
exists (
select 1
from public.profiles
where profiles.id = auth.uid()
and profiles.role = 'admin'
)
);
commit;