Same trigger for insert and delete

Hi there,

I was working on some trigger for updating the claims in the JWT for the user that gets changes to their permissions.
The following function came to life because it was not updating on deletes but only on inserts.

create or replace function update_claims_member_schemas()
returns trigger as
$$
  declare
    nothing text;
  begin
    select update_user_claims(new.user_id) into nothing;
    select update_user_claims(old.user_id) into nothing;
    return new;
  end;
$$
language plpgsql security definer;

create trigger user_roles_update_user_claims after delete or insert on user_roles
for each row execute
  procedure update_claims_member_schemas(user_id);

I have to execute the function for new and old now because an insert gives an object called new and a delete gives an object called old.

Because this seems incorrect I was wondering if there is a better way to do this. Is there an object which is always available or is this the correct way?


Thanks in advance!
Was this page helpful?