2012-04-30 17:37:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import os
|
2022-05-29 09:33:30 +00:00
|
|
|
import urllib.parse as urlparse
|
2012-06-19 15:53:51 +00:00
|
|
|
|
2018-03-28 21:50:34 +00:00
|
|
|
try:
|
|
|
|
|
from django import VERSION as DJANGO_VERSION
|
|
|
|
|
except ImportError:
|
|
|
|
|
DJANGO_VERSION = None
|
|
|
|
|
|
2012-06-19 15:53:51 +00:00
|
|
|
|
2012-04-30 17:37:05 +00:00
|
|
|
# Register database schemes in URLs.
|
2022-05-04 08:43:30 +00:00
|
|
|
urlparse.uses_netloc.append("postgres")
|
|
|
|
|
urlparse.uses_netloc.append("postgresql")
|
|
|
|
|
urlparse.uses_netloc.append("pgsql")
|
|
|
|
|
urlparse.uses_netloc.append("postgis")
|
|
|
|
|
urlparse.uses_netloc.append("mysql")
|
|
|
|
|
urlparse.uses_netloc.append("mysql2")
|
|
|
|
|
urlparse.uses_netloc.append("mysqlgis")
|
|
|
|
|
urlparse.uses_netloc.append("mysql-connector")
|
|
|
|
|
urlparse.uses_netloc.append("mssql")
|
2022-05-28 20:18:21 +00:00
|
|
|
urlparse.uses_netloc.append("mssqlms")
|
2022-05-04 08:43:30 +00:00
|
|
|
urlparse.uses_netloc.append("spatialite")
|
|
|
|
|
urlparse.uses_netloc.append("sqlite")
|
|
|
|
|
urlparse.uses_netloc.append("oracle")
|
|
|
|
|
urlparse.uses_netloc.append("oraclegis")
|
|
|
|
|
urlparse.uses_netloc.append("redshift")
|
2022-05-17 21:13:39 +00:00
|
|
|
urlparse.uses_netloc.append("cockroach")
|
2022-05-04 08:43:30 +00:00
|
|
|
|
|
|
|
|
DEFAULT_ENV = "DATABASE_URL"
|
2012-04-30 17:37:05 +00:00
|
|
|
|
2012-06-19 14:57:33 +00:00
|
|
|
SCHEMES = {
|
2022-05-04 08:43:30 +00:00
|
|
|
"postgis": "django.contrib.gis.db.backends.postgis",
|
|
|
|
|
"mysql": "django.db.backends.mysql",
|
|
|
|
|
"mysql2": "django.db.backends.mysql",
|
|
|
|
|
"mysqlgis": "django.contrib.gis.db.backends.mysql",
|
|
|
|
|
"mysql-connector": "mysql.connector.django",
|
|
|
|
|
"mssql": "sql_server.pyodbc",
|
2022-05-28 20:18:21 +00:00
|
|
|
"mssqlms": "mssql",
|
2022-05-04 08:43:30 +00:00
|
|
|
"spatialite": "django.contrib.gis.db.backends.spatialite",
|
|
|
|
|
"sqlite": "django.db.backends.sqlite3",
|
|
|
|
|
"oracle": "django.db.backends.oracle",
|
|
|
|
|
"oraclegis": "django.contrib.gis.db.backends.oracle",
|
|
|
|
|
"redshift": "django_redshift_backend",
|
2022-05-17 21:13:39 +00:00
|
|
|
"cockroach": "django_cockroachdb",
|
2012-06-19 14:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-28 21:50:34 +00:00
|
|
|
# https://docs.djangoproject.com/en/2.0/releases/2.0/#id1
|
|
|
|
|
if DJANGO_VERSION and DJANGO_VERSION < (2, 0):
|
2022-05-04 08:43:30 +00:00
|
|
|
SCHEMES["postgres"] = "django.db.backends.postgresql_psycopg2"
|
|
|
|
|
SCHEMES["postgresql"] = "django.db.backends.postgresql_psycopg2"
|
|
|
|
|
SCHEMES["pgsql"] = "django.db.backends.postgresql_psycopg2"
|
2018-03-28 21:50:34 +00:00
|
|
|
else:
|
2022-05-04 08:43:30 +00:00
|
|
|
SCHEMES["postgres"] = "django.db.backends.postgresql"
|
|
|
|
|
SCHEMES["postgresql"] = "django.db.backends.postgresql"
|
|
|
|
|
SCHEMES["pgsql"] = "django.db.backends.postgresql"
|
2018-03-28 21:50:34 +00:00
|
|
|
|
2012-06-19 14:57:33 +00:00
|
|
|
|
2022-01-11 12:56:27 +00:00
|
|
|
def config(
|
|
|
|
|
env=DEFAULT_ENV, default=None, engine=None, conn_max_age=0, ssl_require=False
|
|
|
|
|
):
|
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:
|
2018-03-01 18:37:00 +00:00
|
|
|
config = parse(s, engine, conn_max_age, ssl_require)
|
2012-04-30 18:55:37 +00:00
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
2018-03-01 18:37:00 +00:00
|
|
|
def parse(url, engine=None, conn_max_age=0, ssl_require=False):
|
2012-04-30 18:55:37 +00:00
|
|
|
"""Parses a database URL."""
|
|
|
|
|
|
2022-05-04 08:43:30 +00:00
|
|
|
if url == "sqlite://:memory:":
|
2013-01-05 10:25:37 +00:00
|
|
|
# this is a special case, because if we pass this URL into
|
|
|
|
|
# urlparse, urlparse will choke trying to interpret "memory"
|
|
|
|
|
# as a port number
|
2022-05-04 08:43:30 +00:00
|
|
|
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)
|
|
|
|
|
|
2016-02-03 00:07:35 +00:00
|
|
|
# Split query strings from path.
|
|
|
|
|
path = url.path[1:]
|
2022-05-04 08:43:30 +00:00
|
|
|
if "?" in path and not url.query:
|
|
|
|
|
path, query = path.split("?", 2)
|
2016-02-03 00:07:35 +00:00
|
|
|
else:
|
|
|
|
|
path, query = path, url.query
|
|
|
|
|
query = urlparse.parse_qs(query)
|
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)
|
2022-05-04 08:43:30 +00:00
|
|
|
if url.scheme == "sqlite" and path == "":
|
|
|
|
|
path = ":memory:"
|
2013-01-05 10:22:45 +00:00
|
|
|
|
2014-02-04 15:18:18 +00:00
|
|
|
# Handle postgres percent-encoded paths.
|
2022-05-04 08:43:30 +00:00
|
|
|
hostname = url.hostname or ""
|
|
|
|
|
if "%2f" in hostname.lower():
|
2017-01-20 15:06:53 +00:00
|
|
|
# Switch to url.netloc to avoid lower cased paths
|
|
|
|
|
hostname = url.netloc
|
|
|
|
|
if "@" in hostname:
|
|
|
|
|
hostname = hostname.rsplit("@", 1)[1]
|
|
|
|
|
if ":" in hostname:
|
|
|
|
|
hostname = hostname.split(":", 1)[0]
|
2022-05-04 08:43:30 +00:00
|
|
|
hostname = hostname.replace("%2f", "/").replace("%2F", "/")
|
2014-02-04 15:18:18 +00:00
|
|
|
|
2017-04-08 10:34:30 +00:00
|
|
|
# Lookup specified engine.
|
|
|
|
|
engine = SCHEMES[url.scheme] if engine is None else engine
|
|
|
|
|
|
2022-01-11 12:56:27 +00:00
|
|
|
port = (
|
|
|
|
|
str(url.port)
|
2022-05-28 20:18:21 +00:00
|
|
|
if url.port
|
|
|
|
|
and engine in [SCHEMES["oracle"], SCHEMES["mssql"], SCHEMES["mssqlms"]]
|
2022-01-11 12:56:27 +00:00
|
|
|
else url.port
|
|
|
|
|
)
|
2017-04-08 10:34:30 +00:00
|
|
|
|
2012-04-30 18:55:37 +00:00
|
|
|
# Update with environment configuration.
|
2022-01-11 12:56:27 +00:00
|
|
|
config.update(
|
|
|
|
|
{
|
2022-05-04 08:43:30 +00:00
|
|
|
"NAME": urlparse.unquote(path or ""),
|
|
|
|
|
"USER": urlparse.unquote(url.username or ""),
|
|
|
|
|
"PASSWORD": urlparse.unquote(url.password or ""),
|
|
|
|
|
"HOST": hostname,
|
|
|
|
|
"PORT": port or "",
|
|
|
|
|
"CONN_MAX_AGE": conn_max_age,
|
2022-01-11 12:56:27 +00:00
|
|
|
}
|
|
|
|
|
)
|
2012-04-30 18:55:37 +00:00
|
|
|
|
2016-02-03 00:07:35 +00:00
|
|
|
# Pass the query string into OPTIONS.
|
2015-05-15 16:32:15 +00:00
|
|
|
options = {}
|
2016-02-03 00:07:35 +00:00
|
|
|
for key, values in query.items():
|
2022-05-04 08:43:30 +00:00
|
|
|
if url.scheme == "mysql" and key == "ssl-ca":
|
|
|
|
|
options["ssl"] = {"ca": values[-1]}
|
2016-04-06 15:46:49 +00:00
|
|
|
continue
|
|
|
|
|
|
2015-05-15 16:32:15 +00:00
|
|
|
options[key] = values[-1]
|
2016-02-03 00:07:35 +00:00
|
|
|
|
2018-03-01 18:37:00 +00:00
|
|
|
if ssl_require:
|
2022-05-04 08:43:30 +00:00
|
|
|
options["sslmode"] = "require"
|
2018-03-01 18:37:00 +00:00
|
|
|
|
2016-02-03 00:07:35 +00:00
|
|
|
# Support for Postgres Schema URLs
|
2022-05-04 08:43:30 +00:00
|
|
|
if "currentSchema" in options and engine in (
|
|
|
|
|
"django.contrib.gis.db.backends.postgis",
|
|
|
|
|
"django.db.backends.postgresql_psycopg2",
|
|
|
|
|
"django.db.backends.postgresql",
|
|
|
|
|
"django_redshift_backend",
|
2016-11-28 12:49:26 +00:00
|
|
|
):
|
2022-05-04 08:43:30 +00:00
|
|
|
options["options"] = "-c search_path={0}".format(options.pop("currentSchema"))
|
2016-02-03 00:07:35 +00:00
|
|
|
|
2015-03-23 20:03:20 +00:00
|
|
|
if options:
|
2022-05-04 08:43:30 +00:00
|
|
|
config["OPTIONS"] = options
|
2015-03-23 20:03:20 +00:00
|
|
|
|
2013-12-15 16:59:18 +00:00
|
|
|
if engine:
|
2022-05-04 08:43:30 +00:00
|
|
|
config["ENGINE"] = engine
|
2012-05-01 16:23:09 +00:00
|
|
|
|
|
|
|
|
return config
|