Skip to content

Commit ec5d8e4

Browse files
committed
Try to sort search paths by psql version
Previously the search paths for the psql binaries was not sorted. So multiples postgresql versions on the same system may be used (usually the older one was choosen first) Try to return the paths sorted by postgresql version (most recent first) for each glob search path
1 parent c81ded4 commit ec5d8e4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/testing/postgresql.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,36 @@
1919
import subprocess
2020
from glob import glob
2121
from contextlib import closing
22+
from distutils.version import LooseVersion
2223

2324
from testing.common.database import (
2425
Database, DatabaseFactory, get_path_of, SkipIfNotInstalledDecorator
2526
)
2627

27-
2828
__all__ = ['Postgresql', 'skipIfNotFound']
2929

30+
31+
def sorted_paths(glob_pattern):
32+
"""
33+
Try to return search paths sorted by version number
34+
(latest version first)
35+
36+
"""
37+
values = glob(glob_pattern)
38+
try:
39+
idx = glob_pattern.index('*')
40+
41+
# -> [('9.4', '/usr/lib/postgresql/9.4')]
42+
matches = [(LooseVersion(match[idx:]), match) for match in values]
43+
44+
return [path for _, path in sorted(matches, reverse=True)]
45+
except Exception:
46+
return values
47+
3048
SEARCH_PATHS = (['/usr/local/pgsql', '/usr/local'] +
31-
glob('/usr/pgsql-*') + # for CentOS/RHEL
32-
glob('/usr/lib/postgresql/*') + # for Debian/Ubuntu
33-
glob('/opt/local/lib/postgresql*')) # for MacPorts
49+
sorted_paths('/usr/pgsql-*') + # for CentOS/RHEL
50+
sorted_paths('/usr/lib/postgresql/*') + # for Debian/Ubuntu
51+
sorted_paths('/opt/local/lib/postgresql*')) # for MacPorts
3452

3553

3654
class Postgresql(Database):

0 commit comments

Comments
 (0)