Ticket #2495: 2495-against-10914.diff

File 2495-against-10914.diff, 2.4 KB (added by Honza Král, 15 years ago)

Added tests and cleaned up the patch a bit

  • django/db/backends/mysql/creation.py

    commit 3f01282db09f1e618748ed2a8b629eff8529e7a4
    Author: Honza Kral <honza.kral@gmail.com>
    Date:   Fri Jun 5 23:17:15 2009 +0200
    
        Added tests and patch for #2495
    
    diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
    index 0611e01..a1e3cf4 100644
    a b class DatabaseCreation(BaseDatabaseCreation):  
    6363                field.rel.to._meta.db_table, field.rel.to._meta.pk.column)
    6464            ]
    6565        return table_output, deferred
    66        
    67  No newline at end of file
     66
     67    def sql_indexes_for_field(self, model, f, style):
     68        """
     69        Modify the return-value of BaseDatabaseCreation.sql_indexes_for_field()
     70        to add a  prefix length to the TextField which is currently represented
     71        by MySQL's 'longtext' type -- which requires a prefix-length parameter
     72        in order to install an index.
     73
     74        Use largest possible value allowed - 255 characters.
     75
     76        All other types are passed unchanged.
     77        """
     78
     79        out = super(DatabaseCreation, self).sql_indexes_for_field(model, f, style)
     80
     81        if not out or not f.db_type() == self.data_types['TextField']:
     82            return out
     83
     84        qn = self.connection.ops.quote_name
     85        field = "%s" % style.SQL_FIELD(qn(f.column))
     86
     87        newout = []
     88        for item in out:
     89            # 767-byte prefix length maximum for InnoDB
     90            # when using UTF-8 count 3 bytes per character so use 255 as a safe value
     91            newout.append(item.replace(field, "%s(255)" % field))
     92
     93        return newout
     94
  • new file tests/regressiontests/bug2495/models.py

    diff --git a/tests/regressiontests/bug2495/__init__.py b/tests/regressiontests/bug2495/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/bug2495/models.py b/tests/regressiontests/bug2495/models.py
    new file mode 100644
    index 0000000..91661b3
    - +  
     1from unittest import TestCase
     2
     3from django.db import models, connection
     4
     5class IndexedTextModel(models.Model):
     6    indexed_text = models.TextField(db_index=True)
     7
     8class TestTextFieldIndices(TestCase):
     9    def test_indexed_text_has_an_index(self):
     10        try:
     11            indexes = connection.introspection.get_indexes(connection.cursor(), IndexedTextModel._meta.db_table)
     12        except NotImplementedError:
     13            indexes = {}
     14        assert 'indexed_text' in indexes
     15
Back to Top