Repository: joke2k/django-environ
Pull request #180 (2018-03-28 22:01:01)

 Fix deprecated postgres backend strings
The database backend "django.db.backends.postgresql_psycopg2" has been [deprecated](https://docs.djangoproject.com/en/2.0/releases/2.0/#id1) in Django 2.0 in favor of "django.db.backends.postgresql". There's already some [bug fixes](https://github.com/django/django/commits/2.0.3/django/db/backends/postgresql) that are only available in "django.db.backends.postgresql".

coveralls: 
[![Coverage Status](https://coveralls.io/builds/16235455/badge)](https://coveralls.io/builds/16235455)

Coverage increased (+0.07%) to 88.5% when pulling **3b0955b9179ae1c38ae230af7564415a0f0240dd on mintel:hotfix/psycopg2** into **628ed38e540ec9a6f3aff622953948e54f14a76d on joke2k:develop**.


Patch:
diff --git a/.travis.yml b/.travis.yml
index d43407b..053d8ff 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,6 +4,7 @@ python:
  - "2.7"
  - "3.4"
  - "3.5"
+ - "3.6"
  - "nightly"
  - "pypy"
 env:
@@ -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:
diff --git a/environ/environ.py b/environ/environ.py
index e6a259d..2c8f5ed 100644
--- a/environ/environ.py
+++ b/environ/environ.py
@@ -10,8 +10,11 @@ import sys
 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
 
@@ -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',
@@ -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'
@@ -361,7 +371,7 @@ class Env(object):
         >>> 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):


Test patch:
diff --git a/environ/test.py b/environ/test.py
index 4f7be23..ed867e9 100644
--- a/environ/test.py
+++ b/environ/test.py
@@ -5,6 +5,7 @@ import sys
 import unittest
 import warnings
 
+from django import VERSION as DJANGO_VERSION
 from django.core.exceptions import ImproperlyConfigured
 
 from environ import Env, Path, REDIS_DRIVER
@@ -172,7 +173,10 @@ class EnvTests(BaseTests):
 
     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')
@@ -313,7 +317,10 @@ class DatabaseTestSuite(unittest.TestCase):
         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')