Permalink
Browse files

activerecord: Avoid mysql non-null text column defaulting to empty st…

…ring.

With the :strict config, which defaults to true, the non-null text
columns behave as expected. So the mysql adapter hack to make it behave as
if the default is an empty string is continuing non-strict mode behaviour
within strict mode.
  • Loading branch information...
1 parent 9eb72ac commit e2b1daf5bb9dcc165feb7ff99d9aab881bf0eaf2 @dylanahsmith committed Sep 28, 2012
@@ -68,17 +68,7 @@ def update(*)
end
def create(*)
- if partial_updates?
- keys = keys_for_partial_update
-
- # This is an extremely bloody annoying necessity to work around mysql being crap.
- # See test_mysql_text_not_null_defaults
- keys.concat self.class.columns.select(&:explicit_default?).map(&:name)
-
- super keys
- else
- super
- end
+ partial_updates? ? super(keys_for_partial_update) : super
end
# Serialized attributes should always be written in case they've been
@@ -14,7 +14,7 @@ def initialize(name, default, sql_type = nil, null = true, collation = nil)
def extract_default(default)
if sql_type =~ /blob/i || type == :text
if default.blank?
- return null ? nil : ''
+ return nil
else
raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
end
@@ -30,10 +30,6 @@ def has_default?
super
end
- def explicit_default?
- !null && (sql_type =~ /blob/i || type == :text)
- end
-
# Must return the relevant concrete adapter
def adapter
raise NotImplementedError
@@ -53,10 +53,6 @@ def has_default?
!default.nil?
end
- def explicit_default?
- false
- end
-
# Returns the Ruby class that corresponds to the abstract data type.
def klass
case type
@@ -78,7 +78,7 @@ def test_should_not_set_default_for_blob_and_text_data_types
assert_equal nil, text_column.default
not_null_text_column = MysqlAdapter::Column.new("title", nil, "text", false)
- assert_equal "", not_null_text_column.default
+ assert_equal nil, not_null_text_column.default
end
def test_has_default_should_return_false_for_blog_and_test_data_types
@@ -112,7 +112,7 @@ def test_should_not_set_default_for_blob_and_text_data_types
assert_equal nil, text_column.default
not_null_text_column = Mysql2Adapter::Column.new("title", nil, "text", false)
- assert_equal "", not_null_text_column.default
+ assert_equal nil, not_null_text_column.default
end
def test_has_default_should_return_false_for_blog_and_test_data_types
@@ -51,10 +51,6 @@ class DefaultsTestWithoutTransactionalFixtures < ActiveRecord::TestCase
# We don't want that to happen, so we disable transactional fixtures here.
self.use_transactional_fixtures = false
- # MySQL 5 and higher is quirky with not null text/blob columns.
- # With MySQL Text/blob columns cannot have defaults. If the column is not
- # null MySQL will report that the column has a null default
- # but it behaves as though the column had a default of ''
def test_mysql_text_not_null_defaults
klass = Class.new(ActiveRecord::Base)
klass.table_name = 'test_mysql_text_not_null_defaults'
@@ -64,14 +60,14 @@ def test_mysql_text_not_null_defaults
t.column :null_text, :text, :null => true
t.column :null_blob, :blob, :null => true
end
- assert_equal '', klass.columns_hash['non_null_blob'].default
- assert_equal '', klass.columns_hash['non_null_text'].default
+ assert_equal nil, klass.columns_hash['non_null_blob'].default
+ assert_equal nil, klass.columns_hash['non_null_text'].default
assert_nil klass.columns_hash['null_blob'].default
assert_nil klass.columns_hash['null_text'].default
assert_nothing_raised do
- instance = klass.create!
+ instance = klass.create!(:non_null_text => '', :non_null_blob => '')
assert_equal '', instance.non_null_text
assert_equal '', instance.non_null_blob
assert_nil instance.null_text
@@ -343,12 +343,7 @@ def test_create_table_with_binary_column
columns = Person.connection.columns(:binary_testings)
data_column = columns.detect { |c| c.name == "data" }
-
- if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
- assert_equal '', data_column.default
- else
- assert_nil data_column.default
- end
+ assert_nil data_column.default
Person.connection.drop_table :binary_testings rescue nil
end

0 comments on commit e2b1daf

Please sign in to comment.