Though this is not an answer for the original question however I had to do this conversion in the other way around, that is, mimic UUID.nameUUIDFromBytes
behaviour in PostgreSQL during a migration:
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE EXTENSION IF NOT EXISTS "plpgsql";
CREATE OR REPLACE FUNCTION uuidv3(bytes bytea)
RETURNS uuid
RETURNS null on null input
STABLE
PARALLEL SAFE
AS $$
DECLARE
md5bytes bytea;
BEGIN
md5bytes := digest(bytes, 'md5');
-- md5Bytes[6] &= 0x0f; /* clear version */
-- md5Bytes[6] |= 0x30; /* set to version 3 */
md5bytes := set_byte(md5bytes, 6, (get_byte(md5bytes, 6) & x'0F'::int) | x'30'::int);
-- md5Bytes[8] &= 0x3f; /* clear variant */
-- md5Bytes[8] |= 0x80; /* set to IETF variant */
md5bytes := set_byte(md5bytes, 8, (get_byte(md5bytes, 8) & x'3F'::int)| x'80'::int);
RETURN (encode(md5bytes, 'hex'))::uuid;
END
$$
LANGUAGE plpgsql;
and you should use internal textsend
to convert text columns to bytea
:
SELECT uuidv3(textsend('this is a test'));
Though I must admit I have never tried the other way.