mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
Slice 3c.5. `PropertyPostgresRepository` takes an injected `SpatialRepository` and hydrates `Property.planning_restrictions` by UPRN (bulk in `get_many`, single in `get`). A UPRN with no cached row — or a property with no UPRN — defaults to unrestricted, matching legacy `empty_spatial_df` (ADR-0020). This closes the loop: Ingestion caches the protections, Modelling reads them off the Property to gate solid-wall EWI/IWI (ADR-0019). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
71 lines
2.6 KiB
Python
71 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from types import TracebackType
|
|
from typing import Optional
|
|
|
|
from sqlmodel import Session
|
|
|
|
from repositories.property_baseline.property_baseline_postgres_repository import (
|
|
PropertyBaselinePostgresRepository,
|
|
)
|
|
from repositories.epc.epc_postgres_repository import EpcPostgresRepository
|
|
from repositories.plan.plan_postgres_repository import PlanPostgresRepository
|
|
from repositories.product.product_postgres_repository import (
|
|
ProductPostgresRepository,
|
|
)
|
|
from repositories.property.property_postgres_repository import (
|
|
PropertyPostgresRepository,
|
|
)
|
|
from repositories.scenario.scenario_postgres_repository import (
|
|
ScenarioPostgresRepository,
|
|
)
|
|
from repositories.solar.solar_postgres_repository import SolarPostgresRepository
|
|
from repositories.spatial.spatial_postgres_repository import SpatialPostgresRepository
|
|
from repositories.unit_of_work import UnitOfWork
|
|
|
|
|
|
class PostgresUnitOfWork(UnitOfWork):
|
|
"""Postgres-backed Unit of Work: one ``Session``, all repos bound to it.
|
|
|
|
Built from a session factory (a module-scoped engine + sessionmaker in
|
|
production, ADR-0012) so the connection pool is reused across warm Lambda
|
|
invocations. The session is opened on ``__enter__`` and closed on
|
|
``__exit__``; a fresh instance is one single-use unit.
|
|
"""
|
|
|
|
def __init__(self, session_factory: Callable[[], Session]) -> None:
|
|
self._session_factory = session_factory
|
|
|
|
def __enter__(self) -> "PostgresUnitOfWork":
|
|
self._session = self._session_factory()
|
|
epc_repo = EpcPostgresRepository(self._session)
|
|
spatial_repo = SpatialPostgresRepository(self._session)
|
|
self.property = PropertyPostgresRepository(
|
|
self._session, epc_repo, spatial_repo
|
|
)
|
|
self.epc = epc_repo
|
|
self.solar = SolarPostgresRepository(self._session)
|
|
self.spatial = spatial_repo
|
|
self.property_baseline = PropertyBaselinePostgresRepository(self._session)
|
|
self.scenario = ScenarioPostgresRepository(self._session)
|
|
self.product = ProductPostgresRepository(self._session)
|
|
self.plan = PlanPostgresRepository(self._session)
|
|
return self
|
|
|
|
def __exit__(
|
|
self,
|
|
exc_type: Optional[type[BaseException]],
|
|
exc: Optional[BaseException],
|
|
tb: Optional[TracebackType],
|
|
) -> None:
|
|
try:
|
|
self._session.rollback()
|
|
finally:
|
|
self._session.close()
|
|
|
|
def commit(self) -> None:
|
|
self._session.commit()
|
|
|
|
def rollback(self) -> None:
|
|
self._session.rollback()
|