zadmin/db/migrations/20241214113527_add_ulid_functions.sql
2024-12-14 14:44:37 +01:00

14 lines
430 B
PL/PgSQL

-- +goose Up
-- +goose StatementBegin
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE OR REPLACE FUNCTION generate_ulid() RETURNS uuid
AS $$
SELECT (lpad(to_hex(floor(extract(epoch FROM clock_timestamp()) * 1000)::bigint), 12, '0') || encode(gen_random_bytes(10), 'hex'))::uuid;
$$ LANGUAGE SQL;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP FUNCTION generate_ulid();
-- +goose StatementEnd