Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 499a745ae1b53614035b9993b148f32d4ce3f138 1 parent a46a399
Claude Paroz authored May 21, 2013
24  django/db/__init__.py
@@ -8,6 +8,7 @@
8 8
     ProgrammingError, NotSupportedError, DatabaseError,
9 9
     InterfaceError, Error,
10 10
     load_backend, ConnectionHandler, ConnectionRouter)
  11
+from django.utils.functional import cached_property
11 12
 
12 13
 __all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
13 14
     'IntegrityError', 'DEFAULT_DB_ALIAS')
@@ -45,7 +46,28 @@ def __delattr__(self, name):
45 46
         return delattr(connections[DEFAULT_DB_ALIAS], name)
46 47
 
47 48
 connection = DefaultConnectionProxy()
48  
-backend = load_backend(connection.settings_dict['ENGINE'])
  49
+
  50
+class DefaultBackendProxy(object):
  51
+    """
  52
+    Temporary proxy class used during deprecation period of the `backend` module
  53
+    variable.
  54
+    """
  55
+    @cached_property
  56
+    def _backend(self):
  57
+        warnings.warn("Accessing django.db.backend is deprecated.",
  58
+            PendingDeprecationWarning, stacklevel=2)
  59
+        return load_backend(connections[DEFAULT_DB_ALIAS].settings_dict['ENGINE'])
  60
+
  61
+    def __getattr__(self, item):
  62
+        return getattr(self._backend, item)
  63
+
  64
+    def __setattr__(self, name, value):
  65
+        return setattr(self._backend, name, value)
  66
+
  67
+    def __delattr__(self, name):
  68
+        return delattr(self._backend, name)
  69
+
  70
+backend = DefaultBackendProxy()
49 71
 
50 72
 def close_connection(**kwargs):
51 73
     warnings.warn(
1  docs/internals/deprecation.txt
@@ -373,6 +373,7 @@ these changes.
373 373
 
374 374
 * The following private APIs will be removed:
375 375
 
  376
+  - ``django.db.backend``
376 377
   - ``django.db.close_connection()``
377 378
   - ``django.db.backends.creation.BaseDatabaseCreation.set_autocommit()``
378 379
   - ``django.db.transaction.is_managed()``
9  tests/backends/tests.py
@@ -8,7 +8,7 @@
8 8
 
9 9
 from django.conf import settings
10 10
 from django.core.management.color import no_style
11  
-from django.db import (backend, connection, connections, DEFAULT_DB_ALIAS,
  11
+from django.db import (connection, connections, DEFAULT_DB_ALIAS,
12 12
     DatabaseError, IntegrityError, transaction)
13 13
 from django.db.backends.signals import connection_created
14 14
 from django.db.backends.postgresql_psycopg2 import version as pg_version
@@ -50,7 +50,8 @@ def test_quote_name(self):
50 50
     def test_dbms_session(self):
51 51
         # If the backend is Oracle, test that we can call a standard
52 52
         # stored procedure through our cursor wrapper.
53  
-        convert_unicode = backend.convert_unicode
  53
+        from django.db.backends.oracle.base import convert_unicode
  54
+
54 55
         cursor = connection.cursor()
55 56
         cursor.callproc(convert_unicode('DBMS_SESSION.SET_IDENTIFIER'),
56 57
                         [convert_unicode('_django_testing!')])
@@ -60,8 +61,10 @@ def test_dbms_session(self):
60 61
     def test_cursor_var(self):
61 62
         # If the backend is Oracle, test that we can pass cursor variables
62 63
         # as query parameters.
  64
+        from django.db.backends.oracle.base import Database
  65
+
63 66
         cursor = connection.cursor()
64  
-        var = cursor.var(backend.Database.STRING)
  67
+        var = cursor.var(Database.STRING)
65 68
         cursor.execute("BEGIN %s := 'X'; END; ", [var])
66 69
         self.assertEqual(var.getvalue(), 'X')
67 70
 

0 notes on commit 499a745

Please sign in to comment.
Something went wrong with that request. Please try again.