Skip to content

Move sim's database code to the SQLAlchemy ORM #1097

Closed

Description

@anth-volk

What

Move every module that reads or writes database rows from SQLAlchemy Core to the SQLAlchemy ORM, so the rule sim.persistence.orm-only can go from ratcheted to enforced.

The rule says that code declares tables only as typed ORM mapped classes (DeclarativeBase, Mapped[...], mapped_column) and runs every statement through an ORM Session against those classes, never through Core Table objects or Connections. It lands ratcheted in #1090, with rules/baselines/orm-only.txt listing the 880 findings in today's code. A new finding fails CI, and each conversion removes its entries.

Plan

  1. Table models and sessions: both persistence libraries' Table(...) declarations become mapped classes on one declarative base per library, and engine.py hands out a sessionmaker. Temporary aliases (household_jobs = HouseholdJob.__table__) keep the Core stores working, and the comparison with the Public API's schema keeps passing.
  2. Service side, households: the household job and household report records and stores, and the Entrypoint's household services.
  3. Service side, economy and budget windows: reads, inputs, find-or-create, re-runs, shared rows and result models in policyengine-simulation-persistence.
  4. Modal side: every store in policyengine-stage12-persistence, and the executor's stage12_runtime/database_retry.py.
  5. Connections, readiness and finish: access.py and the Entrypoint's simulation_database.py; remove the aliases, empty the baseline, set the rule to enforced and remove its [rule.downgrade] section. Also drop the sqlmodel dependency that policyengine-fastapi keeps after Answer 500 when a household job's commit is unconfirmed, and move sim's database code to the SQLAlchemy ORM #1090 deletes its unused database.py.

Steps 2 to 4 depend only on step 1, so they can run in parallel. Step 5 closes this issue.

This conversion comes before the cliff work: #1096 and #1099 are rebased onto it and their database code is redone on mapped classes and Sessions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions