From 265e38aade29e9619cd0810730638a04aa2412ac Mon Sep 17 00:00:00 2001 From: Jose Lago Date: Wed, 9 Sep 2026 13:05:26 +0200 Subject: [PATCH] Auto-set is_admin for @appe.at contacts via trigger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Co-authored-by: Stephan Charles Co-authored-by: Amelia Hernandez Co-authored-by: Markus Franz Co-authored-by: Sabrina Hota Co-authored-by: Nilüfer Doğan --- .../20260909020000_appe_at_admin_trigger.sql | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 supabase/migrations/20260909020000_appe_at_admin_trigger.sql diff --git a/supabase/migrations/20260909020000_appe_at_admin_trigger.sql b/supabase/migrations/20260909020000_appe_at_admin_trigger.sql new file mode 100644 index 0000000..3dd880d --- /dev/null +++ b/supabase/migrations/20260909020000_appe_at_admin_trigger.sql @@ -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;