Skip to content

Commit

Permalink
Fail recorder setup with unsupported dialect or version (#70888)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed May 18, 2022
1 parent f3c5828 commit 037f694
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
3 changes: 3 additions & 0 deletions homeassistant/components/recorder/core.py
Expand Up @@ -56,6 +56,7 @@
StatisticData,
StatisticMetaData,
StatisticsRuns,
UnsupportedDialect,
process_timestamp,
)
from .pool import POOL_SIZE, MutexPool, RecorderPool
Expand Down Expand Up @@ -606,6 +607,8 @@ def _setup_recorder(self) -> None | int:
try:
self._setup_connection()
return migration.get_schema_version(self.get_session)
except UnsupportedDialect:
break
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception(
"Error during connection setup: %s (retrying in %s seconds)",
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/recorder/models.py
Expand Up @@ -123,6 +123,10 @@ def result_processor(self, dialect, coltype): # type: ignore[no-untyped-def]
EVENT_ORIGIN_TO_IDX = {origin: idx for idx, origin in enumerate(EVENT_ORIGIN_ORDER)}


class UnsupportedDialect(Exception):
"""The dialect or its version is not supported."""


class Events(Base): # type: ignore[misc,valid-type]
"""Event history data."""

Expand Down
32 changes: 16 additions & 16 deletions homeassistant/components/recorder/util.py
Expand Up @@ -34,6 +34,7 @@
TABLE_SCHEMA_CHANGES,
TABLES_TO_CHECK,
RecorderRuns,
UnsupportedDialect,
process_timestamp,
)

Expand Down Expand Up @@ -328,29 +329,31 @@ def query_on_connection(dbapi_connection: Any, statement: str) -> Any:
return result


