Skip to content

Commit

Permalink
Fix logic for purge of recorder runs (#130378)
Browse files Browse the repository at this point in the history
* Fix logic for purge of recorder runs

* Make test more explicit

* Explicitly don't remove unclosed recorder runs in purge
  • Loading branch information
emontnemery authored Nov 26, 2024
1 parent ec8fe3d commit 44f90dc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/recorder/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
6 changes: 5 additions & 1 deletion tests/components/recorder/test_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/components/recorder/test_purge_v32_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 44f90dc

Please sign in to comment.