diff --git a/homeassistant/components/recorder/util.py b/homeassistant/components/recorder/util.py index 8658c7b3677e6bdc5498782ae5f7286d31c63e5c..b4bbce271303aa293581c0769f1db2323830f55f 100644 --- a/homeassistant/components/recorder/util.py +++ b/homeassistant/components/recorder/util.py @@ -7,7 +7,6 @@ from datetime import timedelta import functools import logging import os -import re import time from typing import TYPE_CHECKING @@ -49,7 +48,7 @@ MIN_VERSION_MARIA_DB = AwesomeVersion("10.3.0", AwesomeVersionStrategy.SIMPLEVER MIN_VERSION_MARIA_DB_ROWNUM = AwesomeVersion("10.2.0", AwesomeVersionStrategy.SIMPLEVER) MIN_VERSION_MYSQL = AwesomeVersion("8.0.0", AwesomeVersionStrategy.SIMPLEVER) MIN_VERSION_MYSQL_ROWNUM = AwesomeVersion("5.8.0", AwesomeVersionStrategy.SIMPLEVER) -MIN_VERSION_PGSQL = AwesomeVersion(120000, AwesomeVersionStrategy.BUILDVER) +MIN_VERSION_PGSQL = AwesomeVersion("12.0", AwesomeVersionStrategy.SIMPLEVER) MIN_VERSION_SQLITE = AwesomeVersion("3.32.1", AwesomeVersionStrategy.SIMPLEVER) MIN_VERSION_SQLITE_ROWNUM = AwesomeVersion("3.25.0", AwesomeVersionStrategy.SIMPLEVER) @@ -324,20 +323,6 @@ def _extract_version_from_server_response(server_response): return None -def _pgsql_numerical_version_to_string(version_num): - """Convert numerical PostgreSQL version to string.""" - if version_num < 100000: - major = version_num // 10000 - minor = version_num % 10000 // 100 - patch = version_num % 100 - return f"{major}.{minor}.{patch}" - - # version 10+ - major = version_num // 10000 - patch = version_num % 10000 - return f"{major}.{patch}" - - def setup_connection_for_dialect( instance, dialect_name, dbapi_connection, first_connection ): @@ -379,7 +364,7 @@ def setup_connection_for_dialect( result = query_on_connection(dbapi_connection, "SELECT VERSION()") version_string = result[0][0] version = _extract_version_from_server_response(version_string) - is_maria_db = re.search("MariaDb", version_string, re.IGNORECASE) + is_maria_db = "mariadb" in version_string.lower() if is_maria_db: if version and version < MIN_VERSION_MARIA_DB_ROWNUM: @@ -403,21 +388,12 @@ def setup_connection_for_dialect( elif dialect_name == "postgresql": if first_connection: # server_version_num was added in 2006 - result = query_on_connection(dbapi_connection, "SHOW server_version_num") + result = query_on_connection(dbapi_connection, "SHOW server_version") version_string = result[0][0] - try: - version = AwesomeVersion( - version_string, AwesomeVersionStrategy.BUILDVER - ) - except AwesomeVersionException: - version = None + version = _extract_version_from_server_response(version_string) if not version or version < MIN_VERSION_PGSQL: - if version: - version_string = _pgsql_numerical_version_to_string(int(version)) _warn_unsupported_version( - version_string, - "PostgreSQL", - _pgsql_numerical_version_to_string(int(MIN_VERSION_PGSQL)), + version or version_string, "PostgreSQL", MIN_VERSION_PGSQL ) else: diff --git a/tests/components/recorder/test_util.py b/tests/components/recorder/test_util.py index ced614ee2fa0fe4b997f405223bce09240abd3e6..da88d17f02aff5ec5f01bb58a9f91e7455df1675 100644 --- a/tests/components/recorder/test_util.py +++ b/tests/components/recorder/test_util.py @@ -287,11 +287,11 @@ def test_supported_mysql(caplog, mysql_version): "pgsql_version,message", [ ( - "110013", - "Version 11.13 of PostgreSQL is not supported; minimum supported version is 12.0.", + "11.12 (Debian 11.12-1.pgdg100+1)", + "Version 11.12 of PostgreSQL is not supported; minimum supported version is 12.0.", ), ( - "90210", + "9.2.10", "Version 9.2.10 of PostgreSQL is not supported; minimum supported version is 12.0.", ), ( @@ -312,7 +312,7 @@ def test_warn_outdated_pgsql(caplog, pgsql_version, message): def fetchall_mock(): nonlocal execute_args - if execute_args[-1] == "SHOW server_version_num": + if execute_args[-1] == "SHOW server_version": return [[pgsql_version]] return None @@ -330,9 +330,7 @@ def test_warn_outdated_pgsql(caplog, pgsql_version, message): @pytest.mark.parametrize( "pgsql_version", - [ - (130000), - ], + ["14.0 (Debian 14.0-1.pgdg110+1)"], ) def test_supported_pgsql(caplog, pgsql_version): """Test setting up the connection for a supported PostgreSQL version.""" @@ -346,7 +344,7 @@ def test_supported_pgsql(caplog, pgsql_version): def fetchall_mock(): nonlocal execute_args - if execute_args[-1] == "SHOW server_version_num": + if execute_args[-1] == "SHOW server_version": return [[pgsql_version]] return None