Merge pull request #52 from rcrdclub/master

Proper support for OPTIONS based on DATABASE_URL querystring
This commit is contained in:
2016-02-02 17:09:47 -05:00
2 changed files with 32 additions and 3 deletions
+10 -3
View File
@@ -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:
+22
View File
@@ -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()