Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
rubymerchant authored and jeremy committed Aug 10, 2009
1 parent 734e903 commit 5704ecf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Expand Up @@ -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
Expand All @@ -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

Expand Down
34 changes: 34 additions & 0 deletions activerecord/test/cases/column_definition_test.rb
Expand Up @@ -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.