Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(db): error table updating function (#35819)

Charis22ae9a2caf40
diff --git a/supabase/migrations/20250521194255_error_code_update_functions.sql b/supabase/migrations/20250521194255_error_code_update_functions.sql
new file mode 100644
index 00000000000..b549bb2e84f
--- /dev/null
+++ b/supabase/migrations/20250521194255_error_code_update_functions.sql
@@ -0,0 +1,47 @@
+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;
+$$;
diff --git a/supabase/migrations/20250522171141_error_code_delete_function.sql b/supabase/migrations/20250522171141_error_code_delete_function.sql
new file mode 100644
index 00000000000..368c3b6a753
--- /dev/null
+++ b/supabase/migrations/20250522171141_error_code_delete_function.sql
@@ -0,0 +1,16 @@
+create or replace function content.delete_error_codes_except(
+ skip_codes jsonb
+)
+returns void
+set search_path = ''
+language sql
+as $$
+ delete from content.error
+ where not exists (
+ select 1
+ from jsonb_array_elements(skip_codes) skipped
+ join content.service on service.name = (skipped ->> 'service')
+ where service.id = error.service
+ and error.code = (skipped ->> 'error_code')
+ );
+$$;