Permalink
Browse files

AR should respect default values for MySQL BINARY and VARBINARY columns.

[#1273 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent 734e903 commit 5704ecffadffa04981ec5999c362cb691d4a676e @rubymerchant rubymerchant committed with jeremy Aug 10, 2009
View
4 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -76,7 +76,7 @@ def self.mysql_connection(config) # :nodoc:
module ConnectionAdapters
class MysqlColumn < Column #:nodoc:
def extract_default(default)
- if type == :binary || type == :text
+ if sql_type =~ /blob/i || type == :text
if default.blank?
return null ? nil : ''
else
@@ -90,7 +90,7 @@ def extract_default(default)
end
def has_default?
- return false if type == :binary || type == :text #mysql forbids defaults on blob and text columns
+ return false if sql_type =~ /blob/i || type == :text #mysql forbids defaults on blob and text columns
super
end
View
34 activerecord/test/cases/column_definition_test.rb
@@ -33,4 +33,38 @@ def test_should_specify_not_null_if_null_option_is_false
column.limit, column.precision, column.scale, column.default, column.null)
assert_equal %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, column_def.to_sql
end
+
+ if current_adapter?(:MysqlAdapter)
+ def test_should_set_default_for_mysql_binary_data_types
+ binary_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "binary(1)")
+ assert_equal "a", binary_column.default
+
+ varbinary_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "varbinary(1)")
+ assert_equal "a", varbinary_column.default
+ end
+
+ def test_should_not_set_default_for_blob_and_text_data_types
+ assert_raise ArgumentError do
+ ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "blob")
+ end
+
+ assert_raise ArgumentError do
+ ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "Hello", "text")
+ end
+
+ text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text")
+ assert_equal nil, text_column.default
+
+ not_null_text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text", false)
+ assert_equal "", not_null_text_column.default
+ end
+
+ def test_has_default_should_return_false_for_blog_and_test_data_types
+ blob_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "blob")
+ assert !blob_column.has_default?
+
+ text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text")
+ assert !text_column.has_default?
+ end
+ end
end

0 comments on commit 5704ecf

Please sign in to comment.