2012-04-30 17:37:05 +00:00
|
|
|
import os
|
2022-05-29 09:33:30 +00:00
|
|
|
import urllib.parse as urlparse
|
2022-12-21 11:47:41 +00:00
|
|
|
from typing import Any, Dict, Optional, Union
|
|
|
|
|
|
|
|
|
|
from typing_extensions import TypedDict
|
2012-06-19 15:53:51 +00:00
|
|
|
|
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-09-25 20:49:01 +00:00
|
|
|
"postgres": "django.db.backends.postgresql",
|
|
|
|
|
"postgresql": "django.db.backends.postgresql",
|
|
|
|
|
"pgsql": "django.db.backends.postgresql",
|
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",
|
2022-07-10 09:16:06 +00:00
|
|
|
"timescale": "timescale.db.backends.postgresql",
|
|
|
|
|
"timescalegis": "timescale.db.backends.postgis",
|
2012-06-19 14:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-30 09:48:07 +00:00
|
|
|
# Register database schemes in URLs.
|
|
|
|
|
for key in SCHEMES.keys():
|
|
|
|
|
urlparse.uses_netloc.append(key)
|
|
|
|
|
|
2012-06-19 14:57:33 +00:00
|
|
|
|
2022-12-21 11:47:41 +00:00
|
|
|
# From https://docs.djangoproject.com/en/4.0/ref/settings/#databases
|
|
|
|
|
class DBConfig(TypedDict, total=False):
|
|
|
|
|
ATOMIC_REQUESTS: bool
|
|
|
|
|
AUTOCOMMIT: bool
|
|
|
|
|
CONN_MAX_AGE: int
|
|
|
|
|
CONN_HEALTH_CHECKS: bool
|
|
|
|
|
DISABLE_SERVER_SIDE_CURSORS: bool
|
|
|
|
|
ENGINE: str
|
|
|
|
|
HOST: str
|
|
|
|
|
NAME: str
|
|
|
|
|
OPTIONS: Optional[Dict[str, Any]]
|
|
|
|
|
PASSWORD: str
|
|
|
|
|
PORT: Union[str, int]
|
|
|
|
|
TEST: Dict[str, Any]
|
|
|
|
|
TIME_ZONE: str
|
|
|
|
|
USER: str
|
|
|
|
|
|
|
|
|
|
|
2022-01-11 12:56:27 +00:00
|
|
|
def config(
|
2022-12-21 11:47:41 +00:00
|
|
|
env: str = DEFAULT_ENV,
|
|
|
|
|
default: Optional[str] = None,
|
|
|
|
|
engine: Optional[str] = None,
|
|
|
|
|
conn_max_age: int = 0,
|
|
|
|
|
conn_health_checks: bool = False,
|
|
|
|
|
ssl_require: bool = False,
|
|
|
|
|
test_options: Optional[Dict] = None,
|
|
|
|
|
) -> DBConfig:
|
2012-04-30 18:55:37 +00:00
|
|
|
"""Returns configured DATABASE dictionary from DATABASE_URL."""
|
2012-05-30 08:46:21 +00:00
|
|
|
s = os.environ.get(env, default)
|
2012-05-30 05:23:28 +00:00
|
|
|
|
|
|
|
|
if s:
|
2022-12-12 14:56:50 +00:00
|
|
|
return parse(
|
|
|
|
|
s, engine, conn_max_age, conn_health_checks, ssl_require, test_options
|
|
|
|
|
)
|
2012-04-30 18:55:37 +00:00
|
|
|
|
2022-05-29 09:33:52 +00:00
|
|
|
return {}
|
2012-04-30 18:55:37 +00:00
|
|
|
|
|
|
|
|
|
2022-12-11 15:38:29 +00:00
|
|
|
def parse(
|
2022-12-21 11:47:41 +00:00
|
|
|
url: str,
|
|
|
|
|
engine: Optional[str] = None,
|
|
|
|
|
conn_max_age: int = 0,
|
|
|
|
|
conn_health_checks: bool = False,
|
|
|
|
|
ssl_require: bool = False,
|
|
|
|
|
test_options: Optional[dict] = None,
|
|
|
|
|
) -> DBConfig:
|
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
|
2022-12-21 11:47:41 +00:00
|
|
|
parsed_config: DBConfig = {}
|
2012-04-30 18:55:37 +00:00
|
|
|
|
2022-12-13 09:00:55 +00:00
|
|
|
if test_options is None:
|
|
|
|
|
test_options = {}
|
|
|
|
|
|
2022-12-21 11:47:41 +00:00
|
|
|
spliturl = urlparse.urlsplit(url)
|
2012-04-30 18:55:37 +00:00
|
|
|
|
2016-02-03 00:07:35 +00:00
|
|
|
# Split query strings from path.
|
2022-12-21 11:47:41 +00:00
|
|
|
path = spliturl.path[1:]
|
2022-12-30 09:44:45 +00:00
|
|
|
query = urlparse.parse_qs(spliturl.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-12-21 11:47:41 +00:00
|
|
|
if spliturl.scheme == "sqlite" and path == "":
|
2022-05-04 08:43:30 +00:00
|
|
|
path = ":memory:"
|
2013-01-05 10:22:45 +00:00
|
|
|
|
2014-02-04 15:18:18 +00:00
|
|
|
# Handle postgres percent-encoded paths.
|
2022-12-21 11:47:41 +00:00
|
|
|
hostname = spliturl.hostname or ""
|
2022-12-12 20:18:02 +00:00
|
|
|
if "%" in hostname:
|
2017-01-20 15:06:53 +00:00
|
|
|
# Switch to url.netloc to avoid lower cased paths
|
2022-12-21 11:47:41 +00:00
|
|
|
hostname = spliturl.netloc
|
2017-01-20 15:06:53 +00:00
|
|
|
if "@" in hostname:
|
|
|
|
|
hostname = hostname.rsplit("@", 1)[1]
|
2022-12-12 20:18:02 +00:00
|
|
|
# Use URL Parse library to decode % encodes
|
|
|
|
|
hostname = urlparse.unquote(hostname)
|
2014-02-04 15:18:18 +00:00
|
|
|
|
2017-04-08 10:34:30 +00:00
|
|
|
# Lookup specified engine.
|
2022-12-21 08:19:39 +00:00
|
|
|
if engine is None:
|
2022-12-21 11:47:41 +00:00
|
|
|
engine = SCHEMES.get(spliturl.scheme)
|
2022-12-21 08:19:39 +00:00
|
|
|
if engine is None:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"No support for '%s'. We support: %s"
|
2022-12-21 11:47:41 +00:00
|
|
|
% (spliturl.scheme, ", ".join(sorted(SCHEMES.keys())))
|
2022-12-21 08:19:39 +00:00
|
|
|
)
|
2017-04-08 10:34:30 +00:00
|
|
|
|
2022-01-11 12:56:27 +00:00
|
|
|
port = (
|
2022-12-21 11:47:41 +00:00
|
|
|
str(spliturl.port)
|
|
|
|
|
if spliturl.port
|
2022-12-11 15:15:20 +00:00
|
|
|
and engine in (SCHEMES["oracle"], SCHEMES["mssql"], SCHEMES["mssqlms"])
|
2022-12-21 11:47:41 +00:00
|
|
|
else spliturl.port
|
2022-01-11 12:56:27 +00:00
|
|
|
)
|
2017-04-08 10:34:30 +00:00
|
|
|
|
2012-04-30 18:55:37 +00:00
|
|
|
# Update with environment configuration.
|
2022-05-29 09:33:52 +00:00
|
|
|
parsed_config.update(
|
2022-01-11 12:56:27 +00:00
|
|
|
{
|
2022-05-04 08:43:30 +00:00
|
|
|
"NAME": urlparse.unquote(path or ""),
|
2022-12-21 11:47:41 +00:00
|
|
|
"USER": urlparse.unquote(spliturl.username or ""),
|
|
|
|
|
"PASSWORD": urlparse.unquote(spliturl.password or ""),
|
2022-05-04 08:43:30 +00:00
|
|
|
"HOST": hostname,
|
|
|
|
|
"PORT": port or "",
|
|
|
|
|
"CONN_MAX_AGE": conn_max_age,
|
2022-12-11 15:38:29 +00:00
|
|
|
"CONN_HEALTH_CHECKS": conn_health_checks,
|
2022-12-30 09:44:45 +00:00
|
|
|
"ENGINE": engine,
|
2022-01-11 12:56:27 +00:00
|
|
|
}
|
|
|
|
|
)
|
2022-12-12 14:56:50 +00:00
|
|
|
if test_options:
|
|
|
|
|
parsed_config.update(
|
|
|
|
|
{
|
|
|
|
|
'TEST': test_options,
|
|
|
|
|
}
|
|
|
|
|
)
|
2012-04-30 18:55:37 +00:00
|
|
|
|
2016-02-03 00:07:35 +00:00
|
|
|
# Pass the query string into OPTIONS.
|
2022-12-21 11:47:41 +00:00
|
|
|
options: Dict[str, Any] = {}
|
2016-02-03 00:07:35 +00:00
|
|
|
for key, values in query.items():
|
2022-12-21 11:47:41 +00:00
|
|
|
if spliturl.scheme == "mysql" and key == "ssl-ca":
|
2022-05-04 08:43:30 +00:00
|
|
|
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",
|
2022-07-10 09:16:06 +00:00
|
|
|
"timescale.db.backends.postgresql",
|
|
|
|
|
"timescale.db.backends.postgis",
|
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-29 09:33:52 +00:00
|
|
|
parsed_config["OPTIONS"] = options
|
2015-03-23 20:03:20 +00:00
|
|
|
|
2022-05-29 09:33:52 +00:00
|
|
|
return parsed_config
|