summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlly Cope <olly@ollycope.com>2018-01-29 12:20:59 +0000
committerOlly Cope <olly@ollycope.com>2018-01-29 12:20:59 +0000
commit464d4c134c5d1eb30abe2f56648ea3d9ba217f2b (patch)
tree7cd5f450081d5c62afb9cd9d5515cba1d6239bde
parentabcf6e77c89b3de95a54057910fbcecf359ddf8d (diff)
downloadyoyo-464d4c134c5d1eb30abe2f56648ea3d9ba217f2b.tar.gz
postgresql: connect using keyword arguments in preference to a dsn
As well as being cleaner, this resolves a bug I've seen in the test suite, where connections are incorrectly made to the default postgres database.
-rw-r--r--yoyo/backends.py13
-rw-r--r--yoyo/tests/test_connections.py4
2 files changed, 8 insertions, 9 deletions
diff --git a/yoyo/backends.py b/yoyo/backends.py
index 6af931d..e1a32ee 100644
--- a/yoyo/backends.py
+++ b/yoyo/backends.py
@@ -446,17 +446,16 @@ class PostgresqlBackend(DatabaseBackend):
driver_module = 'psycopg2'
def connect(self, dburi):
- connargs = []
+ connect_args = {'dbname': dburi.database}
if dburi.username is not None:
- connargs.append('user=%s' % dburi.username)
+ connect_args['user'] = dburi.username
if dburi.password is not None:
- connargs.append('password=%s' % dburi.password)
+ connect_args['password'] = dburi.password
if dburi.port is not None:
- connargs.append('port=%d' % dburi.port)
+ connect_args['port'] = dburi.port
if dburi.hostname is not None:
- connargs.append('host=%s' % dburi.hostname)
- connargs.append('dbname=%s' % dburi.database)
- return self.driver.connect(' '.join(connargs))
+ connect_args['host'] = dburi.hostname
+ return self.driver.connect(**connect_args)
@contextmanager
def disable_transactions(self):
diff --git a/yoyo/tests/test_connections.py b/yoyo/tests/test_connections.py
index f3b7440..9e5808c 100644
--- a/yoyo/tests/test_connections.py
+++ b/yoyo/tests/test_connections.py
@@ -77,8 +77,8 @@ def test_connections(import_module):
db='northwind', foo='bar')),
(backends.SQLiteBackend, 'sqlite3', call('northwind')),
(backends.PostgresqlBackend, 'psycopg2',
- call('user=scott password=tiger port=42 '
- 'host=db.example.org dbname=northwind')),
+ call(user='scott', password='tiger', port=42,
+ host='db.example.org', dbname='northwind')),
]