Code

Ticket #18332: ticket18332-patch2.diff

File ticket18332-patch2.diff, 3.7 KB (added by vanessagomes, 23 months ago)

Changind the return of backend_info()

Line 
1diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
2index f26653f..82bbeb7 100644
3--- a/django/db/backends/__init__.py
4+++ b/django/db/backends/__init__.py
5@@ -310,6 +310,13 @@ class BaseDatabaseWrapper(object):
6 
7     def make_debug_cursor(self, cursor):
8         return util.CursorDebugWrapper(cursor, self)
9+       
10+    def backend_info(self):
11+        """
12+        Returns a namedtuple that containd the name and version tuple
13+        from the current database.
14+        """
15+        return self._get_backend_info()
16 
17 class BaseDatabaseFeatures(object):
18     allows_group_by_pk = False
19diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
20index 4fba304..3585591 100644
21--- a/django/db/backends/sqlite3/base.py
22+++ b/django/db/backends/sqlite3/base.py
23@@ -10,7 +10,9 @@ import decimal
24 import warnings
25 import re
26 import sys
27+import sqlite3
28 
29+from collections import namedtuple
30 from django.db import utils
31 from django.db.backends import *
32 from django.db.backends.signals import connection_created
33@@ -26,6 +28,7 @@ try:
34         from pysqlite2 import dbapi2 as Database
35     except ImportError:
36         from sqlite3 import dbapi2 as Database
37+        from sqlite3 import sqlite_version
38 except ImportError as exc:
39     from django.core.exceptions import ImproperlyConfigured
40     raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
41@@ -322,6 +325,11 @@ class DatabaseWrapper(BaseDatabaseWrapper):
42         # an in-memory db.
43         if self.settings_dict['NAME'] != ":memory:":
44             BaseDatabaseWrapper.close(self)
45+   
46+    def _get_backend_info(self):
47+        version_tuple = tuple(map(lambda x: int(x), sqlite_version.split('.')))
48+        BackendInfo = namedtuple('BackendInfo', 'vendor version')
49+        return BackendInfo(self.vendor, version_tuple)
50 
51 FORMAT_QMARK_REGEX = re.compile(r'(?<!%)%s')
52 
53diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt
54index 51e64bd..d8b9aff 100644
55--- a/docs/releases/1.5.txt
56+++ b/docs/releases/1.5.txt
57@@ -33,6 +33,17 @@ version compatible with Python 2.6.
58 What's new in Django 1.5
59 ========================
60 
61+Backend info
62+~~~~~~~~~~~~
63+ *  It was added connection.backend_info() that returns the name and the version of the current database.
64+    Authors of other backends should implement this in their custom backends.
65+
66+ *  Use of backend_info:
67+    >>> from django.db import connection
68+    >>> connection.backend_info()
69+    BackendInfo(vendor='sqlite', version=(3, 7, 2))
70+
71+
72 Support for saving a subset of model's fields
73 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74 
75diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
76index 10e69b6..79bbbe0 100644
77--- a/tests/regressiontests/backends/tests.py
78+++ b/tests/regressiontests/backends/tests.py
79@@ -4,7 +4,7 @@ from __future__ import absolute_import
80 
81 import datetime
82 import threading
83-
84+from sqlite3 import sqlite_version
85 from django.conf import settings
86 from django.core.management.color import no_style
87 from django.core.exceptions import ImproperlyConfigured
88@@ -409,6 +409,11 @@ class BackendTestCase(TestCase):
89         query = 'CREATE TABLE %s (id INTEGER);' % models.Article._meta.db_table
90         with self.assertRaises(DatabaseError):
91             cursor.execute(query)
92+           
93+    def test_backend_info_sqlite(self):
94+        self.assertEqual(connection.vendor, connection.backend_info().vendor)
95+        version_tuple = tuple(map(lambda x: int(x), sqlite_version.split('.')))
96+        self.assertEqual(version_tuple, connection.backend_info().version)           
97 
98 # We don't make these tests conditional because that means we would need to
99 # check and differentiate between: