Model/repositories/property_baseline/property_baseline_postgres_repository.py
Khalim Conn-Kowlessar c3691d9af2 refactor(property-baseline): rename baseline → property_baseline aggregate (PR #1139 review)
Wholesale rename of the Baseline aggregate to PropertyBaseline for clarity /
to disambiguate from baselines that appear elsewhere in Modelling. Scoped to
this aggregate only — the distinct Rebaselining term (rebaseline_reason,
StubRebaseliner, RebaselineNotImplemented) is deliberately untouched.

- domain/baseline → domain/property_baseline; BaselinePerformance →
  PropertyBaselinePerformance.
- repositories/baseline → repositories/property_baseline; BaselineRepository
  / BaselinePostgresRepository → PropertyBaseline*.
- orchestration/baseline_orchestrator.py → property_baseline_orchestrator.py;
  BaselineOrchestrator → PropertyBaselineOrchestrator. BaselineStage →
  PropertyBaselineStage.
- infrastructure/postgres: baseline_performance_table.py →
  property_baseline_performance_table.py; table `baseline_performance` →
  `property_baseline_performance`; Model renamed.
- UnitOfWork attribute `.baseline` → `.property_baseline`.
- Docs: ADR-0004 references + migration doc (renamed to
  property-baseline-performance-table.md) updated.

CONTEXT.md glossary term ("Baseline Performance") left as-is pending a
ubiquitous-language call (raised on the PR). 123 tests pass; pyright strict
clean (only the unrelated pre-existing moto import errors remain).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-01 14:54:59 +00:00

43 lines
1.7 KiB
Python

from __future__ import annotations
from typing import Optional
from sqlmodel import Session, col, delete, select
from domain.property_baseline.property_baseline_performance import PropertyBaselinePerformance
from infrastructure.postgres.property_baseline_performance_table import (
PropertyBaselinePerformanceModel,
)
from repositories.property_baseline.property_baseline_repository import PropertyBaselineRepository
class PropertyBaselinePostgresRepository(PropertyBaselineRepository):
"""Maps PropertyBaselinePerformance to/from the ``property_baseline_performance`` table."""
def __init__(self, session: Session) -> None:
self._session = session
def save(self, baseline: PropertyBaselinePerformance, property_id: int) -> int:
# Idempotent on property_id: a re-run (or re-score) replaces the row
# rather than hitting the unique constraint (ADR-0012).
self._session.exec( # type: ignore[call-overload]
delete(PropertyBaselinePerformanceModel).where(
col(PropertyBaselinePerformanceModel.property_id) == property_id
)
)
row = PropertyBaselinePerformanceModel.from_domain(baseline, property_id)
self._session.add(row)
self._session.flush()
if row.id is None:
raise ValueError("property_baseline_performance row did not receive an id")
return row.id
def get_for_property(
self, property_id: int
) -> Optional[PropertyBaselinePerformance]:
row = self._session.exec(
select(PropertyBaselinePerformanceModel).where(
PropertyBaselinePerformanceModel.property_id == property_id
)
).first()
return row.to_domain() if row is not None else None