Skip to content

Commit edec1af

Browse files
Elomar Françajosevalim
authored andcommitted
Don't carry default value when changing column for a binary type on MySQL [rails#3234 state:resolved]
Signed-off-by: José Valim <[email protected]>
1 parent 3afdfc3 commit edec1af

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def change_column_null(table_name, column_name, null, default = nil)
513513
def change_column(table_name, column_name, type, options = {}) #:nodoc:
514514
column = column_for(table_name, column_name)
515515

516-
unless options_include_default?(options)
516+
if has_default?(type) && !options_include_default?(options)
517517
options[:default] = column.default
518518
end
519519

@@ -675,6 +675,10 @@ def column_for(table_name, column_name)
675675
end
676676
column
677677
end
678+
679+
def has_default?(sql_type)
680+
sql_type =~ :binary || sql_type == :text #mysql forbids defaults on blob and text columns
681+
end
678682
end
679683
end
680684
end

activerecord/test/cases/migration_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,16 @@ def test_change_column_default
860860
assert_equal "Tester", Person.new.first_name
861861
end
862862

863+
def test_change_column_type_default_should_change
864+
old_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
865+
assert !old_columns.find { |c| c.name == 'data' }
866+
867+
assert_nothing_raised do
868+
Person.connection.add_column "people", "data", :string, :default => ''
869+
Person.connection.change_column "people", "data", :binary
870+
end
871+
end
872+
863873
def test_change_column_quotes_column_names
864874
Person.connection.create_table :testings do |t|
865875
t.column :select, :string

0 commit comments

Comments
 (0)