Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #20474 -- Proxied and deprecated django.db.backend

  • Loading branch information...
commit 499a745ae1b53614035b9993b148f32d4ce3f138 1 parent a46a399
@claudep claudep authored
View
24 django/db/__init__.py
@@ -8,6 +8,7 @@
ProgrammingError, NotSupportedError, DatabaseError,
InterfaceError, Error,
load_backend, ConnectionHandler, ConnectionRouter)
+from django.utils.functional import cached_property
__all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
'IntegrityError', 'DEFAULT_DB_ALIAS')
@@ -45,7 +46,28 @@ def __delattr__(self, name):
return delattr(connections[DEFAULT_DB_ALIAS], name)
connection = DefaultConnectionProxy()
-backend = load_backend(connection.settings_dict['ENGINE'])
+
+class DefaultBackendProxy(object):
+ """
+ Temporary proxy class used during deprecation period of the `backend` module
+ variable.
+ """
+ @cached_property
+ def _backend(self):
+ warnings.warn("Accessing django.db.backend is deprecated.",
+ PendingDeprecationWarning, stacklevel=2)
+ return load_backend(connections[DEFAULT_DB_ALIAS].settings_dict['ENGINE'])
+
+ def __getattr__(self, item):
+ return getattr(self._backend, item)
+
+ def __setattr__(self, name, value):
+ return setattr(self._backend, name, value)
+
+ def __delattr__(self, name):
+ return delattr(self._backend, name)
+
+backend = DefaultBackendProxy()
def close_connection(**kwargs):
warnings.warn(
View
1  docs/internals/deprecation.txt
@@ -373,6 +373,7 @@ these changes.
* The following private APIs will be removed:
+ - ``django.db.backend``
- ``django.db.close_connection()``
- ``django.db.backends.creation.BaseDatabaseCreation.set_autocommit()``
- ``django.db.transaction.is_managed()``
View
9 tests/backends/tests.py
@@ -8,7 +8,7 @@
from django.conf import settings
from django.core.management.color import no_style
-from django.db import (backend, connection, connections, DEFAULT_DB_ALIAS,
+from django.db import (connection, connections, DEFAULT_DB_ALIAS,
DatabaseError, IntegrityError, transaction)
from django.db.backends.signals import connection_created
from django.db.backends.postgresql_psycopg2 import version as pg_version
@@ -50,7 +50,8 @@ def test_quote_name(self):
def test_dbms_session(self):
# If the backend is Oracle, test that we can call a standard
# stored procedure through our cursor wrapper.
- convert_unicode = backend.convert_unicode
+ from django.db.backends.oracle.base import convert_unicode
+
cursor = connection.cursor()
cursor.callproc(convert_unicode('DBMS_SESSION.SET_IDENTIFIER'),
[convert_unicode('_django_testing!')])
@@ -60,8 +61,10 @@ def test_dbms_session(self):
def test_cursor_var(self):
# If the backend is Oracle, test that we can pass cursor variables
# as query parameters.
+ from django.db.backends.oracle.base import Database
+
cursor = connection.cursor()
- var = cursor.var(backend.Database.STRING)
+ var = cursor.var(Database.STRING)
cursor.execute("BEGIN %s := 'X'; END; ", [var])
self.assertEqual(var.getvalue(), 'X')
Please sign in to comment.
Something went wrong with that request. Please try again.