Auto-set is_admin for @appe.at contacts via trigger

The one-time backfill (20260909010000) missed contacts created later (e.g. by
the Pretix sync), so their is_admin flag stayed false and the admin UI badge
showed them as non-admin even though is_admin() treated them as admins.

Adds a BEFORE INSERT OR UPDATE trigger that auto-sets is_admin=true for any
@appe.at email, keeping the flag, the UI badge, and is_admin() in sync for
every current + future contact. Re-applied the rule to the 2 rows created
since the last backfill.
This commit is contained in:
Lago committed 2026-09-09 13:05:26 +02:00
1 parent d341a8aa8f
commit cdcfdb2c33
1 file changed
+35
@@ -0,0 +1,35 @@
-- =============================================================================
-- Convivo — @appe.at contacts are admins automatically (trigger)
--
-- Follow-up to 20260909010000 (which made is_admin() domain-aware + backfilled
-- existing rows). That backfill only ran once, so contacts created LATER (e.g.
-- by the Pretix sync) still had is_admin=false — the DB function treated them
-- as admins, but the admin UI's "admin" badge reads the flag, so they looked
-- non-admin.
--
-- This adds a BEFORE INSERT OR UPDATE trigger that auto-sets is_admin=true
-- whenever a contact's email is on the @appe.at domain. Now the flag, the UI
-- badge, and is_admin() all agree, for every current + future contact.
-- =============================================================================
create or replace function public._set_appe_at_admin()
returns trigger
language plpgsql security definer set search_path = public as $$
begin
if new.email like '%@appe.at' then
new.is_admin := true;
end if;
return new;
end;
$$;
drop trigger if exists trg_contacts_appe_at_admin on public.contacts;
create trigger trg_contacts_appe_at_admin
before insert or update on public.contacts
for each row execute function public._set_appe_at_admin();
-- Re-apply the rule to any rows created since the last backfill (idempotent).
update public.contacts
set is_admin = true
where email like '%@appe.at'
and not is_admin;