diff --git a/dj_database_url.py b/dj_database_url.py index 37975fc..ef8afb1 100644 --- a/dj_database_url.py +++ b/dj_database_url.py @@ -66,9 +66,8 @@ def parse(url, engine=None, conn_max_age=0): url = urlparse.urlparse(url) - # Remove query strings. - path = url.path[1:] - path = path.split('?', 2)[0] + # Path (without leading '/'), and with no query string + path = url.path[1:].split('?')[0] # If we are using sqlite and we have no path, then assume we # want an in-memory database (this is the behaviour of sqlalchemy) @@ -90,6 +89,14 @@ def parse(url, engine=None, conn_max_age=0): 'CONN_MAX_AGE': conn_max_age, }) + # Parse the query string into OPTIONS. + qs = urlparse.parse_qs(url.query) + options = {} + for key, values in qs.iteritems(): + options[key] = values[-1] + if options: + config['OPTIONS'] = options + if engine: config['ENGINE'] = engine elif url.scheme in SCHEMES: diff --git a/test_dj_database_url.py b/test_dj_database_url.py index d4c6c44..4bda93a 100644 --- a/test_dj_database_url.py +++ b/test_dj_database_url.py @@ -125,5 +125,27 @@ class DatabaseTestSuite(unittest.TestCase): assert url['CONN_MAX_AGE'] == conn_max_age + def test_database_url_with_options(self): + # Test full options + os.environ['DATABASE_URL'] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?sslrootcert=rds-combined-ca-bundle.pem&sslmode=verify-full' + url = dj_database_url.config() + + 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'] == { + 'sslrootcert': 'rds-combined-ca-bundle.pem', + 'sslmode': 'verify-full' + } + + # Test empty options + os.environ['DATABASE_URL'] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?' + url = dj_database_url.config() + assert 'OPTIONS' not in url + + if __name__ == '__main__': unittest.main()