Skip to content

Commit

Permalink
Fix deprecated postgres backend strings (#180)
Browse files Browse the repository at this point in the history
* Fix deprecated postgres backend strings

The backend "django.db.backends.postgresql_psycopg2" has been deprecated
in Django 2.0 in favor of "django.db.backends.postgresql".

https://docs.djangoproject.com/en/2.0/releases/2.0/#id1

* Add Django 2.0 to Travis envs

* Add Python 3.6 to Travis

* Exclude Python 3.4 + master Travis test
  • Loading branch information
jtdoepke authored and joke2k committed Jun 5, 2018
1 parent 68ef2a9 commit 34bb11f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "nightly"
- "pypy"
env:
Expand Down Expand Up @@ -47,6 +48,9 @@ matrix:
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
- python: "pypy"
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
# Django master dropped support for Python 3.4.
- 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"
after_success:
Expand Down
20 changes: 15 additions & 5 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import warnings

try:
from django import VERSION as DJANGO_VERSION
from django.core.exceptions import ImproperlyConfigured
except ImportError:
DJANGO_VERSION = None

class ImproperlyConfigured(Exception):
pass

Expand Down Expand Up @@ -69,10 +72,6 @@ class Env(object):
URL_CLASS = urllib.parse.ParseResult
DEFAULT_DATABASE_ENV = 'DATABASE_URL'
DB_SCHEMES = {
'postgres': 'django.db.backends.postgresql_psycopg2',
'postgresql': 'django.db.backends.postgresql_psycopg2',
'psql': '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 @@ -86,6 +85,17 @@ class Env(object):
'sqlite': 'django.db.backends.sqlite3',
'ldap': 'ldapdb.backends.ldap',
}
if DJANGO_VERSION is not None and DJANGO_VERSION < (2, 0):
DB_SCHEMES['postgres'] = 'django.db.backends.postgresql_psycopg2'
DB_SCHEMES['postgresql'] = 'django.db.backends.postgresql_psycopg2'
DB_SCHEMES['psql'] = 'django.db.backends.postgresql_psycopg2'
DB_SCHEMES['pgsql'] = 'django.db.backends.postgresql_psycopg2'
else:
# https://docs.djangoproject.com/en/2.0/releases/2.0/#id1
DB_SCHEMES['postgres'] = 'django.db.backends.postgresql'
DB_SCHEMES['postgresql'] = 'django.db.backends.postgresql'
DB_SCHEMES['psql'] = 'django.db.backends.postgresql'
DB_SCHEMES['pgsql'] = 'django.db.backends.postgresql'
_DB_BASE_OPTIONS = ['CONN_MAX_AGE', 'ATOMIC_REQUESTS', 'AUTOCOMMIT']

DEFAULT_CACHE_ENV = 'CACHE_URL'
Expand Down Expand Up @@ -361,7 +371,7 @@ def db_url_config(cls, url, engine=None):
>>> Env.db_url_config('sqlite:////full/path/to/your/file.sqlite')
{'ENGINE': 'django.db.backends.sqlite3', 'HOST': '', 'NAME': '/full/path/to/your/file.sqlite', 'PASSWORD': '', 'PORT': '', 'USER': ''}
>>> Env.db_url_config('postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn')
{'ENGINE': 'django.db.backends.postgresql_psycopg2', 'HOST': 'ec2-107-21-253-135.compute-1.amazonaws.com', 'NAME': 'd8r82722r2kuvn', 'PASSWORD': 'wegauwhgeuioweg', 'PORT': 5431, 'USER': 'uf07k1i6d8ia0v'}
{'ENGINE': 'django.db.backends.postgresql', 'HOST': 'ec2-107-21-253-135.compute-1.amazonaws.com', 'NAME': 'd8r82722r2kuvn', 'PASSWORD': 'wegauwhgeuioweg', 'PORT': 5431, 'USER': 'uf07k1i6d8ia0v'}
"""
if not isinstance(url, cls.URL_CLASS):
Expand Down
11 changes: 9 additions & 2 deletions environ/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
import warnings

from django import VERSION as DJANGO_VERSION
from django.core.exceptions import ImproperlyConfigured

from environ import Env, Path, REDIS_DRIVER
Expand Down Expand Up @@ -172,7 +173,10 @@ def test_url_encoded_parts(self):

def test_db_url_value(self):
pg_config = self.env.db()
self.assertEqual(pg_config['ENGINE'], 'django.db.backends.postgresql_psycopg2')
if DJANGO_VERSION < (2, 0):
self.assertEqual(pg_config['ENGINE'], 'django.db.backends.postgresql_psycopg2')
else:
self.assertEqual(pg_config['ENGINE'], 'django.db.backends.postgresql')
self.assertEqual(pg_config['NAME'], 'd8r82722')
self.assertEqual(pg_config['HOST'], 'ec2-107-21-253-135.compute-1.amazonaws.com')
self.assertEqual(pg_config['USER'], 'uf07k1')
Expand Down Expand Up @@ -313,7 +317,10 @@ def test_postgres_parsing(self):
url = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
url = Env.db_url_config(url)

self.assertEqual(url['ENGINE'], 'django.db.backends.postgresql_psycopg2')
if DJANGO_VERSION < (2, 0):
self.assertEqual(url['ENGINE'], 'django.db.backends.postgresql_psycopg2')
else:
self.assertEqual(url['ENGINE'], 'django.db.backends.postgresql')
self.assertEqual(url['NAME'], 'd8r82722r2kuvn')
self.assertEqual(url['HOST'], 'ec2-107-21-253-135.compute-1.amazonaws.com')
self.assertEqual(url['USER'], 'uf07k1i6d8ia0v')
Expand Down

0 comments on commit 34bb11f

Please sign in to comment.