Always keep the current recorder run when purging (#43733)

pull/43767/head
J. Nick Koston 2020-11-29 22:13:50 -10:00 committed by GitHub
parent 533f22bb76
commit 2cbb93be43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -68,6 +68,7 @@ def purge_old_data(instance, purge_days: int, repack: bool) -> bool:
deleted_rows = (
session.query(RecorderRuns)
.filter(RecorderRuns.start < purge_before)
.filter(RecorderRuns.run_id != instance.run_info.run_id)
.delete(synchronize_session=False)
)
_LOGGER.debug("Deleted %s recorder_runs", deleted_rows)

View File

@ -62,6 +62,22 @@ def test_purge_old_events(hass, hass_recorder):
assert events.count() == 2
def test_purge_old_recorder_runs(hass, hass_recorder):
"""Test deleting old recorder runs keeps current run."""
hass = hass_recorder()
_add_test_recorder_runs(hass)
# make sure we start with 7 recorder runs
with session_scope(hass=hass) as session:
recorder_runs = session.query(RecorderRuns)
assert recorder_runs.count() == 7
# run purge_old_data()
finished = purge_old_data(hass.data[DATA_INSTANCE], 0, repack=False)
assert finished
assert recorder_runs.count() == 1
def test_purge_method(hass, hass_recorder):
"""Test purge method."""
hass = hass_recorder()