From 57b4d4e6c0f5a21edc72b6e7f28937e10d550884 Mon Sep 17 00:00:00 2001 From: Arthur Skowronek Date: Wed, 11 Nov 2015 18:46:37 +0100 Subject: [PATCH 1/2] Add tox config to ease testing against multiple Python versions --- tox.ini | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tox.ini diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..bfbad6a --- /dev/null +++ b/tox.ini @@ -0,0 +1,6 @@ +[tox] +envlist = py{26,27,32,33,34} + + +[testenv] +commands = python test_dj_database_url.py From c12fa956fb5b873a3146fe8b76f084582f52c228 Mon Sep 17 00:00:00 2001 From: Arthur Skowronek Date: Wed, 11 Nov 2015 18:47:21 +0100 Subject: [PATCH 2/2] Handle search path config in connect url for postgres --- dj_database_url.py | 18 ++++++++++++++++-- test_dj_database_url.py | 11 +++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/dj_database_url.py b/dj_database_url.py index 8a73c71..d4b1bb1 100644 --- a/dj_database_url.py +++ b/dj_database_url.py @@ -66,9 +66,13 @@ def parse(url, engine=None): url = urlparse.urlparse(url) - # Remove query strings. + # Split query strings from path. path = url.path[1:] - path = path.split('?', 2)[0] + if '?' in path and not url.query: + path, query = path.split('?', 2) + else: + path, query = path, url.query + query = urlparse.parse_qs(query) # If we are using sqlite and we have no path, then assume we # want an in-memory database (this is the behaviour of sqlalchemy) @@ -94,4 +98,14 @@ def parse(url, engine=None): elif url.scheme in SCHEMES: config['ENGINE'] = SCHEMES[url.scheme] + if config['ENGINE'] == 'django.db.backends.postgresql_psycopg2': + try: + current_schema = query['currentSchema'][0] + except (KeyError, IndexError): + pass + else: + config['OPTIONS'] = { + 'options': '-c search_path=' + current_schema + } + return config diff --git a/test_dj_database_url.py b/test_dj_database_url.py index 001960a..c5c1ce0 100644 --- a/test_dj_database_url.py +++ b/test_dj_database_url.py @@ -34,6 +34,17 @@ class DatabaseTestSuite(unittest.TestCase): assert url['PASSWORD'] == '' assert url['PORT'] == '' + def test_postgres_search_path_parsing(self): + url = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?currentSchema=otherschema' + url = dj_database_url.parse(url) + assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2' + assert url['NAME'] == 'd8r82722r2kuvn' + assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com' + assert url['USER'] == 'uf07k1i6d8ia0v' + assert url['PASSWORD'] == 'wegauwhgeuioweg' + assert url['PORT'] == 5431 + assert url['OPTIONS'] == {'options': '-c search_path=otherschema'} + def test_postgis_parsing(self): url = 'postgis://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn' url = dj_database_url.parse(url)