Skip to content

Commit

Permalink
fix order of tries (#23)
Browse files Browse the repository at this point in the history
The previous implementation was using sets in order to avoid trying the same connection
option multiple times. This, however, resulted in a random order of trials.
Here, we make sure to always try the user-provided configuration first.
  • Loading branch information
ltalirz authored Feb 16, 2021
1 parent 8216784 commit f453385
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pgsu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ def determine_setup(self):

# Try to connect as a postgres superuser via psycopg2 (equivalent to using psql).
LOGGER.debug('Trying to connect via "psycopg2"...')
for pg_user in set([self.dsn.get('user'), None]):
for pg_user in unique_list([self.dsn.get('user'), None]):
dsn['user'] = pg_user
# First try the host specified (works if 'host' has setting 'trust' in pg_hba.conf).
# Then try local connection (works if 'local' has setting 'trust' in pg_hba.conf).
# Then try 'host' localhost via TCP/IP.
for pg_host in set([self.dsn.get('host'), None, 'localhost']):
for pg_host in unique_list([self.dsn.get('host'), None, 'localhost']): # yapf: disable
dsn['host'] = pg_host

if _try_connect_psycopg(**dsn):
Expand Down Expand Up @@ -374,3 +374,12 @@ def escape_for_bash(str_to_escape):
"""
escaped_quotes = str_to_escape.replace("'", """'"'"'""")
return "'{}'".format(escaped_quotes)


def unique_list(non_unique_list):
"""
Return list with unique subset of provided list, maintaining list order.
Source: https://stackoverflow.com/a/480227/1069467
"""
seen = set()
return [x for x in non_unique_list if not (x in seen or seen.add(x))]

0 comments on commit f453385

Please sign in to comment.