From 6068f3e4cbbd985b0dc309030ec9f8d3c1cf7a96 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 27 Sep 2006 02:42:31 +0000 Subject: [PATCH] Reintroduced the changes from [3855] with more flexible handling of version strings. Refs #2188, #2827. git-svn-id: http://code.djangoproject.com/svn/django/trunk@3872 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/core/management.py | 9 ++++++++- django/db/backends/mysql/base.py | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 3f256aad09..5ff4bc4da0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -119,6 +119,7 @@ answer newbie questions, and generally made Django that much better: Eric Moritz Robin Munn Nebojša Dorđević + Fraser Nevett Sam Newman Neal Norwitz oggie rob diff --git a/django/core/management.py b/django/core/management.py index bcfcd60617..4fa40b5807 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -820,7 +820,8 @@ def get_validation_errors(outfile, app=None): validates all models of all installed apps. Writes errors, if any, to outfile. Returns number of errors. """ - from django.db import models + from django.conf import settings + from django.db import models, connection from django.db.models.loading import get_app_errors from django.db.models.fields.related import RelatedObject @@ -862,6 +863,12 @@ def get_validation_errors(outfile, app=None): if f.db_index not in (None, True, False): e.add(opts, '"%s": "db_index" should be either None, True or False.' % f.name) + # Check that maxlength <= 255 if using older MySQL versions. + if settings.DATABASE_ENGINE == 'mysql': + db_version = connection.get_server_version() + if db_version < (5, 0, 3) and isinstance(f, (models.CharField, models.CommaSeparatedIntegerField, models.SlugField)) and f.maxlength > 255: + e.add(opts, '"%s": %s cannot have a "maxlength" greater than 255 when you are using a version of MySQL prior to 5.0.3 (you are using %s).' % (f.name, f.__class__.__name__, '.'.join(str(n) for n in db_version[:3]))) + # Check to see if the related field will clash with any # existing fields, m2m fields, m2m related objects or related objects if f.rel: diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 23ea76316f..4511869728 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -13,6 +13,7 @@ except ImportError, e: from MySQLdb.converters import conversions from MySQLdb.constants import FIELD_TYPE import types +import re DatabaseError = Database.DatabaseError @@ -24,6 +25,12 @@ django_conversions.update({ FIELD_TYPE.TIME: util.typecast_time, }) +# This should match the numerical portion of the version numbers (we can treat +# versions like 5.0.24 and 5.0.24a as the same). Based on the list of version +# at http://dev.mysql.com/doc/refman/4.1/en/news.html and +# http://dev.mysql.com/doc/refman/5.0/en/news.html . +server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})') + # This is an extra debug layer over MySQL queries, to display warnings. # It's only used when DEBUG=True. class MysqlDebugWrapper: @@ -61,6 +68,7 @@ class DatabaseWrapper(local): def __init__(self): self.connection = None self.queries = [] + self.server_version = None def _valid_connection(self): if self.connection is not None: @@ -110,6 +118,16 @@ class DatabaseWrapper(local): self.connection.close() self.connection = None + def get_server_version(self): + if not self.server_version: + if not self._valid_connection(): + self.cursor() + m = server_version_re.match(self.connection.get_server_info()) + if not m: + raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info()) + self.server_version = m.groups() + return self.server_version + supports_constraints = True def quote_name(name):