Fix recorder with MSSQL (#46678)
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
parent
46e593485e
commit
a060acc2b1
7 changed files with 196 additions and 108 deletions
35
homeassistant/components/recorder/repack.py
Normal file
35
homeassistant/components/recorder/repack.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
"""Purge repack helper."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from . import Recorder
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def repack_database(instance: Recorder) -> None:
|
||||
"""Repack based on engine type."""
|
||||
|
||||
# Execute sqlite command to free up space on disk
|
||||
if instance.engine.dialect.name == "sqlite":
|
||||
_LOGGER.debug("Vacuuming SQL DB to free space")
|
||||
instance.engine.execute("VACUUM")
|
||||
return
|
||||
|
||||
# Execute postgresql vacuum command to free up space on disk
|
||||
if instance.engine.dialect.name == "postgresql":
|
||||
_LOGGER.debug("Vacuuming SQL DB to free space")
|
||||
with instance.engine.connect().execution_options(
|
||||
isolation_level="AUTOCOMMIT"
|
||||
) as conn:
|
||||
conn.execute("VACUUM")
|
||||
return
|
||||
|
||||
# Optimize mysql / mariadb tables to free up space on disk
|
||||
if instance.engine.dialect.name == "mysql":
|
||||
_LOGGER.debug("Optimizing SQL DB to free space")
|
||||
instance.engine.execute("OPTIMIZE TABLE states, events, recorder_runs")
|
||||
return
|
Loading…
Add table
Add a link
Reference in a new issue