0
0
mirror of https://github.com/django/django.git synced 2024-11-29 14:46:18 +01:00

Fixed #22424 -- Fixed handling of default values for TextField/BinaryField on MySQL.

Thanks syphar for the review and suggestions.
This commit is contained in:
Loic Bistuer 2014-05-18 11:58:16 +07:00
parent 6aacb4c991
commit 1d3d01b4f7
3 changed files with 146 additions and 2 deletions

View File

@ -1,4 +1,6 @@
from django.db.backends.schema import BaseDatabaseSchemaEditor
from django.db.models import NOT_PROVIDED
from django.utils import six
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
@ -28,4 +30,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),
})

View File

@ -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 % {

View File

@ -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