Code

Ticket #2495: 2495-against-13302.diff

File 2495-against-13302.diff, 8.2 KB (added by Honza_Kral, 4 years ago)

another approach to the problem, including rework of #12234

Line 
1commit 645f5dda46705b4c403f2cab0ff634abf92d92d8
2Author: Honza Kral <honza.kral@gmail.com>
3Date:   Thu May 27 17:35:32 2010 +0200
4
5    fixed #2495
6
7diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
8index 492ac1e..06df548 100644
9--- a/django/db/backends/creation.py
10+++ b/django/db/backends/creation.py
11@@ -253,12 +253,22 @@ class BaseDatabaseCreation(object):
12             output.extend(self.sql_indexes_for_field(model, f, style))
13         return output
14 
15-    def sql_indexes_for_field(self, model, f, style):
16-        "Return the CREATE INDEX SQL statements for a single model field"
17+    def compile_sql_index_for_field(self, db_table, f, style, tablespace_sql):
18+        "Compile CREATE INDEX SQL statements for sql_indexes_for_field"
19         from django.db.backends.util import truncate_name
20 
21+        qn = self.connection.ops.quote_name
22+        index_name = '%s_%s' % (db_table, self._digest(f.column))
23+        return [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
24+                style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + ' ' +
25+                style.SQL_KEYWORD('ON') + ' ' +
26+                style.SQL_TABLE(qn(db_table)) + ' ' +
27+                "(%s)" % style.SQL_FIELD(qn(f.column)) +
28+                "%s;" % tablespace_sql]
29+
30+    def sql_indexes_for_field(self, model, f, style):
31+        "Return the CREATE INDEX SQL statements for a single model field"
32         if f.db_index and not f.unique:
33-            qn = self.connection.ops.quote_name
34             tablespace = f.db_tablespace or model._meta.db_tablespace
35             if tablespace:
36                 sql = self.connection.ops.tablespace_sql(tablespace)
37@@ -268,13 +278,7 @@ class BaseDatabaseCreation(object):
38                     tablespace_sql = ''
39             else:
40                 tablespace_sql = ''
41-            i_name = '%s_%s' % (model._meta.db_table, self._digest(f.column))
42-            output = [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
43-                style.SQL_TABLE(qn(truncate_name(i_name, self.connection.ops.max_name_length()))) + ' ' +
44-                style.SQL_KEYWORD('ON') + ' ' +
45-                style.SQL_TABLE(qn(model._meta.db_table)) + ' ' +
46-                "(%s)" % style.SQL_FIELD(qn(f.column)) +
47-                "%s;" % tablespace_sql]
48+            output = self.compile_sql_index_for_field(model._meta.db_table, f, style, tablespace_sql)
49         else:
50             output = []
51         return output
52diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
53index 8b026a9..a3ef8df 100644
54--- a/django/db/backends/mysql/creation.py
55+++ b/django/db/backends/mysql/creation.py
56@@ -63,3 +63,29 @@ class DatabaseCreation(BaseDatabaseCreation):
57                 field.rel.to._meta.db_table, field.rel.to._meta.pk.column)
58             ]
59         return table_output, deferred
60+
61+    def compile_sql_index_for_field(self, db_table, f, style, tablespace_sql):
62+        """
63+        Adds a  prefix length to the TextField which is currently represented
64+        by MySQL's 'longtext' type -- which requires a prefix-length parameter
65+        in order to install an index.
66+
67+        Use largest possible value allowed - 255 characters.
68+        """
69+        from django.db.backends.util import truncate_name
70+
71+        qn = self.connection.ops.quote_name
72+        index_name = '%s_%s' % (db_table, self._digest(f.column))
73+
74+        if f.db_type() == self.data_types['TextField']:
75+            field_name = style.SQL_FIELD(qn(f.column)) + '(255)'
76+        else:
77+            field_name = style.SQL_FIELD(qn(f.column))
78+
79+        return [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
80+                style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + ' ' +
81+                style.SQL_KEYWORD('ON') + ' ' +
82+                style.SQL_TABLE(qn(db_table)) + ' ' +
83+                "(%s)" % field_name +
84+                "%s;" % tablespace_sql]
85+
86diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py
87index af26d0b..3db1506 100644
88--- a/django/db/backends/postgresql/creation.py
89+++ b/django/db/backends/postgresql/creation.py
90@@ -35,41 +35,29 @@ class DatabaseCreation(BaseDatabaseCreation):
91             return "WITH ENCODING '%s'" % self.connection.settings_dict['TEST_CHARSET']
92         return ''
93 
94-    def sql_indexes_for_field(self, model, f, style):
95-        if f.db_index and not f.unique:
96-            qn = self.connection.ops.quote_name
97-            db_table = model._meta.db_table
98-            tablespace = f.db_tablespace or model._meta.db_tablespace
99-            if tablespace:
100-                sql = self.connection.ops.tablespace_sql(tablespace)
101-                if sql:
102-                    tablespace_sql = ' ' + sql
103-                else:
104-                    tablespace_sql = ''
105-            else:
106-                tablespace_sql = ''
107+    def compile_sql_index_for_field(self, db_table, f, style, tablespace_sql):
108+        def _get_index_sql(index_name, opclass=''):
109+            from django.db.backends.util import truncate_name
110 
111-            def get_index_sql(index_name, opclass=''):
112-                return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
113-                        style.SQL_TABLE(qn(index_name)) + ' ' +
114-                        style.SQL_KEYWORD('ON') + ' ' +
115-                        style.SQL_TABLE(qn(db_table)) + ' ' +
116-                        "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
117-                        "%s;" % tablespace_sql)
118+            qn = self.connection.ops.quote_name
119+            return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
120+                style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + ' ' +
121+                style.SQL_KEYWORD('ON') + ' ' +
122+                style.SQL_TABLE(qn(db_table)) + ' ' +
123+                "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
124+                "%s;" % tablespace_sql)
125 
126-            output = [get_index_sql('%s_%s' % (db_table, f.column))]
127+        output = [_get_index_sql('%s_%s' % (db_table, f.column))]
128 
129-            # Fields with database column types of `varchar` and `text` need
130-            # a second index that specifies their operator class, which is
131-            # needed when performing correct LIKE queries outside the
132-            # C locale. See #12234.
133-            db_type = f.db_type()
134-            if db_type.startswith('varchar'):
135-                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
136-                                            ' varchar_pattern_ops'))
137-            elif db_type.startswith('text'):
138-                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
139-                                            ' text_pattern_ops'))
140-        else:
141-            output = []
142+        # Fields with database column types of `varchar` and `text` need
143+        # a second index that specifies their operator class, which is
144+        # needed when performing correct LIKE queries outside the
145+        # C locale. See #12234.
146+        db_type = f.db_type()
147+        if db_type.startswith('varchar'):
148+            output.append(_get_index_sql('%s_%s_like' % (db_table, f.column),
149+                                        ' varchar_pattern_ops'))
150+        elif db_type.startswith('text'):
151+            output.append(_get_index_sql('%s_%s_like' % (db_table, f.column),
152+                                        ' text_pattern_ops'))
153         return output
154diff --git a/tests/regressiontests/bug2495/__init__.py b/tests/regressiontests/bug2495/__init__.py
155new file mode 100644
156index 0000000..e69de29
157diff --git a/tests/regressiontests/bug2495/models.py b/tests/regressiontests/bug2495/models.py
158new file mode 100644
159index 0000000..91661b3
160--- /dev/null
161+++ b/tests/regressiontests/bug2495/models.py
162@@ -0,0 +1,15 @@
163+from unittest import TestCase
164+
165+from django.db import models, connection
166+
167+class IndexedTextModel(models.Model):
168+    indexed_text = models.TextField(db_index=True)
169+
170+class TestTextFieldIndices(TestCase):
171+    def test_indexed_text_has_an_index(self):
172+        try:
173+            indexes = connection.introspection.get_indexes(connection.cursor(), IndexedTextModel._meta.db_table)
174+        except NotImplementedError:
175+            indexes = {}
176+        assert 'indexed_text' in indexes
177+