Model/repositories/scenario/scenario_postgres_repository.py
Khalim Conn-Kowlessar c18968ba3c refactor(modelling): consolidate scenario + installed_measure into the subpackage
Move the scenario and installed_measure tables into
infrastructure/postgres/modelling/ as full-parity SQLModel definitions
(ScenarioModel, InstalledMeasureModel + MeasureType), completing the cluster
consolidation. backend/app/db/models/recommendations.py is now a pure
re-export shim.

ScenarioModel.goal is the PortfolioGoal enum (legacy planning branches on it),
sourced from domain/modelling/portfolio_goal.py; the repo's to_domain maps it to
its value string, so domain Scenario.goal is now the value ("Increasing EPC")
consistent with the orchestrator's check — fixing the latent name-vs-value
inconsistency the old str column masked (the scenario repo test stored the enum
*name*). Parity columns are nullable (mirror convention; live NOT-NULLs owned by
Drizzle).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 22:52:35 +00:00

31 lines
1.2 KiB
Python

from __future__ import annotations
from sqlmodel import Session, col, select
from domain.modelling.scenario import Scenario
from infrastructure.postgres.modelling import ScenarioModel
from repositories.scenario.scenario_repository import ScenarioRepository
class ScenarioPostgresRepository(ScenarioRepository):
"""Reads the live ``scenario`` table (via the ``ScenarioModel`` mirror) and
maps each row to the thin domain ``Scenario`` the Modelling stage uses
(ADR-0017). The legacy file-path / aggregate columns are not read."""
def __init__(self, session: Session) -> None:
self._session = session
def get_many(self, scenario_ids: list[int]) -> list[Scenario]:
rows = self._session.exec(
select(ScenarioModel).where(col(ScenarioModel.id).in_(scenario_ids))
).all()
by_id: dict[int, ScenarioModel] = {
row.id: row for row in rows if row.id is not None
}
scenarios: list[Scenario] = []
for scenario_id in scenario_ids:
row = by_id.get(scenario_id)
if row is None:
raise ValueError(f"no scenario with id {scenario_id}")
scenarios.append(row.to_domain())
return scenarios