feat: replace UUID v4 with UUID v7 across backend

Define uuid_generate_v7() in the first migration and switch all table
primary key defaults to it. Remove the uuid-ossp extension dependency.
Update refresh token and request ID generation in Go code to use
uuid.NewV7() from the existing google/uuid v1.6.0 library.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-02-27 13:19:55 +02:00
parent 92d40618cd
commit ea4a6301ea
9 changed files with 43 additions and 12 deletions

View File

@@ -1,5 +1,35 @@
-- +goose Up
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Generate UUID v7 (time-ordered, millisecond precision).
-- Structure: 48-bit unix_ts_ms | 4-bit version (0111) | 12-bit rand_a | 2-bit variant (10) | 62-bit rand_b
CREATE OR REPLACE FUNCTION uuid_generate_v7()
RETURNS uuid
AS $$
DECLARE
unix_ts_ms bytea;
uuid_bytes bytea;
BEGIN
-- 48-bit Unix timestamp in milliseconds.
-- int8send produces 8 big-endian bytes; skip the first 2 zero bytes to get 6.
unix_ts_ms = substring(int8send(floor(extract(epoch from clock_timestamp()) * 1000)::bigint) from 3);
-- Use a random v4 UUID as the source of random bits for rand_a and rand_b.
uuid_bytes = uuid_send(gen_random_uuid());
-- Overwrite bytes 05 with the timestamp (positions 16 in 1-indexed bytea).
uuid_bytes = overlay(uuid_bytes placing unix_ts_ms from 1 for 6);
-- Set version nibble (bits 4851) to 0111 (7).
uuid_bytes = set_bit(uuid_bytes, 48, 0);
uuid_bytes = set_bit(uuid_bytes, 49, 1);
uuid_bytes = set_bit(uuid_bytes, 50, 1);
uuid_bytes = set_bit(uuid_bytes, 51, 1);
-- Variant bits (6465) stay at 10 as inherited from gen_random_uuid().
RETURN encode(uuid_bytes, 'hex')::uuid;
END
$$ LANGUAGE plpgsql VOLATILE;
CREATE TYPE user_plan AS ENUM ('free', 'paid');
CREATE TYPE user_gender AS ENUM ('male', 'female');
@@ -7,7 +37,7 @@ CREATE TYPE user_goal AS ENUM ('lose', 'maintain', 'gain');
CREATE TYPE activity_level AS ENUM ('low', 'moderate', 'high');
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
firebase_uid VARCHAR(128) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL DEFAULT '',
@@ -47,3 +77,4 @@ DROP TYPE IF EXISTS activity_level;
DROP TYPE IF EXISTS user_goal;
DROP TYPE IF EXISTS user_gender;
DROP TYPE IF EXISTS user_plan;
DROP FUNCTION IF EXISTS uuid_generate_v7();