diff --git a/django/db/__init__.py b/django/db/__init__.py index 08c901ab7b..077e1b2d8b 100644 --- a/django/db/__init__.py +++ b/django/db/__init__.py @@ -8,6 +8,7 @@ from django.db.utils import (DEFAULT_DB_ALIAS, 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 @@ class DefaultConnectionProxy(object): 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( diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 5fa7ea16ac..c4bbc3b7a4 100644 --- a/docs/internals/deprecation.txt +++ b/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()`` diff --git a/tests/backends/tests.py b/tests/backends/tests.py index 79e3dd444e..08cae0f7c3 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -8,7 +8,7 @@ import threading 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 @@ class OracleChecks(unittest.TestCase): 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 @@ class OracleChecks(unittest.TestCase): 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')