create table public.site_settings ( id boolean primary key default true check (id), beta_badge_enabled boolean not null default true, updated_at timestamptz not null default now(), updated_by uuid references public.profiles(id) on delete set null ); insert into public.site_settings (id) values (true); alter table public.site_settings enable row level security; revoke all on public.site_settings from public, anon, authenticated; grant select, update on public.site_settings to service_role; create policy "Server manages site settings" on public.site_settings for all to service_role using (true) with check (true); -- Invoked only by the server after authenticating the moderator. The profile -- check also protects this operation if another server caller is added later. create function public.set_beta_badge(p_reviewer uuid, p_enabled boolean) returns boolean language plpgsql security invoker set search_path = '' as $$ begin if not exists ( select 1 from public.profiles where id = p_reviewer and role = 'moderator' ) then raise exception using errcode = '42501', message = 'Moderator permission required'; end if; if p_enabled is null then raise exception 'Beta badge visibility is required'; end if; update public.site_settings set beta_badge_enabled = p_enabled, updated_at = now(), updated_by = p_reviewer where id = true; if not found then raise exception 'Site settings not found'; end if; return p_enabled; end; $$; revoke all on function public.set_beta_badge(uuid, boolean) from public, anon, authenticated; grant execute on function public.set_beta_badge(uuid, boolean) to service_role;