Optimize recorder MySQL tables when repacking (#36762)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Alex van den Hoogen 2020-06-23 02:28:03 +02:00 committed by GitHub
parent 16f1ef5a44
commit ee816ed3dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,10 +33,15 @@ def purge_old_data(instance, purge_days, repack):
)
_LOGGER.debug("Deleted %s events", deleted_rows)
# Execute sqlite vacuum command to free up space on disk
if repack and instance.engine.driver in ("pysqlite", "postgresql"):
_LOGGER.debug("Vacuuming SQL DB to free space")
instance.engine.execute("VACUUM")
if repack:
# Execute sqlite or postgresql vacuum command to free up space on disk
if instance.engine.driver in ("pysqlite", "postgresql"):
_LOGGER.debug("Vacuuming SQL DB to free space")
instance.engine.execute("VACUUM")
# 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")
except SQLAlchemyError as err:
_LOGGER.warning("Error purging history: %s.", err)