fix(postgres): generar UUIDv7 en Python para bypasear función SQL rota
El DEFAULT de correspondencia_interna.id llama a collina_canon.uuidv7() (función de Quetzalcóatl). En algunos builds de PostgreSQL revienta con "operator does not exist: bit | integer" por hacer bit-shifts sobre bit strings sin cast a int. Generamos el UUIDv7 en Python siguiendo RFC 9562 (48 bits ts ms + versión 7 + 12 bits rand + variant 0b10 + 62 bits rand) y lo pasamos como $1 en el INSERT. Conserva la propiedad de orden temporal que motivó usar v7 sobre v4 (locality en B-tree y en particiones mensuales). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b4d7bfdf6a
commit
2f49384abc
2 files changed
+82
-5
No files matched your search
@@ -0,0 +1,40 @@
|
||||
"""Tests del generador uuidv7 local (bypassa la función SQL de Quetzalcóatl)."""
|
||||
import time
|
||||
from uuid import UUID
|
||||
|
||||
from valeria.postgres import uuidv7
|
||||
|
||||
|
||||
def test_uuidv7_devuelve_UUID():
|
||||
u = uuidv7()
|
||||
assert isinstance(u, UUID)
|
||||
|
||||
|
||||
def test_uuidv7_version_y_variante_correctas():
|
||||
u = uuidv7()
|
||||
# Version 7 en bits 76-79
|
||||
assert (u.int >> 76) & 0xF == 0x7
|
||||
# Variant RFC 4122 (0b10) en bits 62-63
|
||||
assert (u.int >> 62) & 0x3 == 0x2
|
||||
|
||||
|
||||
def test_uuidv7_timestamp_es_unix_ms_reciente():
|
||||
antes = int(time.time() * 1000)
|
||||
u = uuidv7()
|
||||
despues = int(time.time() * 1000)
|
||||
ts = u.int >> 80 # 48 bits altos
|
||||
assert antes - 1 <= ts <= despues + 1
|
||||
|
||||
|
||||
def test_uuidv7_es_monotonico_creciente():
|
||||
"""Dos llamadas separadas en el tiempo deben ordenar por el timestamp."""
|
||||
a = uuidv7()
|
||||
time.sleep(0.005)
|
||||
b = uuidv7()
|
||||
assert b.int > a.int
|
||||
|
||||
|
||||
def test_uuidv7_unicos_en_misma_milesima():
|
||||
"""Varias llamadas en el mismo ms tienen 74 bits aleatorios → no colisionan."""
|
||||
ids = {uuidv7() for _ in range(1000)}
|
||||
assert len(ids) == 1000
|
||||
Reference in new issue
Block a user