From e8ea0a64fa58cdea6dd0ca3b739186a649ebf5b1 Mon Sep 17 00:00:00 2001 From: Jeremy Shute Date: Mon, 23 Mar 2015 16:03:20 -0400 Subject: [PATCH] supports database options --- dj_database_url.py | 25 +++++++++++++++++++++++++ test_dj_database_url.py | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/dj_database_url.py b/dj_database_url.py index 8a73c71..4212603 100644 --- a/dj_database_url.py +++ b/dj_database_url.py @@ -89,9 +89,34 @@ def parse(url, engine=None): 'PORT': url.port or '', }) + # Parse the query string into OPTIONS. + qs = urlparse.parse_qs(url.query) + options = {} + for k in qs: + options[k] = qs[k][-1] + if options: + config['OPTIONS'] = options + if engine: config['ENGINE'] = engine elif url.scheme in SCHEMES: config['ENGINE'] = SCHEMES[url.scheme] return config + + +def main(): + import django.db + from django.conf import settings + default = django.db.DEFAULT_DB_ALIAS + settings.configure() + settings.DATABASES[default] = config() + db = django.db.connections[default] + db.connect() + import pprint + pprint.pprint(db.settings_dict) + print db.is_usable() + + +if __name__ == "__main__": + main() diff --git a/test_dj_database_url.py b/test_dj_database_url.py index 001960a..f8d3f0a 100644 --- a/test_dj_database_url.py +++ b/test_dj_database_url.py @@ -111,5 +111,26 @@ class DatabaseTestSuite(unittest.TestCase): assert url['ENGINE'] == engine + def test_database_url_with_options(self): + del os.environ['DATABASE_URL'] + a = dj_database_url.config() + assert not a + + 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' + } + + if __name__ == '__main__': unittest.main()