Home Assistant Unofficial Reference 2024.12.1
mysql.py
Go to the documentation of this file.
1 """Provide info to system health for mysql."""
2 
3 from __future__ import annotations
4 
5 from sqlalchemy import text
6 from sqlalchemy.orm.session import Session
7 
8 
9 def db_size_bytes(session: Session, database_name: str) -> float | None:
10  """Get the mysql database size."""
11  size = session.execute(
12  text(
13  "SELECT ROUND(SUM(DATA_LENGTH + INDEX_LENGTH), 2) "
14  "FROM information_schema.TABLES WHERE "
15  "TABLE_SCHEMA=:database_name"
16  ),
17  {"database_name": database_name},
18  ).scalar()
19 
20  if size is None:
21  return None
22 
23  return float(size)
float|None db_size_bytes(Session session, str database_name)
Definition: mysql.py:9