Fix logic for purge of recorder runs (#130378)

* Fix logic for purge of recorder runs

* Make test more explicit

* Explicitly don't remove unclosed recorder runs in purge
pull/131688/head
Erik Montnemery 2024-11-26 07:47:47 +01:00 committed by GitHub
parent ec8fe3db4e
commit 44f90dca0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 3 deletions

View File

@ -608,7 +608,8 @@ def delete_recorder_runs_rows(
"""Delete recorder_runs rows."""
return lambda_stmt(
lambda: delete(RecorderRuns)
.filter(RecorderRuns.start < purge_before)
.filter(RecorderRuns.end.is_not(None))
.filter(RecorderRuns.end < purge_before)
.filter(RecorderRuns.run_id != current_run_id)
.execution_options(synchronize_session=False)
)

View File

@ -352,6 +352,8 @@ async def test_purge_old_recorder_runs(
with session_scope(hass=hass) as session:
recorder_runs = session.query(RecorderRuns)
assert recorder_runs.count() == 7
# Make sure we have a run that is not closed
assert sum(run.end is None for run in recorder_runs) == 1
purge_before = dt_util.utcnow()
@ -376,7 +378,9 @@ async def test_purge_old_recorder_runs(
with session_scope(hass=hass) as session:
recorder_runs = session.query(RecorderRuns)
assert recorder_runs.count() == 1
assert recorder_runs.count() == 3
# Make sure we did not purge the unclosed run
assert sum(run.end is None for run in recorder_runs) == 1
async def test_purge_old_statistics_runs(

View File

@ -347,7 +347,7 @@ async def test_purge_old_recorder_runs(
with session_scope(hass=hass) as session:
recorder_runs = session.query(RecorderRuns)
assert recorder_runs.count() == 1
assert recorder_runs.count() == 3
async def test_purge_old_statistics_runs(