tenant.clave: String(32) con índice único. Al migrar: ningún tenant, nada; uno, recibe clave demo; dos, todo lo que cuelga del segundo pasa al primero por id, el segundo se borra y la fusión queda en audit_trail; más de dos, la migración falla sin tocar nada.
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
"""La migración 0003 amplía `audit_trail.accion` a 128 caracteres sin perder filas."""
|
|
|
|
from pathlib import Path
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import command
|
|
from alembic.config import Config
|
|
|
|
RAIZ = Path(__file__).resolve().parents[1]
|
|
ACCION_LARGA = "GET /expediente/" + "x" * 84
|
|
|
|
|
|
def _config(url: str) -> Config:
|
|
cfg = Config(str(RAIZ / "alembic.ini"))
|
|
cfg.set_main_option("sqlalchemy.url", url)
|
|
return cfg
|
|
|
|
|
|
def _largo_accion(conn: sa.Connection) -> int:
|
|
columnas = {c["name"]: c for c in sa.inspect(conn).get_columns("audit_trail")}
|
|
return columnas["accion"]["type"].length
|
|
|
|
|
|
def test_0003_amplia_accion_a_128_y_conserva_las_filas(tmp_path: Path) -> None:
|
|
url = f"sqlite:///{tmp_path / 'migracion.db'}"
|
|
cfg = _config(url)
|
|
command.upgrade(cfg, "0002")
|
|
|
|
engine = sa.create_engine(url)
|
|
with engine.begin() as conn:
|
|
assert _largo_accion(conn) == 32
|
|
conn.execute(
|
|
sa.text(
|
|
"INSERT INTO tenant (created_at, created_by, nombre, regimen, "
|
|
"actividad_vulnerable, uma_vigente, ventana_meses, fecha_corte) VALUES "
|
|
"('2026-10-06', 'pytest', 'Demo', 'LFPIORPI', 'Art. 17', 117.31, 6, '2026-08-31')"
|
|
)
|
|
)
|
|
conn.execute(
|
|
sa.text(
|
|
"INSERT INTO audit_trail (created_at, created_by, tenant_id, entidad, accion, "
|
|
"usuario, fecha) VALUES ('2026-10-06', 'pytest', 1, 'http', 'GET /inicio', "
|
|
"'pytest', '2026-10-06')"
|
|
)
|
|
)
|
|
|
|
command.upgrade(cfg, "0003")
|
|
|
|
with engine.begin() as conn:
|
|
assert _largo_accion(conn) == 128
|
|
assert conn.execute(sa.text("SELECT accion FROM audit_trail")).scalars().all() == [
|
|
"GET /inicio"
|
|
]
|
|
conn.execute(
|
|
sa.text(
|
|
"INSERT INTO audit_trail (created_at, created_by, tenant_id, entidad, accion, "
|
|
"usuario, fecha) VALUES ('2026-10-06', 'pytest', 1, 'http', :accion, "
|
|
"'pytest', '2026-10-06')"
|
|
),
|
|
{"accion": ACCION_LARGA},
|
|
)
|
|
guardadas = conn.execute(sa.text("SELECT accion FROM audit_trail")).scalars().all()
|
|
assert ACCION_LARGA in guardadas
|
|
|
|
command.downgrade(cfg, "0002")
|
|
with engine.connect() as conn:
|
|
assert _largo_accion(conn) == 32
|