Opened 6 years ago
Closed 6 years ago
#13032 closed defect (fixed)
Postgres server version reported incorrectly for psql >= 10
Reported by: | Ryan J Ollos | Owned by: | Ryan J Ollos |
---|---|---|---|
Priority: | normal | Milestone: | 1.3.3 |
Component: | general | Version: | |
Severity: | normal | Keywords: | |
Cc: | Branch: | ||
Release Notes: |
Fixed PostgreSQL server version reported incorrectly for version ≥ 10. |
||
API Changes: | |||
Internal Changes: |
Description (last modified by )
The About Trac page on my dev machine is showing server: 10.0.4
. It should display 10.4:
$ psql -c "SELECT version();" trac tracuser version --------------------------------------------------------------------------------------------------------------- PostgreSQL 10.4 on x86_64-apple-darwin17.5.0, compiled by Apple LLVM version 9.1.0 (clang-902.0.39.1), 64-bit (1 row)
I've done only a cursory investigation, but it looks like the versioning policy has changed. So for version ≥ 10 we should parse 100004
to tuple (10, 4)
: tags/trac-1.3.2/trac/db/postgres_backend.py@:454:#L452.
-
trac/db/postgres_backend.py
diff --git a/trac/db/postgres_backend.py b/trac/db/postgres_backend.py index c2e94becc..2647f9b9d 100644
a b def _quote(identifier): 99 99 def _version_tuple(ver): 100 100 101 101 if ver: 102 # Extract 9.1.23 from 90123.103 102 def get_digit(version, n): 104 103 return version / 10 ** (2 * n) % 100 105 return get_digit(ver, 2), get_digit(ver, 1), get_digit(ver, 0) 104 first_digit = get_digit(ver, 2) 105 if first_digit >= 10: 106 # Extract (10, 4) from 100004. 107 return first_digit, get_digit(ver, 0) 108 else: 109 # Extract (9, 1, 23) from 90123. 110 return first_digit, get_digit(ver, 1), get_digit(ver, 0) 106 111 107 112 108 113 def _version_string(ver):
A few other minor changes may also be needed.
Attachments (0)
Change History (6)
comment:1 by , 6 years ago
Description: | modified (diff) |
---|
follow-up: 3 comment:2 by , 6 years ago
comment:3 by , 6 years ago
Replying to anonymous:
or just:
major, minor = ver / 10000, ver % 10000 if major >= 10: # Extract (10, 4) from 100004. return major, minor else: # Extract (9, 1, 23) from 90123. return major, minor / 100, minor % 100
The latter looks good to me.
Also, I think we should use _version_string()
rather than directly formatting at the following:
-
trac/db/postgres_backend.py
diff --git a/trac/db/postgres_backend.py b/trac/db/postgres_backend.py index c2e94becc..bb3922f32 100644
a b class PostgreSQLConnector(Component): 146 146 cnx = PostgreSQLConnection(path, log, user, password, host, port, 147 147 params) 148 148 if not self.required: 149 server_ver = _version_string(cnx.server_version) 150 client_ver = _version_string(self._client_version) 149 151 if cnx.server_version < min_postgresql_version: 150 152 error = _( 151 153 "PostgreSQL version is %(version)s. Minimum required " 152 "version is %(min_version)s.", 153 version='%d.%d.%d' % cnx.server_version, 154 min_version='%d.%d.%d' % min_postgresql_version) 154 "version is %(min_version)s.", version=server_ver, 155 min_version=_version_string(min_postgresql_version)) 155 156 raise TracError(error) 156 self._postgresql_version = \ 157 'server: %s, client: %s' \ 158 % (_version_string(cnx.server_version), 159 _version_string(self._client_version)) 157 self._postgresql_version = 'server: %s, client: %s' % \ 158 (server_ver, client_ver) 160 159 self.required = True 161 160 return cnx 162 161
comment:4 by , 6 years ago
For Python 3 compatibility it should use from __future__ import division
and //
instead of /
.
comment:5 by , 6 years ago
We could use divmod
rather than incompatible /
between python 2 and 3.
-
trac/db/postgres_backend.py
diff --git a/trac/db/postgres_backend.py b/trac/db/postgres_backend.py index c2e94becc..67491704e 100644
a b def _quote(identifier): 97 97 98 98 99 99 def _version_tuple(ver): 100 101 100 if ver: 102 # Extract 9.1.23 from 90123. 103 def get_digit(version, n): 104 return version / 10 ** (2 * n) % 100 105 return get_digit(ver, 2), get_digit(ver, 1), get_digit(ver, 0) 101 major, minor = divmod(ver, 10000) 102 if major >= 10: 103 # Extract 10.4 from 100004. 104 return major, minor 105 else: 106 # Extract 9.1.23 from 90123. 107 minor, patch = divmod(minor, 100) 108 return major, minor, patch 106 109 107 110 108 111 def _version_string(ver): -
trac/db/tests/postgres_test.py
diff --git a/trac/db/tests/postgres_test.py b/trac/db/tests/postgres_test.py index 88199d1d0..619cad948 100644
a b from trac.db.api import DatabaseManager 19 19 from trac.db.schema import Table, Column, Index 20 20 from trac.test import EnvironmentStub, get_dburi 21 21 try: 22 from trac.db.postgres_backend import PostgreSQLConnector, assemble_pg_dsn 22 from trac.db.postgres_backend import (PostgreSQLConnector, _version_tuple, 23 assemble_pg_dsn) 23 24 except pkg_resources.DistributionNotFound: 24 PostgreSQLConnector = assemble_pg_dsn = None25 PostgreSQLConnector = _version_tuple = assemble_pg_dsn = None 25 26 26 27 27 28 class PostgresTableCreationSQLTest(unittest.TestCase): … … class PostgresTableCreationSQLTest(unittest.TestCase): 142 143 ["dbname='/trac'", r"password='pa\\\'ss'", "user='user'"], 143 144 sorted(assemble_pg_dsn('/trac', 'user', r"pa\'ss").split(' '))) 144 145 146 def test_version_tuple(self): 147 self.assertEqual((9, 0, 0), _version_tuple(90000)) 148 self.assertEqual((9, 1, 23), _version_tuple(90123)) 149 self.assertEqual((9, 98, 97), _version_tuple(99897)) 150 self.assertEqual((10, 0), _version_tuple(100000)) 151 self.assertEqual((10, 4), _version_tuple(100004)) 152 self.assertEqual((10, 9999), _version_tuple(109999)) 153 self.assertEqual((11, 0), _version_tuple(110000)) 154 145 155 146 156 class PostgresTableAlterationSQLTest(unittest.TestCase): 147 157 def setUp(self):
comment:6 by , 6 years ago
Release Notes: | modified (diff) |
---|---|
Resolution: | → fixed |
Status: | assigned → closed |
Thanks for suggestions. Committed in r16623.
psycopg docs on server_version:
There:
In the (unlikely) case of
100100
that would parse incorrectly to tuple(10, 0)
instead of(10, 100)
.or just: