Files
supabase__supabase/supabase/migrations/20250521194255_error_code_update_functions.sql
Charis 22ae9a2caf feat(db): error table updating function (#35819)
* feat(db): error table updating function

Add a function for updating the error table (to be used for syncing the
repo contents to the database).

* feat(db): add function to delete unused error codes

Add a DB function to delete any unused error codes (actually a soft
delete due to a rule on the content.error table). This allows syncing
the repo state to the DB state by removing any entries that have been
removed from the repo.
2025-05-27 10:05:40 -04:00

48 lines
1.3 KiB
PL/PgSQL

alter default privileges in schema content
revoke execute on functions from anon;
alter default privileges in schema content
revoke execute on functions from authenticated;
create function content.update_error_code(
code text,
service text,
http_status_code smallint default null,
message text default null
)
returns boolean
set search_path = ''
language plpgsql
as $$
#variable_conflict use_variable
declare
service_id uuid;
result boolean;
begin
insert into content.service (name)
values (service)
on conflict (name) do nothing;
select id into service_id
from content.service
where name = service;
insert into content.error (service, code, http_status_code, message)
values (service_id, code, http_status_code, message)
on conflict on constraint error_pkey do
update set
http_status_code = excluded.http_status_code,
message = excluded.message
where
error.service = service_id
and error.code = code
and (
error.http_status_code is distinct from excluded.http_status_code
or error.message is distinct from excluded.message
)
returning true into result;
return coalesce(result, false);
end;
$$;