Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Preserve MySQL boolean column defaults when changing a column in a mi…

…gration. Closes #5015. [pdcawley@bofh.org.uk]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4340 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 9c5591da2dd9404ac189690ad59670ec7d364b16 1 parent f2b3deb
Marcel Molina authored
2  activerecord/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Preserve MySQL boolean column defaults when changing a column in a migration. Closes #5015. [pdcawley@bofh.org.uk]
+
* PostgreSQL: migrations support :limit with :integer columns by mapping limit < 4 to smallint, > 4 to bigint, and anything else to integer. #2900 [keegan@thebasement.org]
* Dates and times interpret empty strings as nil rather than 2000-01-01. #4830 [kajism@yahoo.com]
4 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
View
@@ -308,7 +308,9 @@ def change_column_default(table_name, column_name, default) #:nodoc:
end
def change_column(table_name, column_name, type, options = {}) #:nodoc:
- options[:default] ||= select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
+ if options[:default].nil?
+ options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
+ end
change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit])}"
add_column_options!(change_column_sql, options)
2  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
View
@@ -243,7 +243,7 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc:
definition[column_name].instance_eval do
self.type = type
self.limit = options[:limit] if options[:limit]
- self.default = options[:default] if options[:default]
+ self.default = options[:default] unless options[:default].nil?
end
end
end
8 activerecord/test/migration_test.rb
View
@@ -1,5 +1,6 @@
require 'abstract_unit'
require 'fixtures/person'
+require 'fixtures/topic'
require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names'
require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders'
@@ -320,6 +321,13 @@ def test_change_column
new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
assert new_columns.find { |c| c.name == 'age' and c.type == :string }
+
+ old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
+ assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
+ assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false }
+ new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
+ assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
+ assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
end
def test_change_column_with_new_default
Please sign in to comment.
Something went wrong with that request. Please try again.