Model/repositories/unit_of_work.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

47 lines
1.6 KiB
Python

from __future__ import annotations
from abc import ABC, abstractmethod
from types import TracebackType
from typing import Optional
from repositories.property_baseline.property_baseline_repository import PropertyBaselineRepository
from repositories.epc.epc_repository import EpcRepository
from repositories.property.property_repository import PropertyRepository
from repositories.solar.solar_repository import SolarRepository
class UnitOfWork(ABC):
"""A single batch transaction across the DB-backed repos (ADR-0012).
A context manager that exposes the repos bound to one session. A stage runs
its whole batch inside one unit and calls ``commit()`` once; leaving the
block without committing — including via an exception — rolls back, so a
failed batch persists nothing and the subtask fails noisily.
The non-DB dependencies (EPC/Solar fetchers, the geospatial S3 repo, the
Rebaseliner) are *not* part of the unit — only transactional DB work is.
"""
property: PropertyRepository
epc: EpcRepository
solar: SolarRepository
property_baseline: PropertyBaselineRepository
@abstractmethod
def commit(self) -> None: ...
@abstractmethod
def rollback(self) -> None: ...
def __enter__(self) -> "UnitOfWork":
return self
def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc: Optional[BaseException],
tb: Optional[TracebackType],
) -> None:
# Roll back whatever was not explicitly committed (a no-op after a
# successful commit). All-or-nothing per batch.
self.rollback()