2012-04-30 17:37:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import os
|
2012-06-19 15:53:51 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import urlparse
|
|
|
|
|
except ImportError:
|
|
|
|
|
import urllib.parse as urlparse
|
|
|
|
|
|
|
|
|
|
|
2012-04-30 17:37:05 +00:00
|
|
|
# Register database schemes in URLs.
|
|
|
|
|
urlparse.uses_netloc.append('postgres')
|
2012-06-19 15:21:06 +00:00
|
|
|
urlparse.uses_netloc.append('postgresql')
|
2012-09-08 16:42:37 +00:00
|
|
|
urlparse.uses_netloc.append('pgsql')
|
2012-06-05 02:37:58 +00:00
|
|
|
urlparse.uses_netloc.append('postgis')
|
2012-04-30 17:37:05 +00:00
|
|
|
urlparse.uses_netloc.append('mysql')
|
2012-06-19 15:21:06 +00:00
|
|
|
urlparse.uses_netloc.append('mysql2')
|
2013-09-26 14:39:16 +00:00
|
|
|
urlparse.uses_netloc.append('mysqlgis')
|
2014-03-28 10:50:25 +00:00
|
|
|
urlparse.uses_netloc.append('mysql-connector')
|
2013-02-04 15:24:58 +00:00
|
|
|
urlparse.uses_netloc.append('spatialite')
|
2012-05-01 16:23:09 +00:00
|
|
|
urlparse.uses_netloc.append('sqlite')
|
2014-10-24 18:48:14 +00:00
|
|
|
urlparse.uses_netloc.append('oracle')
|
|
|
|
|
urlparse.uses_netloc.append('oraclegis')
|
2012-04-30 17:37:05 +00:00
|
|
|
|
2012-04-30 18:33:12 +00:00
|
|
|
DEFAULT_ENV = 'DATABASE_URL'
|
2012-04-30 17:37:05 +00:00
|
|
|
|
2012-06-19 14:57:33 +00:00
|
|
|
SCHEMES = {
|
|
|
|
|
'postgres': 'django.db.backends.postgresql_psycopg2',
|
|
|
|
|
'postgresql': 'django.db.backends.postgresql_psycopg2',
|
2012-09-08 16:42:37 +00:00
|
|
|
'pgsql': 'django.db.backends.postgresql_psycopg2',
|
2012-06-19 14:57:33 +00:00
|
|
|
'postgis': 'django.contrib.gis.db.backends.postgis',
|
|
|
|
|
'mysql': 'django.db.backends.mysql',
|
|
|
|
|
'mysql2': 'django.db.backends.mysql',
|
2013-09-26 14:39:16 +00:00
|
|
|
'mysqlgis': 'django.contrib.gis.db.backends.mysql',
|
2014-03-28 10:50:25 +00:00
|
|
|
'mysql-connector': 'mysql.connector.django',
|
2013-02-01 16:33:44 +00:00
|
|
|
'spatialite': 'django.contrib.gis.db.backends.spatialite',
|
2013-09-26 14:39:16 +00:00
|
|
|
'sqlite': 'django.db.backends.sqlite3',
|
2014-10-24 18:48:14 +00:00
|
|
|
'oracle': 'django.db.backends.oracle',
|
|
|
|
|
'oraclegis': 'django.contrib.gis.db.backends.oracle',
|
2012-06-19 14:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-05-11 05:29:47 +00:00
|
|
|
def config(env=DEFAULT_ENV, default=None, engine=None, conn_max_age=0):
|
2012-04-30 18:55:37 +00:00
|
|
|
"""Returns configured DATABASE dictionary from DATABASE_URL."""
|
2012-04-30 17:37:05 +00:00
|
|
|
|
2012-04-30 18:33:12 +00:00
|
|
|
config = {}
|
2012-04-30 17:37:05 +00:00
|
|
|
|
2012-05-30 08:46:21 +00:00
|
|
|
s = os.environ.get(env, default)
|
2012-05-30 05:23:28 +00:00
|
|
|
|
|
|
|
|
if s:
|
2015-05-11 05:29:47 +00:00
|
|
|
config = parse(s, engine, conn_max_age)
|
2012-04-30 18:55:37 +00:00
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
2015-05-11 05:29:47 +00:00
|
|
|
def parse(url, engine=None, conn_max_age=0):
|
2012-04-30 18:55:37 +00:00
|
|
|
"""Parses a database URL."""
|
|
|
|
|
|
2013-01-05 10:25:37 +00:00
|
|
|
if url == 'sqlite://:memory:':
|
|
|
|
|
# this is a special case, because if we pass this URL into
|
|
|
|
|
# urlparse, urlparse will choke trying to interpret "memory"
|
|
|
|
|
# as a port number
|
|
|
|
|
return {
|
|
|
|
|
'ENGINE': SCHEMES['sqlite'],
|
|
|
|
|
'NAME': ':memory:'
|
|
|
|
|
}
|
2013-01-05 10:28:27 +00:00
|
|
|
# note: no other settings are required for sqlite
|
2013-01-05 10:25:37 +00:00
|
|
|
|
|
|
|
|
# otherwise parse the url as normal
|
2012-04-30 18:55:37 +00:00
|
|
|
config = {}
|
|
|
|
|
|
|
|
|
|
url = urlparse.urlparse(url)
|
|
|
|
|
|
2015-05-14 21:52:16 +00:00
|
|
|
# Path (without leading '/'), and with no query string
|
|
|
|
|
path = url.path[1:].split('?')[0]
|
2012-06-19 15:21:06 +00:00
|
|
|
|
2014-02-04 15:18:18 +00:00
|
|
|
# If we are using sqlite and we have no path, then assume we
|
2013-01-05 10:22:45 +00:00
|
|
|
# want an in-memory database (this is the behaviour of sqlalchemy)
|
|
|
|
|
if url.scheme == 'sqlite' and path == '':
|
|
|
|
|
path = ':memory:'
|
|
|
|
|
|
2014-02-04 15:18:18 +00:00
|
|
|
# Handle postgres percent-encoded paths.
|
|
|
|
|
hostname = url.hostname or ''
|
|
|
|
|
if '%2f' in hostname.lower():
|
|
|
|
|
hostname = hostname.replace('%2f', '/').replace('%2F', '/')
|
|
|
|
|
|
2012-04-30 18:55:37 +00:00
|
|
|
# Update with environment configuration.
|
|
|
|
|
config.update({
|
2014-12-31 16:25:39 +00:00
|
|
|
'NAME': urlparse.unquote(path or ''),
|
|
|
|
|
'USER': urlparse.unquote(url.username or ''),
|
|
|
|
|
'PASSWORD': urlparse.unquote(url.password or ''),
|
2014-02-04 15:18:18 +00:00
|
|
|
'HOST': hostname,
|
2012-06-22 23:57:53 +00:00
|
|
|
'PORT': url.port or '',
|
2015-05-11 05:29:47 +00:00
|
|
|
'CONN_MAX_AGE': conn_max_age,
|
2012-04-30 18:55:37 +00:00
|
|
|
})
|
|
|
|
|
|
2015-03-23 20:03:20 +00:00
|
|
|
# Parse the query string into OPTIONS.
|
2015-05-15 16:32:15 +00:00
|
|
|
qs = urlparse.parse_qs(url.query)
|
|
|
|
|
options = {}
|
2016-02-02 22:36:35 +00:00
|
|
|
for key, values in qs.items():
|
2015-05-15 16:32:15 +00:00
|
|
|
options[key] = values[-1]
|
2015-03-23 20:03:20 +00:00
|
|
|
if options:
|
|
|
|
|
config['OPTIONS'] = options
|
|
|
|
|
|
2013-12-15 16:59:18 +00:00
|
|
|
if engine:
|
|
|
|
|
config['ENGINE'] = engine
|
|
|
|
|
elif url.scheme in SCHEMES:
|
2012-06-19 14:57:33 +00:00
|
|
|
config['ENGINE'] = SCHEMES[url.scheme]
|
2012-05-01 16:23:09 +00:00
|
|
|
|
|
|
|
|
return config
|