Prepare for recorder purge to be active by default (#11976)

This commit is contained in:
Anders Melchiorsen 2018-01-30 12:41:33 +01:00 committed by Pascal Vizeli
parent ec1c395f09
commit d7017f2138
2 changed files with 11 additions and 3 deletions

View file

@ -76,9 +76,9 @@ FILTER_SCHEMA = vol.Schema({
CONFIG_SCHEMA = vol.Schema({
DOMAIN: FILTER_SCHEMA.extend({
vol.Inclusive(CONF_PURGE_KEEP_DAYS, 'purge'):
vol.Optional(CONF_PURGE_KEEP_DAYS):
vol.All(vol.Coerce(int), vol.Range(min=1)),
vol.Inclusive(CONF_PURGE_INTERVAL, 'purge'):
vol.Optional(CONF_PURGE_INTERVAL, default=1):
vol.All(vol.Coerce(int), vol.Range(min=1)),
vol.Optional(CONF_DB_URL): cv.string,
})
@ -122,6 +122,12 @@ def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
keep_days = conf.get(CONF_PURGE_KEEP_DAYS)
purge_interval = conf.get(CONF_PURGE_INTERVAL)
if keep_days is None:
_LOGGER.warning(
"From version 0.64.0 the 'recorder' component will by default "
"purge data older than 10 days. To keep data longer you must "
"configure a 'purge_keep_days' value.")
db_url = conf.get(CONF_DB_URL, None)
if not db_url:
db_url = DEFAULT_URL.format(
@ -162,6 +168,7 @@ class Recorder(threading.Thread):
self.hass = hass
self.keep_days = keep_days
self.purge_interval = purge_interval
self.did_vacuum = False
self.queue = queue.Queue() # type: Any
self.recording_start = dt_util.utcnow()
self.db_url = uri

View file

@ -55,11 +55,12 @@ def purge_old_data(instance, purge_days):
# Execute sqlite vacuum command to free up space on disk
_LOGGER.debug("DB engine driver: %s", instance.engine.driver)
if instance.engine.driver == 'pysqlite':
if instance.engine.driver == 'pysqlite' and not instance.did_vacuum:
from sqlalchemy import exc
_LOGGER.info("Vacuuming SQLite to free space")
try:
instance.engine.execute("VACUUM")
instance.did_vacuum = True
except exc.OperationalError as err:
_LOGGER.error("Error vacuuming SQLite: %s.", err)