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

This commit is contained in:
Claude Paroz 2013-05-21 11:11:05 +02:00
parent a46a399534
commit 499a745ae1
3 changed files with 30 additions and 4 deletions

View File

@ -8,6 +8,7 @@ from django.db.utils import (DEFAULT_DB_ALIAS,
ProgrammingError, NotSupportedError, DatabaseError, ProgrammingError, NotSupportedError, DatabaseError,
InterfaceError, Error, InterfaceError, Error,
load_backend, ConnectionHandler, ConnectionRouter) load_backend, ConnectionHandler, ConnectionRouter)
from django.utils.functional import cached_property
__all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError', __all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
'IntegrityError', 'DEFAULT_DB_ALIAS') 'IntegrityError', 'DEFAULT_DB_ALIAS')
@ -45,7 +46,28 @@ class DefaultConnectionProxy(object):
return delattr(connections[DEFAULT_DB_ALIAS], name) return delattr(connections[DEFAULT_DB_ALIAS], name)
connection = DefaultConnectionProxy() 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): def close_connection(**kwargs):
warnings.warn( warnings.warn(

View File

@ -373,6 +373,7 @@ these changes.
* The following private APIs will be removed: * The following private APIs will be removed:
- ``django.db.backend``
- ``django.db.close_connection()`` - ``django.db.close_connection()``
- ``django.db.backends.creation.BaseDatabaseCreation.set_autocommit()`` - ``django.db.backends.creation.BaseDatabaseCreation.set_autocommit()``
- ``django.db.transaction.is_managed()`` - ``django.db.transaction.is_managed()``

View File

@ -8,7 +8,7 @@ import threading
from django.conf import settings from django.conf import settings
from django.core.management.color import no_style 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) DatabaseError, IntegrityError, transaction)
from django.db.backends.signals import connection_created from django.db.backends.signals import connection_created
from django.db.backends.postgresql_psycopg2 import version as pg_version from django.db.backends.postgresql_psycopg2 import version as pg_version
@ -50,7 +50,8 @@ class OracleChecks(unittest.TestCase):
def test_dbms_session(self): def test_dbms_session(self):
# If the backend is Oracle, test that we can call a standard # If the backend is Oracle, test that we can call a standard
# stored procedure through our cursor wrapper. # stored procedure through our cursor wrapper.
convert_unicode = backend.convert_unicode from django.db.backends.oracle.base import convert_unicode
cursor = connection.cursor() cursor = connection.cursor()
cursor.callproc(convert_unicode('DBMS_SESSION.SET_IDENTIFIER'), cursor.callproc(convert_unicode('DBMS_SESSION.SET_IDENTIFIER'),
[convert_unicode('_django_testing!')]) [convert_unicode('_django_testing!')])
@ -60,8 +61,10 @@ class OracleChecks(unittest.TestCase):
def test_cursor_var(self): def test_cursor_var(self):
# If the backend is Oracle, test that we can pass cursor variables # If the backend is Oracle, test that we can pass cursor variables
# as query parameters. # as query parameters.
from django.db.backends.oracle.base import Database
cursor = connection.cursor() cursor = connection.cursor()
var = cursor.var(backend.Database.STRING) var = cursor.var(Database.STRING)
cursor.execute("BEGIN %s := 'X'; END; ", [var]) cursor.execute("BEGIN %s := 'X'; END; ", [var])
self.assertEqual(var.getvalue(), 'X') self.assertEqual(var.getvalue(), 'X')