gohorsejobs/backend/migrations/009_create_uuid_v7_function.sql
Tiago Yamamoto e6fb6dd8cd fix: uuid_generate_v7 integer overflow and seeder role constraint
Migration 009:
- Simplified uuid_generate_v7() to avoid integer overflow on bit shifts
- Uses double precision for timestamp then converts to hex

Seeder:
- Changed roles from 'admin','company' to 'companyAdmin'
- Matches users table CHECK constraint: superadmin, companyAdmin, recruiter, jobSeeker
2025-12-24 11:56:31 -03:00

50 lines
1.5 KiB
PL/PgSQL

-- Migration: Create UUID v7 generation function
-- Description: PostgreSQL function to generate UUID v7 (time-ordered UUIDs)
-- Uses gen_random_uuid() as base, then overrides first 6 bytes with timestamp
-- Enable pgcrypto extension for gen_random_bytes()
CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- Simple UUID v7 function that works with PostgreSQL's type system
CREATE OR REPLACE FUNCTION uuid_generate_v7()
RETURNS uuid AS $$
DECLARE
v_time double precision;
v_secs bigint;
v_msec int;
v_random bytea;
v_uuid bytea;
v_timestamp bytea;
BEGIN
-- Get current time
v_time := extract(epoch from clock_timestamp());
v_secs := floor(v_time);
v_msec := floor((v_time - v_secs) * 1000);
-- Generate random bytes for the rest
v_random := gen_random_bytes(10);
-- Build timestamp bytes (6 bytes = 48 bits)
v_timestamp := decode(lpad(to_hex(v_secs * 1000 + v_msec), 12, '0'), 'hex');
-- Combine: timestamp (6 bytes) + random (10 bytes)
v_uuid := v_timestamp || v_random;
-- Set version 7 (0111) in byte 6 (bits 4-7)
v_uuid := set_byte(v_uuid, 6, (get_byte(v_uuid, 6) & 15) | 112);
-- Set variant RFC 4122 (10xx) in byte 8
v_uuid := set_byte(v_uuid, 8, (get_byte(v_uuid, 8) & 63) | 128);
RETURN encode(v_uuid, 'hex')::uuid;
END;
$$ LANGUAGE plpgsql VOLATILE;
-- Comment
COMMENT ON FUNCTION uuid_generate_v7() IS 'Generates a UUID v7 (time-ordered) - RFC 9562 compliant';
-- Test the function
DO $$
BEGIN
RAISE NOTICE 'UUID v7 test: %', uuid_generate_v7();
END $$;