diff --git a/repositories/plan/plan_postgres_repository.py b/repositories/plan/plan_postgres_repository.py index aa1e30157..21540523a 100644 --- a/repositories/plan/plan_postgres_repository.py +++ b/repositories/plan/plan_postgres_repository.py @@ -26,8 +26,9 @@ class PlanPostgresRepository(PlanRepository): A re-run INSERTs a fresh Plan rather than deleting the prior one (the cascade delete was slow); when the new Plan is the default it demotes any prior - default Plan for the same (property_id, scenario_id) to ``is_default=False``, - so readers can select the current Plan via ``is_default=True``.""" + default Plan for the same property — across all Scenarios — to + ``is_default=False``, so readers can select the property's one current Plan + via ``is_default=True``.""" def __init__(self, session: Session) -> None: self._session = session @@ -67,17 +68,17 @@ class PlanPostgresRepository(PlanRepository): return [] # Demote prior default Plans for every property in the batch that is - # receiving a new default Plan — one UPDATE for the whole batch. + # receiving a new default Plan — one UPDATE for the whole batch, + # across ALL scenarios: readers select a property's default by + # portfolio + is_default alone, so the invariant is one default per + # property. Scenario-scoped demotion left a promoted first Plan + # (non-default scenario) defaulted forever alongside the default + # scenario's later Plan (#1490). default_pids = [r.property_id for r in requests if r.is_default] if default_pids: - # scenario_id is uniform per batch (one scenario per SQS message). - scenario_id = requests[0].scenario_id self._session.exec( # type: ignore[call-overload] update(PlanModel) - .where( - col(PlanModel.property_id).in_(default_pids), - col(PlanModel.scenario_id) == scenario_id, - ) + .where(col(PlanModel.property_id).in_(default_pids)) .values(is_default=False) ) diff --git a/repositories/plan/plan_repository.py b/repositories/plan/plan_repository.py index 149a88618..15cafced1 100644 --- a/repositories/plan/plan_repository.py +++ b/repositories/plan/plan_repository.py @@ -43,7 +43,8 @@ class PlanRepository(ABC): ) -> int: """Persist ``plan`` and return its Plan id. Keeps prior Plans for ``(property_id, scenario_id)`` as history; when ``is_default`` is True, - demotes those prior Plans to ``is_default=False``.""" + demotes the property's prior default Plan — whatever its Scenario — to + ``is_default=False`` (one default Plan per property).""" ... @abstractmethod