create or replace function public.prevent_promoting_to_system()
returns trigger
language plpgsql
as $$
begin
  if new.is_system = true and old.is_system is distinct from true then
    raise exception 'Não é permitido transformar um grupo comum em grupo do sistema.';
  end if;
  return new;
end;
$$;

drop trigger if exists trg_prevent_promoting_to_system on public.patient_groups;

create trigger trg_prevent_promoting_to_system
before update on public.patient_groups
for each row
execute function public.prevent_promoting_to_system();
