Code

Ticket #17760: 17760-3.diff

File 17760-3.diff, 7.2 KB (added by cdestigter, 2 years ago)

minor bug fix for 17760-2.diff

Line 
1diff --git a/django/contrib/gis/db/backends/spatialite/creation.py b/django/contrib/gis/db/backends/spatialite/creation.py
2index 33b6f95..5c97113 100644
3--- a/django/contrib/gis/db/backends/spatialite/creation.py
4+++ b/django/contrib/gis/db/backends/spatialite/creation.py
5@@ -31,9 +31,6 @@ class SpatiaLiteCreation(DatabaseCreation):
6         self.connection.close()
7         self.connection.settings_dict["NAME"] = test_database_name
8 
9-        # Confirm the feature set of the test database
10-        self.connection.features.confirm()
11-
12         # Need to load the SpatiaLite initialization SQL before running `syncdb`.
13         self.load_spatialite_sql()
14 
15diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
16index 7674f5c..4a15e40 100644
17--- a/django/db/backends/__init__.py
18+++ b/django/db/backends/__init__.py
19@@ -10,6 +10,7 @@ from django.conf import settings
20 from django.db import DEFAULT_DB_ALIAS
21 from django.db.backends import util
22 from django.db.transaction import TransactionManagementError
23+from django.utils.functional import cached_property
24 from django.utils.importlib import import_module
25 from django.utils.timezone import is_aware
26 
27@@ -399,12 +400,10 @@ class BaseDatabaseFeatures(object):
28     # in the SQL standard.
29     supports_tablespaces = False
30 
31-    # Features that need to be confirmed at runtime
32-    # Cache whether the confirmation has been performed.
33-    _confirmed = False
34-    supports_transactions = None
35-    supports_stddev = None
36-    can_introspect_foreign_keys = None
37+    # Confirm support for introspected foreign keys
38+    # Every database can do this reliably, except MySQL,
39+    # which can't do it for MyISAM tables
40+    can_introspect_foreign_keys = True
41 
42     # Support for the DISTINCT ON clause
43     can_distinct_on_fields = False
44@@ -412,14 +411,8 @@ class BaseDatabaseFeatures(object):
45     def __init__(self, connection):
46         self.connection = connection
47 
48-    def confirm(self):
49-        "Perform manual checks of any database features that might vary between installs"
50-        self._confirmed = True
51-        self.supports_transactions = self._supports_transactions()
52-        self.supports_stddev = self._supports_stddev()
53-        self.can_introspect_foreign_keys = self._can_introspect_foreign_keys()
54-
55-    def _supports_transactions(self):
56+    @cached_property
57+    def supports_transactions(self):
58         "Confirm support for transactions"
59         cursor = self.connection.cursor()
60         cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
61@@ -432,7 +425,8 @@ class BaseDatabaseFeatures(object):
62         self.connection._commit()
63         return count == 0
64 
65-    def _supports_stddev(self):
66+    @cached_property
67+    def supports_stddev(self):
68         "Confirm support for STDDEV and related stats functions"
69         class StdDevPop(object):
70             sql_function = 'STDDEV_POP'
71@@ -440,12 +434,7 @@ class BaseDatabaseFeatures(object):
72         try:
73             self.connection.ops.check_aggregate_support(StdDevPop())
74         except NotImplementedError:
75-            self.supports_stddev = False
76-
77-    def _can_introspect_foreign_keys(self):
78-        "Confirm support for introspected foreign keys"
79-        # Every database can do this reliably, except MySQL,
80-        # which can't do it for MyISAM tables
81+            return False
82         return True
83 
84 
85diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
86index 2db0acc..a5ed311 100644
87--- a/django/db/backends/creation.py
88+++ b/django/db/backends/creation.py
89@@ -258,9 +258,6 @@ class BaseDatabaseCreation(object):
90         self.connection.close()
91         self.connection.settings_dict["NAME"] = test_database_name
92 
93-        # Confirm the feature set of the test database
94-        self.connection.features.confirm()
95-
96         # Report syncdb messages at one level lower than that requested.
97         # This ensures we don't get flooded with messages during testing
98         # (unless you really ask to be flooded)
99diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
100index 830808b..f3c4cf2 100644
101--- a/django/db/backends/mysql/base.py
102+++ b/django/db/backends/mysql/base.py
103@@ -32,6 +32,7 @@ from django.db.backends.mysql.client import DatabaseClient
104 from django.db.backends.mysql.creation import DatabaseCreation
105 from django.db.backends.mysql.introspection import DatabaseIntrospection
106 from django.db.backends.mysql.validation import DatabaseValidation
107+from django.utils.functional import cached_property
108 from django.utils.safestring import SafeString, SafeUnicode
109 from django.utils.timezone import is_aware, is_naive, utc
110 
111@@ -152,26 +153,25 @@ class DatabaseFeatures(BaseDatabaseFeatures):
112 
113     def __init__(self, connection):
114         super(DatabaseFeatures, self).__init__(connection)
115-        self._storage_engine = None
116 
117+    @cached_property
118     def _mysql_storage_engine(self):
119         "Internal method used in Django tests. Don't rely on this from your code"
120-        if self._storage_engine is None:
121-            cursor = self.connection.cursor()
122-            cursor.execute('CREATE TABLE INTROSPECT_TEST (X INT)')
123-            # This command is MySQL specific; the second column
124-            # will tell you the default table type of the created
125-            # table. Since all Django's test tables will have the same
126-            # table type, that's enough to evaluate the feature.
127-            cursor.execute("SHOW TABLE STATUS WHERE Name='INTROSPECT_TEST'")
128-            result = cursor.fetchone()
129-            cursor.execute('DROP TABLE INTROSPECT_TEST')
130-            self._storage_engine = result[1]
131-        return self._storage_engine
132-
133-    def _can_introspect_foreign_keys(self):
134+        cursor = self.connection.cursor()
135+        cursor.execute('CREATE TABLE INTROSPECT_TEST (X INT)')
136+        # This command is MySQL specific; the second column
137+        # will tell you the default table type of the created
138+        # table. Since all Django's test tables will have the same
139+        # table type, that's enough to evaluate the feature.
140+        cursor.execute("SHOW TABLE STATUS WHERE Name='INTROSPECT_TEST'")
141+        result = cursor.fetchone()
142+        cursor.execute('DROP TABLE INTROSPECT_TEST')
143+        return result[1]
144+
145+    @cached_property
146+    def can_introspect_foreign_keys(self):
147         "Confirm support for introspected foreign keys"
148-        return self._mysql_storage_engine() != 'MyISAM'
149+        return self._mysql_storage_engine != 'MyISAM'
150 
151 class DatabaseOperations(BaseDatabaseOperations):
152     compiler_module = "django.db.backends.mysql.compiler"
153diff --git a/tests/regressiontests/transactions_regress/tests.py b/tests/regressiontests/transactions_regress/tests.py
154index 5972263..abd7a4c 100644
155--- a/tests/regressiontests/transactions_regress/tests.py
156+++ b/tests/regressiontests/transactions_regress/tests.py
157@@ -208,7 +208,7 @@ class SavepointTest(TransactionTestCase):
158         work()
159 
160     @skipIf(connection.vendor == 'mysql' and \
161-            connection.features._mysql_storage_engine() == 'MyISAM',
162+            connection.features._mysql_storage_engine == 'MyISAM',
163             "MyISAM MySQL storage engine doesn't support savepoints")
164     @skipUnlessDBFeature('uses_savepoints')
165     def test_savepoint_rollback(self):