Skip to content
Browse files

web.database now accepts a URL, $DATABASE_URL (fixes #171)

  • Loading branch information...
1 parent 35dc18c commit df9e043bf95bbac604dc8bc846a2447722ad176a @aaronsw aaronsw committed Aug 7, 2012
Showing with 21 additions and 1 deletion.
  1. +21 −1 web/db.py
View
22 web/db.py
@@ -11,7 +11,7 @@
"database", 'DB',
]
-import time
+import time, os
try:
import datetime
except ImportError:
@@ -1131,13 +1131,33 @@ def _process_insert_query(self, query, tablename, seqname):
else:
return query + "; SELECT %s.currval FROM dual" % seqname
+def dburl2dict(url):
+ """
+ Takes a URL to a database and parses it into an equivalent dictionary.
+
+ >>> dburl2dict('postgres://james:day@serverfarm.example.net:5432/mygreatdb')
+ {'user': 'james', 'host': 'serverfarm.example.net', 'db': 'mygreatdb', 'pw': 'day', 'dbn': 'postgres'}
+
+ """
+ dbn, rest = url.split('://', 1)
+ user, rest = rest.split(':', 1)
+ pw, rest = rest.split('@', 1)
+ host, rest = rest.split(':', 1)
+ port, rest = rest.split('/', 1)
+ db = rest
+ return dict(dbn=dbn, user=user, pw=pw, db=db, host=host)
+
_databases = {}
def database(dburl=None, **params):
"""Creates appropriate database using params.
Pooling will be enabled if DBUtils module is available.
Pooling can be disabled by passing pooling=False in params.
"""
+ if not dburl and not params:
+ dburl = os.environ['DATABASE_URL']
+ if dburl:
+ params = dburl2dict(dburl)
dbn = params.pop('dbn')
if dbn in _databases:
return _databases[dbn](**params)

0 comments on commit df9e043

Please sign in to comment.
Something went wrong with that request. Please try again.