Persistance: a revision per domain creating cat_sistema_fuente and cat_mapeo

How: cacu f89e1f081998, cama f82b64f43abd and tea 6d772e9a342f, on each chain's head; plain types, check
constraints, foreign keys and a unique key that every engine accepts. Verified upgrade, `alembic check`,
downgrade and upgrade again on SQLite.

Implications: both tables start empty; the catalog step seeds the TOML's own mappings.
This commit is contained in:
rockdrigo
2026-11-13 15:29:07 -06:00
parent 66c4ba5c5f
commit 74eb5a4a4c
3 changed files with 198 additions and 0 deletions
@@ -0,0 +1,66 @@
"""source terminology
`cat_sistema_fuente` (who sends the data) and `cat_mapeo` (how each source writes the catalog's codes). Both start
empty; the `catalogs` step seeds the catalog's own mappings, and the application adds the rest.
Revision ID: f89e1f081998
Revises: b4f2d8c01e23
Create Date: 2026-11-13 18:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
import core.indicadores.tipos
# revision identifiers, used by Alembic.
revision = 'f89e1f081998'
down_revision = 'b4f2d8c01e23'
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table('cat_sistema_fuente',
sa.Column('sistema_fuente_id', sa.String(length=64), nullable=False),
sa.Column('etiqueta', sa.String(), nullable=False),
sa.Column('fuente_tabla', sa.String(length=64), nullable=True),
sa.Column('vigente_desde', sa.Date(), nullable=False),
sa.Column('vigente_hasta', sa.Date(), nullable=True),
sa.CheckConstraint('vigente_hasta IS NULL OR vigente_hasta >= vigente_desde', name='ck_cat_sistema_fuente_vigencia'),
sa.PrimaryKeyConstraint('sistema_fuente_id')
)
op.create_table('cat_mapeo',
sa.Column('mapeo_id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('sistema_fuente_id', sa.String(length=64), nullable=False),
sa.Column('clase', sa.String(length=16), nullable=False),
sa.Column('forma', sa.String(length=255), nullable=False),
sa.Column('forma_normalizada', sa.String(length=255), nullable=False),
sa.Column('estudio_id', sa.String(length=64), nullable=True),
sa.Column('resultado_id', sa.String(length=128), nullable=True),
sa.Column('valor_id', sa.String(length=64), nullable=True),
sa.Column('vigente_desde', sa.Date(), nullable=False),
sa.Column('vigente_hasta', sa.Date(), nullable=True),
sa.Column('registrado_por', sa.String(length=64), nullable=False),
sa.Column('registrado_en', core.indicadores.tipos.FechaHoraUTC(), nullable=False),
sa.Column('motivo', sa.String(), nullable=True),
sa.CheckConstraint("clase IN ('estudio', 'etiqueta', 'valor')", name='ck_cat_mapeo_clase'),
sa.CheckConstraint("(clase = 'estudio' AND estudio_id IS NOT NULL AND resultado_id IS NULL AND valor_id IS NULL)"
" OR (clase = 'etiqueta' AND estudio_id IS NULL AND resultado_id IS NOT NULL AND valor_id IS NULL)"
" OR (clase = 'valor' AND estudio_id IS NULL AND resultado_id IS NOT NULL AND valor_id IS NOT NULL)",
name='ck_cat_mapeo_destino'),
sa.CheckConstraint('vigente_hasta IS NULL OR vigente_hasta >= vigente_desde', name='ck_cat_mapeo_vigencia'),
sa.ForeignKeyConstraint(['sistema_fuente_id'], ['cat_sistema_fuente.sistema_fuente_id'], ),
sa.ForeignKeyConstraint(['estudio_id'], ['cat_estudio.estudio_id'], ),
sa.ForeignKeyConstraint(['resultado_id'], ['cat_resultado.resultado_id'], ),
sa.ForeignKeyConstraint(['resultado_id', 'valor_id'], ['cat_valor.resultado_id', 'cat_valor.valor_id'], name='fk_cat_mapeo_valor'),
sa.PrimaryKeyConstraint('mapeo_id'),
sa.UniqueConstraint('sistema_fuente_id', 'clase', 'resultado_id', 'estudio_id', 'forma_normalizada', 'vigente_desde', name='uq_cat_mapeo')
)
op.create_index('ix_cat_mapeo_busqueda', 'cat_mapeo', ['sistema_fuente_id', 'resultado_id', 'forma_normalizada'], unique=False)
def downgrade() -> None:
op.drop_index('ix_cat_mapeo_busqueda', table_name='cat_mapeo')
op.drop_table('cat_mapeo')
op.drop_table('cat_sistema_fuente')
@@ -0,0 +1,66 @@
"""source terminology
`cat_sistema_fuente` (who sends the data) and `cat_mapeo` (how each source writes the catalog's codes). Both start
empty; the `catalogs` step seeds the catalog's own mappings, and the application adds the rest.
Revision ID: f82b64f43abd
Revises: c5a3e9d12f34
Create Date: 2026-11-13 18:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
import core.indicadores.tipos
# revision identifiers, used by Alembic.
revision = 'f82b64f43abd'
down_revision = 'c5a3e9d12f34'
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table('cat_sistema_fuente',
sa.Column('sistema_fuente_id', sa.String(length=64), nullable=False),
sa.Column('etiqueta', sa.String(), nullable=False),
sa.Column('fuente_tabla', sa.String(length=64), nullable=True),
sa.Column('vigente_desde', sa.Date(), nullable=False),
sa.Column('vigente_hasta', sa.Date(), nullable=True),
sa.CheckConstraint('vigente_hasta IS NULL OR vigente_hasta >= vigente_desde', name='ck_cat_sistema_fuente_vigencia'),
sa.PrimaryKeyConstraint('sistema_fuente_id')
)
op.create_table('cat_mapeo',
sa.Column('mapeo_id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('sistema_fuente_id', sa.String(length=64), nullable=False),
sa.Column('clase', sa.String(length=16), nullable=False),
sa.Column('forma', sa.String(length=255), nullable=False),
sa.Column('forma_normalizada', sa.String(length=255), nullable=False),
sa.Column('estudio_id', sa.String(length=64), nullable=True),
sa.Column('resultado_id', sa.String(length=128), nullable=True),
sa.Column('valor_id', sa.String(length=64), nullable=True),
sa.Column('vigente_desde', sa.Date(), nullable=False),
sa.Column('vigente_hasta', sa.Date(), nullable=True),
sa.Column('registrado_por', sa.String(length=64), nullable=False),
sa.Column('registrado_en', core.indicadores.tipos.FechaHoraUTC(), nullable=False),
sa.Column('motivo', sa.String(), nullable=True),
sa.CheckConstraint("clase IN ('estudio', 'etiqueta', 'valor')", name='ck_cat_mapeo_clase'),
sa.CheckConstraint("(clase = 'estudio' AND estudio_id IS NOT NULL AND resultado_id IS NULL AND valor_id IS NULL)"
" OR (clase = 'etiqueta' AND estudio_id IS NULL AND resultado_id IS NOT NULL AND valor_id IS NULL)"
" OR (clase = 'valor' AND estudio_id IS NULL AND resultado_id IS NOT NULL AND valor_id IS NOT NULL)",
name='ck_cat_mapeo_destino'),
sa.CheckConstraint('vigente_hasta IS NULL OR vigente_hasta >= vigente_desde', name='ck_cat_mapeo_vigencia'),
sa.ForeignKeyConstraint(['sistema_fuente_id'], ['cat_sistema_fuente.sistema_fuente_id'], ),
sa.ForeignKeyConstraint(['estudio_id'], ['cat_estudio.estudio_id'], ),
sa.ForeignKeyConstraint(['resultado_id'], ['cat_resultado.resultado_id'], ),
sa.ForeignKeyConstraint(['resultado_id', 'valor_id'], ['cat_valor.resultado_id', 'cat_valor.valor_id'], name='fk_cat_mapeo_valor'),
sa.PrimaryKeyConstraint('mapeo_id'),
sa.UniqueConstraint('sistema_fuente_id', 'clase', 'resultado_id', 'estudio_id', 'forma_normalizada', 'vigente_desde', name='uq_cat_mapeo')
)
op.create_index('ix_cat_mapeo_busqueda', 'cat_mapeo', ['sistema_fuente_id', 'resultado_id', 'forma_normalizada'], unique=False)
def downgrade() -> None:
op.drop_index('ix_cat_mapeo_busqueda', table_name='cat_mapeo')
op.drop_table('cat_mapeo')
op.drop_table('cat_sistema_fuente')
@@ -0,0 +1,66 @@
"""source terminology
`cat_sistema_fuente` (who sends the data) and `cat_mapeo` (how each source writes the catalog's codes). Both start
empty; the `catalogs` step seeds the catalog's own mappings, and the application adds the rest.
Revision ID: 6d772e9a342f
Revises: f2b6d1e8c934
Create Date: 2026-11-13 18:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
import core.indicadores.tipos
# revision identifiers, used by Alembic.
revision = '6d772e9a342f'
down_revision = 'f2b6d1e8c934'
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table('cat_sistema_fuente',
sa.Column('sistema_fuente_id', sa.String(length=64), nullable=False),
sa.Column('etiqueta', sa.String(), nullable=False),
sa.Column('fuente_tabla', sa.String(length=64), nullable=True),
sa.Column('vigente_desde', sa.Date(), nullable=False),
sa.Column('vigente_hasta', sa.Date(), nullable=True),
sa.CheckConstraint('vigente_hasta IS NULL OR vigente_hasta >= vigente_desde', name='ck_cat_sistema_fuente_vigencia'),
sa.PrimaryKeyConstraint('sistema_fuente_id')
)
op.create_table('cat_mapeo',
sa.Column('mapeo_id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('sistema_fuente_id', sa.String(length=64), nullable=False),
sa.Column('clase', sa.String(length=16), nullable=False),
sa.Column('forma', sa.String(length=255), nullable=False),
sa.Column('forma_normalizada', sa.String(length=255), nullable=False),
sa.Column('estudio_id', sa.String(length=64), nullable=True),
sa.Column('resultado_id', sa.String(length=128), nullable=True),
sa.Column('valor_id', sa.String(length=64), nullable=True),
sa.Column('vigente_desde', sa.Date(), nullable=False),
sa.Column('vigente_hasta', sa.Date(), nullable=True),
sa.Column('registrado_por', sa.String(length=64), nullable=False),
sa.Column('registrado_en', core.indicadores.tipos.FechaHoraUTC(), nullable=False),
sa.Column('motivo', sa.String(), nullable=True),
sa.CheckConstraint("clase IN ('estudio', 'etiqueta', 'valor')", name='ck_cat_mapeo_clase'),
sa.CheckConstraint("(clase = 'estudio' AND estudio_id IS NOT NULL AND resultado_id IS NULL AND valor_id IS NULL)"
" OR (clase = 'etiqueta' AND estudio_id IS NULL AND resultado_id IS NOT NULL AND valor_id IS NULL)"
" OR (clase = 'valor' AND estudio_id IS NULL AND resultado_id IS NOT NULL AND valor_id IS NOT NULL)",
name='ck_cat_mapeo_destino'),
sa.CheckConstraint('vigente_hasta IS NULL OR vigente_hasta >= vigente_desde', name='ck_cat_mapeo_vigencia'),
sa.ForeignKeyConstraint(['sistema_fuente_id'], ['cat_sistema_fuente.sistema_fuente_id'], ),
sa.ForeignKeyConstraint(['estudio_id'], ['cat_estudio.estudio_id'], ),
sa.ForeignKeyConstraint(['resultado_id'], ['cat_resultado.resultado_id'], ),
sa.ForeignKeyConstraint(['resultado_id', 'valor_id'], ['cat_valor.resultado_id', 'cat_valor.valor_id'], name='fk_cat_mapeo_valor'),
sa.PrimaryKeyConstraint('mapeo_id'),
sa.UniqueConstraint('sistema_fuente_id', 'clase', 'resultado_id', 'estudio_id', 'forma_normalizada', 'vigente_desde', name='uq_cat_mapeo')
)
op.create_index('ix_cat_mapeo_busqueda', 'cat_mapeo', ['sistema_fuente_id', 'resultado_id', 'forma_normalizada'], unique=False)
def downgrade() -> None:
op.drop_index('ix_cat_mapeo_busqueda', table_name='cat_mapeo')
op.drop_table('cat_mapeo')
op.drop_table('cat_sistema_fuente')