Files
ISSSTELEON/Persistance
Roberto Treviño Cervantes c159a69284 preliminary release
2026-10-29 12:16:56 -06:00
..
2026-10-29 12:16:56 -06:00
2026-09-21 10:24:06 -06:00
2026-09-21 10:38:47 -06:00

Persistance

Database-agnostic SQLAlchemy/Alembic persistence layer for the three screening data marts — CaCu, CaMa and TEA — built on top of the shared schema in ../Core. SQLite for now; swapping dialects is a DATABASE_URL change, not a code change.

Structure

  • persistance/settings.py — one env var per domain (CACU_DATABASE_URL, CAMA_DATABASE_URL, TEA_DATABASE_URL), since a single process now serves three independent databases.
  • persistance/db.py — engine/session factory per domain (get_cacu_session, get_cama_session, get_tea_session).
  • persistance/repository.py — the generic Repository class.
  • persistance/cacu/, persistance/cama/, persistance/tea/ — one Alembic environment per domain, plus that domain's module-level repository instances (see below).

The repository pattern

Repository is one generic class parametrized by a Core model — get(id) / list(limit, offset, **filters) / count(**filters). Every table gets one instance, wired up once per domain in that domain's __init__.py:

# persistance/cacu/__init__.py
paciente_repository = Repository(get_cacu_session, Paciente)
ordenes_estudio_repository = Repository(get_cacu_session, OrdenesEstudio)
citologia_cervical_repository = Repository(get_cacu_session, CitologiaCervical)
biopsia_cervical_repository = Repository(get_cacu_session, BiopsiaCervical)
nota_medica_repository = Repository(get_cacu_session, NotaMedica)

These module-level instances are the standard way every consumer reads data — WebApi's read routes and the models package's training code both import the same instances from persistance.cacu / persistance.cama / persistance.tea, so they're reading through the exact same API, not two different paths to the same tables.

Adding a repository for a new table

Two lines in that domain's __init__.py: import the model from Core, instantiate Repository(get_<domain>_session, TheModel). Nothing else to write — get/list/count come for free.

When a table needs more than get/list/count

Don't bolt a one-off method onto the generic Repository, and don't write a standalone function that opens its own session outside the pattern. Subclass Repository for just that one table:

# persistance/cacu/repository.py
from sqlmodel import select

from core.cacu.models import CitologiaCervical
from persistance.repository import Repository


class CitologiaCervicalRepository(Repository[CitologiaCervical]):
    def list_vph_positivo_confiable(self, umbral_confianza: float = 0.9):
        with self._session_factory() as session:
            statement = select(CitologiaCervical).where(
                CitologiaCervical.cit_vph_resultado == "POSITIVO",
                CitologiaCervical.cit_vph_ocr_confianza >= umbral_confianza,
            )
            return list(session.exec(statement))

Then swap the plain instantiation for the subclass in persistance/cacu/__init__.py:

citologia_cervical_repository = CitologiaCervicalRepository(get_cacu_session, CitologiaCervical)

Every existing caller keeps working unchanged (.get()/.list()/.count() are inherited); the new method is just available too: citologia_cervical_repository.list_vph_positivo_confiable(). Only subclass the tables that actually need it — most are fine with the generic Repository as-is.

Migrations

Three independent Alembic environments, one per domain:

uv run alembic -c persistance/cacu/alembic.ini upgrade head
uv run alembic -c persistance/cama/alembic.ini upgrade head
uv run alembic -c persistance/tea/alembic.ini upgrade head

Each domain's migration history is a single, tool-generated create_table migration against that domain's Core metadata. If the schema changes, delete and regenerate — never hand-edit a migration file.