Skip to content

Commit

Permalink
Breakout alert updates into new migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kiahna-tucker authored and psFried committed Dec 1, 2023
1 parent a2da641 commit 8c6d152
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
26 changes: 9 additions & 17 deletions supabase/migrations/39_alerts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,21 @@ select
create extension if not exists pg_net with schema extensions;
create or replace function internal.send_alerts()
returns trigger as $trigger$
declare
token text;
begin

select decrypted_secret into token from vault.decrypted_secrets where name = 'alert-email-fn-shared-secret' limit 1;

if new.alert_type = 'data_not_processed_in_interval' then
perform
net.http_post(
'https://eyrcnmuzzyriypdajwdk.supabase.co/functions/v1/alert-data-processing',
to_jsonb(new.*),
headers:=format('{"Content-Type": "application/json", "Authorization": "Basic %s"}', token)::jsonb
);
end if;
perform
net.http_post(
url:='https://eyrcnmuzzyriypdajwdk.supabase.co/functions/v1/alert-data-processing',
headers:='{"Content-Type": "application/json", "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV5cmNubXV6enlyaXlwZGFqd2RrIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NDg3NTA1NzksImV4cCI6MTk2NDMyNjU3OX0.y1OyXD3-DYMz10eGxzo1eeamVMMUwIIeOoMryTRAoco"}'::jsonb,
body:=concat('{"time": "', now(), '"}')::jsonb
);

return null;

end;
$trigger$ LANGUAGE plpgsql;

create trigger "Send email after alert fired" after insert on alert_history
for each row execute procedure internal.send_alerts();

create trigger "Send email after alert resolved" after update on alert_history
for each row when (old.resolved_at is null and new.resolved_at is not null) execute procedure internal.send_alerts();
create trigger "Send alerts" after insert or update on alert_history
for each statement execute procedure internal.send_alerts();

commit;
32 changes: 32 additions & 0 deletions supabase/migrations/41_alert_updates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
begin;

create or replace function internal.send_alerts()
returns trigger as $trigger$
declare
token text;
begin

select decrypted_secret into token from vault.decrypted_secrets where name = 'alert-email-fn-shared-secret' limit 1;

if new.alert_type = 'data_not_processed_in_interval' then
perform
net.http_post(
'https://eyrcnmuzzyriypdajwdk.supabase.co/functions/v1/alert-data-processing',
to_jsonb(new.*),
headers:=format('{"Content-Type": "application/json", "Authorization": "Basic %s"}', token)::jsonb
);
end if;

return null;
end;
$trigger$ LANGUAGE plpgsql;

drop trigger "Send alerts" on alert_history;

create trigger "Send email after alert fired" after insert on alert_history
for each row execute procedure internal.send_alerts();

create trigger "Send email after alert resolved" after update on alert_history
for each row when (old.resolved_at is null and new.resolved_at is not null) execute procedure internal.send_alerts();

commit;

0 comments on commit 8c6d152

Please sign in to comment.