M8-R03B: keep meter swap recompute in caller transaction

This commit is contained in:
2026-08-24 06:45:31 +02:00
parent 631b14e2ec
commit 3beeb5a461
4 changed files with 105 additions and 19 deletions
+10 -4
View File
@@ -665,7 +665,9 @@ def compute_closed_periods(session: Session) -> int:
# ---------------------------------------------------------------------------
def recompute_range(session: Session, start: datetime, end: datetime) -> int:
def recompute_range(
session: Session, start: datetime, end: datetime, *, commit: bool = True
) -> int:
"""Recompute (overwrite) all 15-minute periods in ``[start, end)``.
This is the *explicit opt-in* path for recovering from:
@@ -686,8 +688,11 @@ def recompute_range(session: Session, start: datetime, end: datetime) -> int:
Parameters
----------
session:
Active SQLAlchemy session. The function commits after all periods
have been processed.
Active SQLAlchemy session.
commit:
When true (the default), commit after all periods have been processed.
Callers composing this recompute with other writes may pass false and
own the surrounding transaction themselves.
start:
Inclusive start datetime (floored to the nearest quarter-hour internally).
end:
@@ -724,7 +729,8 @@ def recompute_range(session: Session, start: datetime, end: datetime) -> int:
)
t0 += timedelta(minutes=_PERIOD_MINUTES)
session.commit()
if commit:
session.commit()
logger.info(
"recompute_range(%s, %s): wrote %d period(s).",
start.isoformat(),