Simplify recorder PgSQL version checks (#58533)

This commit is contained in:
Erik Montnemery 2021-10-27 16:05:40 +02:00 committed by GitHub
parent a835917311
commit dfa50a842a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 37 deletions

View File

@ -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:

View File

@ -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