From d61b6224b09cb2aa6fb75ccecab246f91553ef7b Mon Sep 17 00:00:00 2001
From: Loic Bistuer <loic.bistuer@gmail.com>
Date: Sun, 18 May 2014 11:58:16 +0700
Subject: [PATCH] [1.7.x] Fixed #22424 -- Fixed handling of default values for
 TextField/BinaryField on MySQL.

Thanks syphar for the review and suggestions.

Backport of 1d3d01b4f7 from master
---
 django/db/backends/mysql/schema.py  |  26 ++++++-
 django/db/backends/schema.py        |  10 ++-
 tests/migrations/test_operations.py | 111 ++++++++++++++++++++++++++++
 3 files changed, 145 insertions(+), 2 deletions(-)

diff --git a/django/db/backends/mysql/schema.py b/django/db/backends/mysql/schema.py
index 3d544f14cc..f3f5a047c5 100644
--- a/django/db/backends/mysql/schema.py
+++ b/django/db/backends/mysql/schema.py
@@ -1,4 +1,5 @@
 from django.db.backends.schema import BaseDatabaseSchemaEditor
+from django.db.models import NOT_PROVIDED
 
 
 class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
@@ -28,4 +29,27 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
     def quote_value(self, value):
         # Inner import to allow module to fail to load gracefully
         import MySQLdb.converters
-        return MySQLdb.escape(value, MySQLdb.converters.conversions)
+
+        if isinstance(value, six.string_types):
+            return '"%s"' % six.text_type(value)
+        else:
+            return MySQLdb.escape(value, MySQLdb.converters.conversions)
+
+    def skip_default(self, field):
+        """
+        MySQL doesn't accept default values for longtext and longblob
+        and implicitly treats these columns as nullable.
+        """
+        return field.db_type(self.connection) in {'longtext', 'longblob'}
+
+    def add_field(self, model, field):
+        super(DatabaseSchemaEditor, self).add_field(model, field)
+
+        # Simulate the effect of a one-off default.
+        if self.skip_default(field) and field.default not in {None, NOT_PROVIDED}:
+            effective_default = self.effective_default(field)
+            self.execute('UPDATE %(table)s SET %(column)s=%(default)s' % {
+                'table': self.quote_name(model._meta.db_table),
+                'column': self.quote_name(field.column),
+                'default': self.quote_value(effective_default),
+            })
diff --git a/django/db/backends/schema.py b/django/db/backends/schema.py
index 2a62803a73..d5e10e964d 100644
--- a/django/db/backends/schema.py
+++ b/django/db/backends/schema.py
@@ -118,6 +118,7 @@ class BaseDatabaseSchemaEditor(object):
         null = field.null
         # If we were told to include a default value, do so
         default_value = self.effective_default(field)
+        include_default = include_default and not self.skip_default(field)
         if include_default and default_value is not None:
             if self.connection.features.requires_literal_defaults:
                 # Some databases can't take defaults as a parameter (oracle)
@@ -148,6 +149,13 @@ class BaseDatabaseSchemaEditor(object):
         # Return the sql
         return sql, params
 
