Skip to content

Instantly share code, notes, and snippets.

View SylvainA77's full-sized avatar
👾
#MariaDBPartner

Sylvain Arbaudie SylvainA77

👾
#MariaDBPartner
View GitHub Profile
@juanparati
juanparati / mariadb_uuidv7.sql
Last active March 2, 2024 19:38
UUID7 generation function for MariaDB
DELIMITER $$
CREATE DEFINER = CURRENT_USER FUNCTION `uuidv7` () RETURNS UUID LANGUAGE SQL NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
COMMENT 'Generate a UUID7 according to https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#variant_and_version_fields'
BEGIN
-- Obtain date with 4 milliseconds precision
SET @now = sysdate(4);
/*
UNIX_TIMESTAMP returns a 32bits number, so this function will not work after the Epochalypse, I am crossing my fingers and I hope that MySQL/MariaDB teams will move to 64bits timestamps before than 2038.
*/
SET @time_sec = LPAD(HEX(TRUNCATE(UNIX_TIMESTAMP(@now), 0)), 9, '0');