Skip to content

Commit

Permalink
Merge pull request jazzband#101 from mintel/psycopg2
Browse files Browse the repository at this point in the history
fix jazzband#96 deprecated postgres backend strings
  • Loading branch information
Ian Stapleton Cordasco committed May 3, 2018
2 parents b6b6a1f + 628335f commit 53c3d79
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
32 changes: 32 additions & 0 deletions .travis.yml
Expand Up @@ -6,4 +6,36 @@ python:
- 3.4
- 3.5
- 3.6

env:
matrix:
- DJANGO="Django>=1.8,<1.9"
- DJANGO="Django>=1.9,<1.10"
- DJANGO="Django>=1.10,<1.11"
- DJANGO="Django>=1.11,<2.0"
- DJANGO="Django>=2.0,<2.1"
- DJANGO="https://github.com/django/django/archive/master.tar.gz"

matrix:
fast_finish: true
include:
- python: 3.3
env: DJANGO="Django>=1.8,<1.9"
exclude:
# Django 2 dropped support for Python 2.
- python: 2.7
env: DJANGO="Django>=2.0,<2.1"
- python: 2.7
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
# Django master dropped support for Python < 3.5.
- python: 3.2
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
- python: 3.3
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
- python: 3.4
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
allow_failures:
- env: DJANGO="https://github.com/django/django/archive/master.tar.gz"

install: pip install . $DJANGO
script: make test
19 changes: 16 additions & 3 deletions dj_database_url.py
Expand Up @@ -7,6 +7,11 @@
except ImportError:
import urllib.parse as urlparse

try:
from django import VERSION as DJANGO_VERSION
except ImportError:
DJANGO_VERSION = None


# Register database schemes in URLs.
urlparse.uses_netloc.append('postgres')
Expand All @@ -27,9 +32,6 @@
DEFAULT_ENV = 'DATABASE_URL'

SCHEMES = {
'postgres': 'django.db.backends.postgresql_psycopg2',
'postgresql': 'django.db.backends.postgresql_psycopg2',
'pgsql': 'django.db.backends.postgresql_psycopg2',
'postgis': 'django.contrib.gis.db.backends.postgis',
'mysql': 'django.db.backends.mysql',
'mysql2': 'django.db.backends.mysql',
Expand All @@ -43,6 +45,16 @@
'redshift': 'django_redshift_backend',
}

# https://docs.djangoproject.com/en/2.0/releases/2.0/#id1
if DJANGO_VERSION and DJANGO_VERSION < (2, 0):
SCHEMES['postgres'] = 'django.db.backends.postgresql_psycopg2'
SCHEMES['postgresql'] = 'django.db.backends.postgresql_psycopg2'
SCHEMES['pgsql'] = 'django.db.backends.postgresql_psycopg2'
else:
SCHEMES['postgres'] = 'django.db.backends.postgresql'
SCHEMES['postgresql'] = 'django.db.backends.postgresql'
SCHEMES['pgsql'] = 'django.db.backends.postgresql'


def config(env=DEFAULT_ENV, default=None, engine=None, conn_max_age=0, ssl_require=False):
"""Returns configured DATABASE dictionary from DATABASE_URL."""
Expand Down Expand Up @@ -131,6 +143,7 @@ def parse(url, engine=None, conn_max_age=0, ssl_require=False):
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',
):
options['options'] = '-c search_path={0}'.format(options.pop('currentSchema'))
Expand Down
24 changes: 16 additions & 8 deletions test_dj_database_url.py
Expand Up @@ -4,19 +4,27 @@
import os
import unittest

from django import VERSION as DJANGO_VERSION

import dj_database_url


POSTGIS_URL = 'postgis://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'

# Django deprecated the `django.db.backends.postgresql_psycopg2` in 2.0.
# https://docs.djangoproject.com/en/2.0/releases/2.0/#id1
EXPECTED_POSTGRES_ENGINE = 'django.db.backends.postgresql'
if DJANGO_VERSION < (2, 0):
EXPECTED_POSTGRES_ENGINE = 'django.db.backends.postgresql_psycopg2'


class DatabaseTestSuite(unittest.TestCase):

def test_postgres_parsing(self):
url = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
url = dj_database_url.parse(url)

assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
Expand All @@ -27,7 +35,7 @@ def test_postgres_unix_socket_parsing(self):
url = 'postgres://%2Fvar%2Frun%2Fpostgresql/d8r82722r2kuvn'
url = dj_database_url.parse(url)

assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == '/var/run/postgresql'
assert url['USER'] == ''
Expand All @@ -37,7 +45,7 @@ def test_postgres_unix_socket_parsing(self):
url = 'postgres://%2FUsers%2Fpostgres%2FRuN/d8r82722r2kuvn'
url = dj_database_url.parse(url)

assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['HOST'] == '/Users/postgres/RuN'
assert url['USER'] == ''
assert url['PASSWORD'] == ''
Expand All @@ -47,7 +55,7 @@ def test_ipv6_parsing(self):
url = 'postgres://ieRaekei9wilaim7:wegauwhgeuioweg@[2001:db8:1234::1234:5678:90af]:5431/d8r82722r2kuvn'
url = dj_database_url.parse(url)

assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == '2001:db8:1234::1234:5678:90af'
assert url['USER'] == 'ieRaekei9wilaim7'
Expand All @@ -57,7 +65,7 @@ def test_ipv6_parsing(self):
def test_postgres_search_path_parsing(self):
url = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?currentSchema=otherschema'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
Expand All @@ -70,7 +78,7 @@ def test_postgres_parsing_with_special_characters(self):
url = 'postgres://%23user:%23password@ec2-107-21-253-135.compute-1.amazonaws.com:5431/%23database'
url = dj_database_url.parse(url)

assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == '#database'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == '#user'
Expand Down Expand Up @@ -142,7 +150,7 @@ def test_database_url(self):

url = dj_database_url.config()

assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
Expand Down Expand Up @@ -196,7 +204,7 @@ def test_database_url_with_options(self):
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['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
Expand Down

0 comments on commit 53c3d79

Please sign in to comment.