Skip to content

Commit

Permalink
Try to sort search paths by psql version
Browse files Browse the repository at this point in the history
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
  • Loading branch information
romuald committed Jan 11, 2018
1 parent c81ded4 commit ec5d8e4
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/testing/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,36 @@
import subprocess
from glob import glob
from contextlib import closing
from distutils.version import LooseVersion

from testing.common.database import (
Database, DatabaseFactory, get_path_of, SkipIfNotInstalledDecorator
)


__all__ = ['Postgresql', 'skipIfNotFound']


def sorted_paths(glob_pattern):
"""
Try to return search paths sorted by version number
(latest version first)
"""
values = glob(glob_pattern)
try:
idx = glob_pattern.index('*')

# -> [('9.4', '/usr/lib/postgresql/9.4')]
matches = [(LooseVersion(match[idx:]), match) for match in values]

return [path for _, path in sorted(matches, reverse=True)]
except Exception:
return values

SEARCH_PATHS = (['/usr/local/pgsql', '/usr/local'] +
glob('/usr/pgsql-*') + # for CentOS/RHEL
glob('/usr/lib/postgresql/*') + # for Debian/Ubuntu
glob('/opt/local/lib/postgresql*')) # for MacPorts
sorted_paths('/usr/pgsql-*') + # for CentOS/RHEL
sorted_paths('/usr/lib/postgresql/*') + # for Debian/Ubuntu
sorted_paths('/opt/local/lib/postgresql*')) # for MacPorts


class Postgresql(Database):
Expand Down

0 comments on commit ec5d8e4

Please sign in to comment.