def _warn_unsupported_dialect(dialect_name: str) -> None:
def _fail_unsupported_dialect(dialect_name: str) -> None:
"""Warn about unsupported database version."""
_LOGGER.warning(
_LOGGER.error(
"Database %s is not supported; Home Assistant supports %s. "
"Starting with Home Assistant 2022.2 this will prevent the recorder from "
"starting. Please migrate your database to a supported software before then",
"Starting with Home Assistant 2022.6 this prevents the recorder from "
"starting. Please migrate your database to a supported software",
dialect_name,
"MariaDB ≥ 10.3, MySQL ≥ 8.0, PostgreSQL ≥ 12, SQLite ≥ 3.31.0",
)
raise UnsupportedDialect


def _warn_unsupported_version(
def _fail_unsupported_version(
server_version: str, dialect_name: str, minimum_version: str
) -> None:
"""Warn about unsupported database version."""
_LOGGER.warning(
_LOGGER.error(
"Version %s of %s is not supported; minimum supported version is %s. "
"Starting with Home Assistant 2022.2 this will prevent the recorder from "
"starting. Please upgrade your database software before then",
"Starting with Home Assistant 2022.6 this prevents the recorder from "
"starting. Please upgrade your database software",
server_version,
dialect_name,
minimum_version,
)
raise UnsupportedDialect


def _extract_version_from_server_response(
Expand Down Expand Up @@ -398,9 +401,6 @@ def setup_connection_for_dialect(
first_connection: bool,
) -> None:
"""Execute statements needed for dialect connection."""
# Returns False if the the connection needs to be setup
# on the next connection, returns True if the connection
# never needs to be setup again.
if dialect_name == SupportedDialect.SQLITE:
if first_connection:
old_isolation = dbapi_connection.isolation_level
Expand All @@ -419,7 +419,7 @@ def setup_connection_for_dialect(
False
)
if not version or version < MIN_VERSION_SQLITE:
_warn_unsupported_version(
_fail_unsupported_version(
version or version_string, "SQLite", MIN_VERSION_SQLITE
)

Expand Down Expand Up @@ -453,7 +453,7 @@ def setup_connection_for_dialect(
False
)
if not version or version < MIN_VERSION_MARIA_DB:
_warn_unsupported_version(
_fail_unsupported_version(
version or version_string, "MariaDB", MIN_VERSION_MARIA_DB
)
else:
Expand All @@ -462,7 +462,7 @@ def setup_connection_for_dialect(
False
)
if not version or version < MIN_VERSION_MYSQL:
_warn_unsupported_version(
_fail_unsupported_version(
version or version_string, "MySQL", MIN_VERSION_MYSQL
)

Expand All @@ -473,12 +473,12 @@ def setup_connection_for_dialect(
version_string = result[0][0]
version = _extract_version_from_server_response(version_string)
if not version or version < MIN_VERSION_PGSQL:
_warn_unsupported_version(
_fail_unsupported_version(
version or version_string, "PostgreSQL", MIN_VERSION_PGSQL
)

else:
_warn_unsupported_dialect(dialect_name)
_fail_unsupported_dialect(dialect_name)


def end_incomplete_runs(session: Session, start_time: datetime) -> None:
Expand Down
42 changes: 24 additions & 18 deletions tests/components/recorder/test_util.py
Expand Up @@ -14,7 +14,7 @@
from homeassistant.components import recorder
from homeassistant.components.recorder import history, util
from homeassistant.components.recorder.const import DATA_INSTANCE, SQLITE_URL_PREFIX
from homeassistant.components.recorder.models import RecorderRuns
from homeassistant.components.recorder.models import RecorderRuns, UnsupportedDialect
from homeassistant.components.recorder.util import (
end_incomplete_runs,
is_second_sunday,
Expand Down Expand Up @@ -168,10 +168,8 @@ def _last_run_was_recently_clean(cursor):
@pytest.mark.parametrize(
"mysql_version, db_supports_row_number",
[
("10.2.0-MariaDB", True),
("10.1.0-MariaDB", False),
("5.8.0", True),
("5.7.0", False),
("10.3.0-MariaDB", True),
("8.0.0", True),
],
)
def test_setup_connection_for_dialect_mysql(mysql_version, db_supports_row_number):
Expand Down Expand Up @@ -207,8 +205,7 @@ def _make_cursor_mock(*_):
@pytest.mark.parametrize(
"sqlite_version, db_supports_row_number",
[
("3.25.0", True),
("3.24.0", False),
("3.31.0", True),
],
)
def test_setup_connection_for_dialect_sqlite(sqlite_version, db_supports_row_number):
Expand Down Expand Up @@ -255,8 +252,7 @@ def _make_cursor_mock(*_):
@pytest.mark.parametrize(
"sqlite_version, db_supports_row_number",
[
("3.25.0", True),
("3.24.0", False),
("3.31.0", True),
],
)
def test_setup_connection_for_dialect_sqlite_zero_commit_interval(
Expand Down Expand Up @@ -319,7 +315,7 @@ def _make_cursor_mock(*_):
),
],
)
def test_warn_outdated_mysql(caplog, mysql_version, message):
def test_fail_outdated_mysql(caplog, mysql_version, message):
"""Test setting up the connection for an outdated mysql version."""
instance_mock = MagicMock(_db_supports_row_number=True)
execute_args = []
Expand All @@ -340,7 +336,10 @@ def _make_cursor_mock(*_):

dbapi_connection = MagicMock(cursor=_make_cursor_mock)

util.setup_connection_for_dialect(instance_mock, "mysql", dbapi_connection, True)
with pytest.raises(UnsupportedDialect):
util.setup_connection_for_dialect(
instance_mock, "mysql", dbapi_connection, True
)

assert message in caplog.text

Expand Down Expand Up @@ -395,7 +394,7 @@ def _make_cursor_mock(*_):
),
],
)
def test_warn_outdated_pgsql(caplog, pgsql_version, message):
def test_fail_outdated_pgsql(caplog, pgsql_version, message):
"""Test setting up the connection for an outdated PostgreSQL version."""
instance_mock = MagicMock(_db_supports_row_number=True)
execute_args = []
Expand All @@ -416,9 +415,10 @@ def _make_cursor_mock(*_):

dbapi_connection = MagicMock(cursor=_make_cursor_mock)

util.setup_connection_for_dialect(
instance_mock, "postgresql", dbapi_connection, True
)
with pytest.raises(UnsupportedDialect):
util.setup_connection_for_dialect(
instance_mock, "postgresql", dbapi_connection, True
)

assert message in caplog.text

Expand Down Expand Up @@ -472,7 +472,7 @@ def _make_cursor_mock(*_):
),
],
)
def test_warn_outdated_sqlite(caplog, sqlite_version, message):
def test_fail_outdated_sqlite(caplog, sqlite_version, message):
"""Test setting up the connection for an outdated sqlite version."""
instance_mock = MagicMock(_db_supports_row_number=True)
execute_args = []
Expand All @@ -493,7 +493,10 @@ def _make_cursor_mock(*_):

dbapi_connection = MagicMock(cursor=_make_cursor_mock)

util.setup_connection_for_dialect(instance_mock, "sqlite", dbapi_connection, True)
with pytest.raises(UnsupportedDialect):
util.setup_connection_for_dialect(
instance_mock, "sqlite", dbapi_connection, True
)

assert message in caplog.text

Expand Down Expand Up @@ -544,7 +547,10 @@ def test_warn_unsupported_dialect(caplog, dialect, message):
instance_mock = MagicMock()
dbapi_connection = MagicMock()

util.setup_connection_for_dialect(instance_mock, dialect, dbapi_connection, True)
with pytest.raises(UnsupportedDialect):
util.setup_connection_for_dialect(
instance_mock, dialect, dbapi_connection, True
)

assert message in caplog.text

Expand Down

0 comments on commit 037f694

Please sign in to comment.