Skip to content
This repository
Browse code

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
authored May 14, 2006
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Preserve MySQL boolean column defaults when changing a column in a migration. Closes #5015. [pdcawley@bofh.org.uk] 
  4
+
3 5
 * 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]
4 6
 
5 7
 * 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
@@ -308,7 +308,9 @@ def change_column_default(table_name, column_name, default) #:nodoc:
308 308
       end
309 309
 
310 310
       def change_column(table_name, column_name, type, options = {}) #:nodoc:
311  
-        options[:default] ||= select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
  311
+        if options[:default].nil?
  312
+          options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
  313
+        end
312 314
         
313 315
         change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit])}"
314 316
         add_column_options!(change_column_sql, options)
2  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -243,7 +243,7 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc:
243 243
           definition[column_name].instance_eval do
244 244
             self.type    = type
245 245
             self.limit   = options[:limit] if options[:limit]
246  
-            self.default = options[:default] if options[:default]
  246
+            self.default = options[:default] unless options[:default].nil?
247 247
           end
248 248
         end
249 249
       end
8  activerecord/test/migration_test.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'abstract_unit'
2 2
 require 'fixtures/person'
  3
+require 'fixtures/topic'
3 4
 require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names'
4 5
 require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders'
5 6
 
@@ -320,6 +321,13 @@ def test_change_column
320 321
       new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
321 322
       assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
322 323
       assert new_columns.find { |c| c.name == 'age' and c.type == :string }
  324
+
  325
+      old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
  326
+      assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
  327
+      assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false }
  328
+      new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
  329
+      assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
  330
+      assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
323 331
     end    
324 332
 
325 333
     def test_change_column_with_new_default

0 notes on commit 9c5591d

Please sign in to comment.
Something went wrong with that request. Please try again.