Ticket #2188: Option 3.diff

File Option 3.diff, 4.4 KB (added by Fraser Nevett <mail@…>, 18 years ago)

Uses text instead of varchar when necessary

  • core/management.py

     
    134134
    135135    Returns list_of_sql, pending_references_dict
    136136    """
    137     from django.db import backend, get_creation_module, models
     137    from django.db import backend, connection, get_creation_module, models
    138138    data_types = get_creation_module().DATA_TYPES
    139139
    140140    opts = klass._meta
     
    151151        col_type = data_types[data_type]
    152152        if col_type is not None:
    153153            # Make the definition (e.g. 'foo VARCHAR(30)') for this field.
     154            if type(col_type).__name__ == 'function':
     155                col_type = col_type(rel_field.__dict__, connection)
    154156            field_output = [style.SQL_FIELD(backend.quote_name(f.column)),
    155157                style.SQL_COLTYPE(col_type % rel_field.__dict__)]
    156158            field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or '')))
  • db/backends/mysql/base.py

     
    1212    raise ImproperlyConfigured, "Error loading MySQLdb module: %s" % e
    1313from MySQLdb.converters import conversions
    1414from MySQLdb.constants import FIELD_TYPE
    15 import types
     15import types, re
    1616
    1717DatabaseError = Database.DatabaseError
    1818
     
    6161    def __init__(self):
    6262        self.connection = None
    6363        self.queries = []
     64        self.server_version = None
    6465
    6566    def _valid_connection(self):
    6667        if self.connection is not None:
     
    9596            return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self)
    9697        return cursor
    9798
     99    def get_server_version(self):
     100        if not self.server_version:
     101            if not self._valid_connection():
     102                self.cursor()
     103            version = re.compile('[\.-]').split(self.connection.get_server_info())
     104            self.server_version = tuple(int(x) for x in version[:3]) + tuple(version[3:])
     105        return self.server_version
     106
    98107    def _commit(self):
    99108        self.connection.commit()
    100109
  • db/backends/mysql/creation.py

     
    11# This dictionary maps Field objects to their associated MySQL column
    2 # types, as strings. Column-type strings can contain format strings; they'll
    3 # be interpolated against the values of Field.__dict__ before being output.
    4 # If a column type is set to None, it won't be included in the output.
     2# types, as strings. Column-type strings can contain format strings or lambda
     3# functions. Any lambda functions will be passed two arguments, the first being
     4# Field.__dict__ and the second a database connection, and shoud return a
     5# string. In either case, the string will be interpolated against the values of
     6# Field.__dict__ before being output. If a column type is set to None, it won't
     7# be included in the output.
    58DATA_TYPES = {
    69    'AutoField':         'integer AUTO_INCREMENT',
    710    'BooleanField':      'bool',
    8     'CharField':         'varchar(%(maxlength)s)',
    9     'CommaSeparatedIntegerField': 'varchar(%(maxlength)s)',
     11    'CharField':         lambda details, connection:
     12                             (details['maxlength'] > 255 and connection.get_server_version() < (5, 0, 3) and 'text')
     13                             or 'varchar(%(maxlength)s)',
     14    'CommaSeparatedIntegerField': lambda details, connection:
     15                                      (details['maxlength'] > 255 and connection.get_server_version() < (5, 0, 3) and 'text')
     16                                      or 'varchar(%(maxlength)s)',
    1017    'DateField':         'date',
    1118    'DateTimeField':     'datetime',
    1219    'FileField':         'varchar(100)',
     
    2128    'PhoneNumberField':  'varchar(20)',
    2229    'PositiveIntegerField': 'integer UNSIGNED',
    2330    'PositiveSmallIntegerField': 'smallint UNSIGNED',
    24     'SlugField':         'varchar(%(maxlength)s)',
     31    'SlugField':         lambda details, connection:
     32                             (details['maxlength'] > 255 and connection.get_server_version() < (5, 0, 3) and 'text')
     33                             or 'varchar(%(maxlength)s)',
    2534    'SmallIntegerField': 'smallint',
    2635    'TextField':         'longtext',
    2736    'TimeField':         'time',
Back to Top