fahrPool/supabase/migrations/20261108100000_auftraege_ta...

30 lines
1.0 KiB
PL/PgSQL

-- Tankfuellstand, mit dem das Fahrzeug am Ziel abgegeben werden soll.
-- Vorgabe des Disponenten, optional (null = keine Vorgabe).
alter table public.auftraege
add column tankfuellstand_abgabe text
constraint auftraege_tankfuellstand_abgabe_check
check (tankfuellstand_abgabe in ('1/4', '1/2', '3/4', 'voll'));
-- Die Fahrer-Update-Policy erlaubt Aenderungen am eigenen Auftrag; die
-- Vorgabe gehoert aber dem Disponenten.
create or replace function public.auftrag_tankfuellstand_schuetzen()
returns trigger
language plpgsql
security definer
set search_path = public
as $$
begin
if new.tankfuellstand_abgabe is distinct from old.tankfuellstand_abgabe
and coalesce(public.user_role()::text, '') not in ('admin', 'disponent')
and auth.uid() is not null
then
raise exception 'Nur Disponent/Admin duerfen den Tankfuellstand aendern';
end if;
return new;
end;
$$;
create trigger auftrag_tankfuellstand_schuetzen
before update on public.auftraege
for each row execute function public.auftrag_tankfuellstand_schuetzen();