A new default plan demotes the prior default across scenarios 🟩

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Khalim Conn-Kowlessar 2026-07-07 16:42:47 +00:00
parent aa94e76d3d
commit 5c982beba4
2 changed files with 12 additions and 10 deletions

View file

@ -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)
)

View file

@ -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