Skip to content

Commit

Permalink
Better handling of PostgreSQL 9 and lower versions
Browse files Browse the repository at this point in the history
  • Loading branch information
samdoran committed Apr 4, 2024
1 parent b4d04d7 commit 89e9340
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
11 changes: 6 additions & 5 deletions koku/masu/api/db_performance/db_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def get_pg_engine_version(self) -> str:
# 120001 --> ['12', '00', '01']
# 140011 --> ['14', '00', '11']
parts = list(map("".join, zip(*[iter(server_version_num)] * 2)))
parts = [part.lstrip("0") for part in parts]
parts = [int(part) for part in parts]

# The second part of the version number was only used in PastgreSQL < 10
# https://www.postgresql.org/support/versioning/
Expand All @@ -158,10 +158,11 @@ def get_pg_engine_version(self) -> str:
# 9.0.0, 9.1.2
# 10.1, 10.2
#
# parts[1] will be "" if it was "00" originally due to the lstrip("0") above
#
major = f"{parts[0]}.{parts[1]}" if parts[1] else parts[0]
minor = "0" if not parts[2] else parts[2]
major = parts[0]
if major < 10:
major = f"{parts[0]}.{parts[1]}"

minor = parts[2]

return f"{major}.{minor}"

Expand Down
10 changes: 7 additions & 3 deletions koku/masu/test/api/test_db_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ def test_get_db_version(self):
"""Test that the db engine version can be retrieved."""
# FIXME: Rewrite using parametrize when pytest is available
testcases = (
("090600", "9.6.0"),
("090000", "9.0.0"),
("090023", "9.0.23"),
("090100", "9.1.0"),
("090600", "9.6.0"),
("092600", "9.26.0"),
("100000", "10.0"),
("100010", "10.10"),
Expand All @@ -86,8 +88,10 @@ def test_get_db_version(self):
server_version_num = case[0]
expected = case[1]

with DBPerformanceStats("KOKU", CONFIGURATOR) as dbp:
with patch.object(dbp, "_execute") as mock_execute:
with (
DBPerformanceStats("KOKU", CONFIGURATOR) as dbp,
patch.object(dbp, "_execute") as mock_execute,
):
mock_execute.return_value.fetchone.return_value = {"server_version_num": server_version_num}
ver = dbp.get_pg_engine_version()

Expand Down

0 comments on commit 89e9340

Please sign in to comment.