1 """Purge repack helper."""
3 from __future__
import annotations
6 from typing
import TYPE_CHECKING
8 from sqlalchemy
import text
10 from .const
import SupportedDialect
11 from .db_schema
import ALL_TABLES
14 from .
import Recorder
16 _LOGGER = logging.getLogger(__name__)
20 """Repack based on engine type."""
21 assert instance.engine
is not None
22 dialect_name = instance.engine.dialect.name
25 if dialect_name == SupportedDialect.SQLITE:
26 _LOGGER.debug(
"Vacuuming SQL DB to free space")
27 with instance.engine.connect()
as conn:
28 conn.execute(text(
"VACUUM"))
33 if dialect_name == SupportedDialect.POSTGRESQL:
34 _LOGGER.debug(
"Vacuuming SQL DB to free space")
35 with instance.engine.connect().execution_options(
36 isolation_level=
"AUTOCOMMIT"
38 conn.execute(text(
"VACUUM"))
43 if dialect_name == SupportedDialect.MYSQL:
44 _LOGGER.debug(
"Optimizing SQL DB to free space")
45 with instance.engine.connect()
as conn:
46 conn.execute(text(f
"OPTIMIZE TABLE {','.join(ALL_TABLES)}"))
None repack_database(Recorder instance)