+    def skip_default(self, field):
+        """
+        Some backends don't accept default values for certain columns types
+        (i.e. MySQL longtext and longblob).
+        """
+        return False
+
     def prepare_default(self, value):
         """
         Only used for backends which have requires_literal_defaults feature
@@ -398,7 +406,7 @@ class BaseDatabaseSchemaEditor(object):
         self.execute(sql, params)
         # Drop the default if we need to
         # (Django usually does not use in-database defaults)
-        if field.default is not None:
+        if not self.skip_default(field) and field.default is not None:
             sql = self.sql_alter_column % {
                 "table": self.quote_name(model._meta.db_table),
                 "changes": self.sql_alter_column_no_default % {
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 388544cb04..5455610912 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -1,3 +1,5 @@
+from __future__ import unicode_literals
+
 import unittest
 
 try:
@@ -312,6 +314,115 @@ class OperationTests(MigrationTestBase):
             operation.database_backwards("test_adfl", editor, new_state, project_state)
         self.assertColumnNotExists("test_adfl_pony", "height")
 
+    def test_add_charfield(self):
+        """
+        Tests the AddField operation on TextField.
+        """
+        project_state = self.set_up_test_model("test_adchfl")
+
+        new_apps = project_state.render()
+        Pony = new_apps.get_model("test_adchfl", "Pony")
+        pony = Pony.objects.create(weight=42)
+
+        new_state = self.apply_operations("test_adchfl", project_state, [
+            migrations.AddField(
+                "Pony",
+                "text",
+                models.CharField(max_length=10, default="some text"),
+            ),
+            migrations.AddField(
+                "Pony",
+                "empty",
+                models.CharField(max_length=10, default=""),
+            ),
+            # If not properly quoted digits would be interpreted as an int.
+            migrations.AddField(
+                "Pony",
+                "digits",
+                models.CharField(max_length=10, default="42"),
+            ),
+        ])
+
+        new_apps = new_state.render()
+        Pony = new_apps.get_model("test_adchfl", "Pony")
+        pony = Pony.objects.get(pk=pony.pk)
+        self.assertEqual(pony.text, "some text")
+        self.assertEqual(pony.empty, "")
+        self.assertEqual(pony.digits, "42")
+
+    def test_add_textfield(self):
+        """
+        Tests the AddField operation on TextField.
+        """
+        project_state = self.set_up_test_model("test_adtxtfl")
+
+        new_apps = project_state.render()
+        Pony = new_apps.get_model("test_adtxtfl", "Pony")
+        pony = Pony.objects.create(weight=42)
+
+        new_state = self.apply_operations("test_adtxtfl", project_state, [
+            migrations.AddField(
+                "Pony",
+                "text",
+                models.TextField(default="some text"),
+            ),
+            migrations.AddField(
+                "Pony",
+                "empty",
+                models.TextField(default=""),
+            ),
+            # If not properly quoted digits would be interpreted as an int.
+            migrations.AddField(
+                "Pony",
+                "digits",
+                models.TextField(default="42"),
+            ),
+        ])
+
+        new_apps = new_state.render()
+        Pony = new_apps.get_model("test_adtxtfl", "Pony")
+        pony = Pony.objects.get(pk=pony.pk)
+        self.assertEqual(pony.text, "some text")
+        self.assertEqual(pony.empty, "")
+        self.assertEqual(pony.digits, "42")
+
+    def test_add_binaryfield(self):
+        """
+        Tests the AddField operation on TextField/BinaryField.
+        """
+        project_state = self.set_up_test_model("test_adbinfl")
+
+        new_apps = project_state.render()
+        Pony = new_apps.get_model("test_adbinfl", "Pony")
+        pony = Pony.objects.create(weight=42)
+
+        new_state = self.apply_operations("test_adbinfl", project_state, [
+            migrations.AddField(
+                "Pony",
+                "blob",
+                models.BinaryField(default=b"some text"),
+            ),
+            migrations.AddField(
+                "Pony",
+                "empty",
+                models.BinaryField(default=b""),
+            ),
+            # If not properly quoted digits would be interpreted as an int.
+            migrations.AddField(
+                "Pony",
+                "digits",
+                models.BinaryField(default=b"42"),
+            ),
+        ])
+
+        new_apps = new_state.render()
+        Pony = new_apps.get_model("test_adbinfl", "Pony")
+        pony = Pony.objects.get(pk=pony.pk)
+        # SQLite returns buffer/memoryview, cast to bytes for checking.
+        self.assertEqual(bytes(pony.blob), b"some text")
+        self.assertEqual(bytes(pony.empty), b"")
+        self.assertEqual(bytes(pony.digits), b"42")
+
     def test_column_name_quoting(self):
         """
         Column names that are SQL keywords shouldn't cause problems when used