-
-
Save SIFAR786/3321922670c007b375b1077b37b1ce97 to your computer and use it in GitHub Desktop.
Decode SendGrid Event ID in Postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace function sendgrid_eid24_to_uuid(eid text) | |
returns uuid language sql immutable strict parallel safe | |
as $function$ | |
select encode(decode(translate(eid, '-_', '+/'), 'base64'), 'hex')::uuid; | |
$function$; | |
create or replace function sendgrid_eid48_to_uuid(eid text) | |
returns uuid language sql immutable strict parallel safe | |
as $function$ | |
select convert_from(decode(translate(eid, '-_', '+/'), 'base64'), 'UTF-8')::uuid; | |
$function$; | |
create or replace function sendgrid_eid_to_uuid(eid text) | |
returns uuid language plpgsql immutable strict parallel safe | |
as $function$ | |
begin | |
case octet_length(eid) | |
when 22 then | |
return sendgrid_eid24_to_uuid(eid || '=='); | |
when 24 then | |
return sendgrid_eid24_to_uuid(eid); | |
when 48 then | |
return sendgrid_eid48_to_uuid(eid); | |
else | |
raise exception 'Unsupported event id: %', eid; | |
end case; | |
end; | |
$function$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment