From 4b3fd424f46d6a6491dd549783b37401dcba89f5 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 2 Apr 2012 20:29:50 +0000 Subject: [PATCH] Fixed #15782 -- Prevented MySQL backend to crash on runserver when db server is down. Thanks toofishes for the report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17868 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/mysql/validation.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/django/db/backends/mysql/validation.py b/django/db/backends/mysql/validation.py index 197f91310e..663cc7da4e 100644 --- a/django/db/backends/mysql/validation.py +++ b/django/db/backends/mysql/validation.py @@ -11,11 +11,17 @@ class DatabaseValidation(BaseDatabaseValidation): characters if they have a unique index on them. """ from django.db import models - db_version = self.connection.get_server_version() + from MySQLdb import OperationalError + try: + db_version = self.connection.get_server_version() + text_version = '.'.join([str(n) for n in db_version[:3]]) + except OperationalError: + db_version = None + text_version = '' varchar_fields = (models.CharField, models.CommaSeparatedIntegerField, models.SlugField) if isinstance(f, varchar_fields) and f.max_length > 255: - if db_version < (5, 0, 3): + if db_version and db_version < (5, 0, 3): msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when you are using a version of MySQL prior to 5.0.3 (you are using %(version)s).' elif f.unique == True: msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when using "unique=True".' @@ -23,4 +29,4 @@ class DatabaseValidation(BaseDatabaseValidation): msg = None if msg: - errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__, 'version': '.'.join([str(n) for n in db_version[:3]])}) + errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__, 'version': text_version})