Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mariadb version parsing #97

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions pleskdistup/common/src/mariadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import typing

from . import dist, log
import re


def is_version_larger(left: str, right: str) -> bool:
Expand Down Expand Up @@ -56,6 +57,16 @@ def _extract_from_mysql_util(self, util_output: str):
self.minor = int(minor_part)
self.patch = int(patch_part)

def _extract_from_mariadb_util(self, util_output: str):
# String example: "mariadb from 11.6.2-MariaDB, client 15.2 for Linux (x86_64) using EditLine wrapper"
match = re.search(r"mariadb from (\d+)\.(\d+)\.(\d+)-MariaDB", util_output)
if not match:
raise ValueError(f"Cannot extract mariadb version from '{util_output}'")
major_part, minor_part, patch_part = match.groups()
self.major = int(major_part)
self.minor = int(minor_part)
self.patch = int(patch_part)

def __init__(self, to_extract: str):
"""Initialize a version object."""
self.major = 0
Expand All @@ -66,8 +77,10 @@ def __init__(self, to_extract: str):
self._extract_from_version_str(to_extract)
elif "Distrib" in to_extract:
self._extract_from_mysql_util(to_extract)
elif to_extract.startswith("mariadb"):
self._extract_from_mariadb_util(to_extract)
else:
raise ValueError(f"Cannot extract php version from '{to_extract}'")
raise ValueError(f"Cannot extract mariadb version from '{to_extract}'")

def __str__(self):
"""Return a string representation of a PHPVersion object."""
Expand All @@ -88,8 +101,9 @@ def get_installed_mariadb_version() -> MariaDBVersion:
if not utility:
raise RuntimeError("Unable to find mariadb or mysql utility")
out = subprocess.check_output([utility, "--version"], universal_newlines=True)
log.debug("Detected mariadb version is: {version}".format(version=out.split("Distrib ")[1].split(",")[0].split("-")[0]))
return MariaDBVersion(out)
result = MariaDBVersion(out)
log.debug(f"Detected mariadb version is: {result}")
return result


def get_mariadb_config_file_path() -> str:
Expand Down
22 changes: 14 additions & 8 deletions pleskdistup/common/tests/mariadbtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@

class MariaDBVersionsTests(unittest.TestCase):
def test_parse_simple(self):
php = mariadb.MariaDBVersion("10.6.12")
self.assertEqual(php.major, 10)
self.assertEqual(php.minor, 6)
self.assertEqual(php.patch, 12)
mariadb_ver = mariadb.MariaDBVersion("10.6.12")
self.assertEqual(mariadb_ver.major, 10)
self.assertEqual(mariadb_ver.minor, 6)
self.assertEqual(mariadb_ver.patch, 12)

def test_parse_utility_output(self):
php = mariadb.MariaDBVersion("mysql Ver 15.1 Distrib 10.6.12-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper")
self.assertEqual(php.major, 10)
self.assertEqual(php.minor, 6)
self.assertEqual(php.patch, 12)
mariadb_ver = mariadb.MariaDBVersion("mysql Ver 15.1 Distrib 10.6.12-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper")
ukablan-wpc marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(mariadb_ver.major, 10)
self.assertEqual(mariadb_ver.minor, 6)
self.assertEqual(mariadb_ver.patch, 12)

def test_parse_utility_output_no_distrib(self):
mariadb_ver = mariadb.MariaDBVersion("mariadb from 11.6.2-MariaDB, client 15.2 for Linux (x86_64) using EditLine wrapper")
self.assertEqual(mariadb_ver.major, 11)
self.assertEqual(mariadb_ver.minor, 6)
self.assertEqual(mariadb_ver.patch, 2)

def test_parse_wrong_string(self):
with self.assertRaises(ValueError):
Expand Down
4 changes: 3 additions & 1 deletion pleskdistup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,11 @@ def do_convert(
if locale.getpreferredencoding(False).lower() != "utf-8":
printerr(messages.ENCODING_INCONSISTENCY_ERROR_MESSAGE)

handle_error(str(e), logfile_path, util_name, options.status_flag_path, options.phase, upgrader)
full_error_message = f"Conversion failed due to encoding problem: {e}\nTrace: {traceback.format_exc()}"
handle_error(full_error_message, logfile_path, util_name, options.status_flag_path, options.phase, upgrader)
return pleskdistup.convert.ConvertResult(success=False, reboot_requested=False)
except Exception as e:
full_error_message = f"Conversion failed: {e}\nTrace: {traceback.format_exc()}"
handle_error(str(e), logfile_path, util_name, options.status_flag_path, options.phase, upgrader)
return pleskdistup.convert.ConvertResult(success=False, reboot_requested=False)

Expand Down
Loading