Ensure recorder runs are cleaned up during purge (#36989)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
b4489e132c
commit
ad6315be5c
2 changed files with 45 additions and 4 deletions
|
@ -6,7 +6,7 @@ from sqlalchemy.exc import SQLAlchemyError
|
|||
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .models import Events, States
|
||||
from .models import Events, RecorderRuns, States
|
||||
from .util import session_scope
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -33,6 +33,13 @@ def purge_old_data(instance, purge_days, repack):
|
|||
)
|
||||
_LOGGER.debug("Deleted %s events", deleted_rows)
|
||||
|
||||
deleted_rows = (
|
||||
session.query(RecorderRuns)
|
||||
.filter(RecorderRuns.start < purge_before)
|
||||
.delete(synchronize_session=False)
|
||||
)
|
||||
_LOGGER.debug("Deleted %s recorder_runs", deleted_rows)
|
||||
|
||||
if repack:
|
||||
# Execute sqlite or postgresql vacuum command to free up space on disk
|
||||
if instance.engine.driver in ("pysqlite", "postgresql"):
|
||||
|
@ -41,7 +48,7 @@ def purge_old_data(instance, purge_days, repack):
|
|||
# Optimize mysql / mariadb tables to free up space on disk
|
||||
elif instance.engine.driver == "mysqldb":
|
||||
_LOGGER.debug("Optimizing SQL DB to free space")
|
||||
instance.engine.execute("OPTIMIZE TABLE states, events")
|
||||
instance.engine.execute("OPTIMIZE TABLE states, events, recorder_runs")
|
||||
|
||||
except SQLAlchemyError as err:
|
||||
_LOGGER.warning("Error purging history: %s.", err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue