Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2634 from loic/ticket22424

Fixed #22424 -- MySQL doesn't accept migrations' one-off default values ...
  • Loading branch information...
commit 4e32e47348473757280ca7a8d78edf6011fe65a3 2 parents 1b07781 + 5789ff7
@andrewgodwin andrewgodwin authored
View
27 django/db/backends/mysql/schema.py
@@ -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
10 django/db/backends/schema.py
@@ -118,6 +118,7 @@ def column_sql(self, model, field, include_default=False):
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 @@ def column_sql(self, model, field, include_default=False):
# 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 @@ def add_field(self, model, field):
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
20 django/db/backends/sqlite3/schema.py
@@ -1,3 +1,5 @@
+import codecs
+
from decimal import Decimal
from django.utils import six
from django.apps.registry import Apps
@@ -28,6 +30,16 @@ def quote_value(self, value):
return '"%s"' % six.text_type(value)
elif value is None:
return "NULL"
+ elif isinstance(value, (bytes, bytearray, six.memoryview)):
+ # Bytes are only allowed for BLOB fields, encoded as string
+ # literals containing hexadecimal data and preceded by a single "X"
+ # character:
+ # value = b'\x01\x02' => value_hex = b'0102' => return X'0102'
+ value = bytes(value)
+ hex_encoder = codecs.getencoder('hex_codec')
+ value_hex, _length = hex_encoder(value)
+ # Use 'ascii' encoding for b'01' => '01', no need to use force_text here.
+ return "X'%s'" % value_hex.decode('ascii')
else:
raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value)))
@@ -37,7 +49,9 @@ def _remake_table(self, model, create_fields=[], delete_fields=[], alter_fields=
"""
# Work out the new fields dict / mapping
body = dict((f.name, f) for f in model._meta.local_fields)
- mapping = dict((f.column, f.column) for f in model._meta.local_fields)
+ # Since mapping might mix column names and default values,
+ # its values must be already quoted.
+ mapping = dict((f.column, self.quote_name(f.column)) for f in model._meta.local_fields)
# If any of the new or altered fields is introducing a new PK,
# remove the old one
restore_pk_field = None
@@ -62,7 +76,7 @@ def _remake_table(self, model, create_fields=[], delete_fields=[], alter_fields=
del body[old_field.name]
del mapping[old_field.column]
body[new_field.name] = new_field
- mapping[new_field.column] = old_field.column
+ mapping[new_field.column] = self.quote_name(old_field.column)
# Remove any deleted fields
for field in delete_fields:
del body[field.name]
@@ -90,7 +104,7 @@ def _remake_table(self, model, create_fields=[], delete_fields=[], alter_fields=
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
self.quote_name(temp_model._meta.db_table),
', '.join(self.quote_name(x) for x, y in field_maps),
- ', '.join(self.quote_name(y) for x, y in field_maps),
+ ', '.join(y for x, y in field_maps),
self.quote_name(model._meta.db_table),
))
# Delete the old table (not using self.delete_model to avoid deleting
View
132 tests/migrations/test_operations.py
@@ -1,3 +1,5 @@
+from __future__ import unicode_literals
+
import unittest
try:
@@ -312,6 +314,136 @@ def test_add_field(self):
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"),
+ ),
+ # Manual quoting is fragile and could trip on quotes. Refs #xyz.
+ migrations.AddField(
+ "Pony",
+ "quotes",
+ models.CharField(max_length=10, default='"\'"'),
+ ),
+ ])
+
+ 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")
+ self.assertEqual(pony.quotes, '"\'"')
+
+ 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"),
+ ),
+ # Manual quoting is fragile and could trip on quotes. Refs #xyz.
+ migrations.AddField(
+ "Pony",
+ "quotes",
+ models.TextField(default='"\'"'),
+ ),
+ ])
+
+ 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")
+ self.assertEqual(pony.quotes, '"\'"')
+
+ 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"),
+ ),
+ # Manual quoting is fragile and could trip on quotes. Refs #xyz.
+ migrations.AddField(
+ "Pony",
+ "quotes",
+ models.BinaryField(default=b'"\'"'),
+ ),
+ ])
+
+ 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")
+ self.assertEqual(bytes(pony.quotes), b'"\'"')
+
def test_column_name_quoting(self):
"""
Column names that are SQL keywords shouldn't cause problems when used
Please sign in to comment.
Something went wrong with that request. Please try again.