Skip to content
This repository
Browse code

change_column accepts :default => nil. Closes #6956.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5937 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 6019c268562d3d1e62c3ad649e81a1803f3e0732 1 parent a491f92
Jeremy Kemper authored January 15, 2007
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* change_column accepts :default => nil.  #6956 [dcmanges, Jeremy Kemper]
  4
+
3 5
 * MySQL, PostgreSQL: change_column_default quotes the default value and doesn't lose column type information.  #3987, #6664 [Jonathan Viney, manfred, altano@bigfoot.com]
4 6
 
5 7
 * Oracle: create_table takes a :sequence_name option to override the 'tablename_seq' default.  #7000 [Michael Schoen]
10  activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -253,9 +253,7 @@ def [](name)
253 253
       #   Requests a maximum column length (<tt>:string</tt>, <tt>:text</tt>,
254 254
       #   <tt>:binary</tt> or <tt>:integer</tt> columns only)
255 255
       # * <tt>:default</tt>:
256  
-      #   The column's default value.  You cannot explicitely set the default
257  
-      #   value to +NULL+.  Simply leave off this option if you want a +NULL+
258  
-      #   default value.
  256
+      #   The column's default value. Use nil for NULL.
259 257
       # * <tt>:null</tt>:
260 258
       #   Allows or disallows +NULL+ values in the column.  This option could
261 259
       #   have been named <tt>:null_allowed</tt>.
@@ -271,11 +269,11 @@ def [](name)
271 269
       #   <tt>:precision</tt>.
272 270
       # * MySQL: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..30]. 
273 271
       #   Default is (10,0).
274  
-      # * PostGres?: <tt>:precision</tt> [1..infinity], 
  272
+      # * PostgreSQL: <tt>:precision</tt> [1..infinity], 
275 273
       #   <tt>:scale</tt> [0..infinity]. No default.
276  
-      # * Sqlite2: Any <tt>:precision</tt> and <tt>:scale</tt> may be used. 
  274
+      # * SQLite2: Any <tt>:precision</tt> and <tt>:scale</tt> may be used. 
277 275
       #   Internal storage as strings. No default.
278  
-      # * Sqlite3: No restrictions on <tt>:precision</tt> and <tt>:scale</tt>,
  276
+      # * SQLite3: No restrictions on <tt>:precision</tt> and <tt>:scale</tt>,
279 277
       #   but the maximum supported <tt>:precision</tt> is 16. No default.
280 278
       # * Oracle: <tt>:precision</tt> [1..38], <tt>:scale</tt> [-84..127]. 
281 279
       #   Default is (38,0).
11  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -273,10 +273,10 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
273 273
           column_type_sql << "(#{limit})" if limit
274 274
           column_type_sql
275 275
         end
276  
-      end            
277  
-    
  276
+      end
  277
+
278 278
       def add_column_options!(sql, options) #:nodoc:
279  
-        sql << " DEFAULT #{quote(options[:default], options[:column])}" unless options[:default].nil?
  279
+        sql << " DEFAULT #{quote(options[:default], options[:column])}" if options_include_default?(options)
280 280
         sql << " NOT NULL" if options[:null] == false
281 281
       end
282 282
 
@@ -293,6 +293,11 @@ def distinct(columns, order_by)
293 293
       def add_order_by_for_association_limiting!(sql, options)
294 294
         sql << "ORDER BY #{options[:order]}"
295 295
       end
  296
+
  297
+      protected
  298
+        def options_include_default?(options)
  299
+          options.include?(:default) && !(options[:null] == false && options[:default].nil?)
  300
+        end
296 301
     end
297 302
   end
298 303
 end
4  activerecord/lib/active_record/connection_adapters/firebird_adapter.rb
@@ -505,8 +505,8 @@ def add_column(table_name, column_name, type, options = {}) # :nodoc:
505 505
 
506 506
       def change_column(table_name, column_name, type, options = {}) # :nodoc:
507 507
         change_column_type(table_name, column_name, type, options)
508  
-        change_column_position(table_name, column_name, options[:position]) if options[:position]
509  
-        change_column_default(table_name, column_name, options[:default]) if options.has_key?(:default)
  508
+        change_column_position(table_name, column_name, options[:position]) if options.include?(:position)
  509
+        change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
510 510
       end
511 511
 
512 512
       def change_column_default(table_name, column_name, default) # :nodoc:
14  activerecord/lib/active_record/connection_adapters/frontbase_adapter.rb
@@ -795,12 +795,12 @@ def add_column(table_name, column_name, type, options = {})
795 795
 
796 796
       def add_column_options!(sql, options) #:nodoc:
797 797
         default_value = quote(options[:default], options[:column])
798  
-        if options[:default]
  798
+        if options_include_default?(options)
799 799
           if options[:type] == :boolean
800 800
             default_value = options[:default] == 0 ? quoted_false : quoted_true
801 801
           end
  802
+          sql << " DEFAULT #{default_value}"
802 803
         end
803  
-        sql << " DEFAULT #{default_value}" unless options[:default].nil?
804 804
         sql << " NOT NULL" if options[:null] == false
805 805
       end
806 806
 
@@ -828,17 +828,15 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc:
828 828
         execute(change_column_sql)
829 829
         change_column_sql = %( ALTER TABLE "#{table_name}" ALTER COLUMN "#{column_name}" )
830 830
 
831  
-        default_value = quote(options[:default], options[:column])
832  
-        if options[:default]
  831
+        if options_include_default?(options)
  832
+          default_value = quote(options[:default], options[:column])
833 833
           if type == :boolean
834 834
             default_value = options[:default] == 0 ? quoted_false : quoted_true
835 835
           end
836  
-        end
837  
-
838  
-        if default_value != "NULL"
839 836
           change_column_sql << " SET DEFAULT #{default_value}"
840  
-          execute(change_column_sql)
841 837
         end
  838
+
  839
+        execute(change_column_sql)
842 840
         
843 841
 #         change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit])}"
844 842
 #         add_column_options!(change_column_sql, options)
4  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -364,10 +364,10 @@ def change_column_default(table_name, column_name, default) #:nodoc:
364 364
       end
365 365
 
366 366
       def change_column(table_name, column_name, type, options = {}) #:nodoc:
367  
-        if options[:default].nil?
  367
+        unless options_include_default?(options)
368 368
           options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
369 369
         end
370  
-        
  370
+
371 371
         change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
372 372
         add_column_options!(change_column_sql, options)
373 373
         execute(change_column_sql)
9  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -321,10 +321,10 @@ def add_column(table_name, column_name, type, options = {})
321 321
         execute("ALTER TABLE #{table_name} ADD COLUMN #{column_name} #{type_to_sql(type, options[:limit])}")
322 322
 
323 323
         # Set optional default. If not null, update nulls to the new default.
324  
-        unless default.nil?
  324
+        if options_include_default?(options)
325 325
           change_column_default(table_name, column_name, default)
326 326
           if notnull
327  
-            execute("UPDATE #{table_name} SET #{column_name}='#{default}' WHERE #{column_name} IS NULL")
  327
+            execute("UPDATE #{table_name} SET #{column_name}=#{quote(default, options[:column])} WHERE #{column_name} IS NULL")
328 328
           end
329 329
         end
330 330
 
@@ -345,7 +345,10 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc:
345 345
           rename_column(table_name, "#{column_name}_ar_tmp", column_name)
346 346
           commit_db_transaction
347 347
         end
348  
-        change_column_default(table_name, column_name, options[:default]) unless options[:default].nil?
  348
+
  349
+        if options_include_default?(options)
  350
+          change_column_default(table_name, column_name, options[:default])
  351
+        end
349 352
       end
350 353
 
351 354
       def change_column_default(table_name, column_name, default) #:nodoc:
5  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -264,10 +264,11 @@ def change_column_default(table_name, column_name, default) #:nodoc:
264 264
 
265 265
       def change_column(table_name, column_name, type, options = {}) #:nodoc:
266 266
         alter_table(table_name) do |definition|
  267
+          include_default = options_include_default?(options)
267 268
           definition[column_name].instance_eval do
268 269
             self.type    = type
269  
-            self.limit   = options[:limit] if options[:limit]
270  
-            self.default = options[:default] unless options[:default].nil?
  270
+            self.limit   = options[:limit] if options.include?(:limit)
  271
+            self.default = options[:default] if include_default
271 272
           end
272 273
         end
273 274
       end
4  activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
@@ -459,9 +459,9 @@ def rename_column(table, column, new_column_name)
459 459
       
460 460
       def change_column(table_name, column_name, type, options = {}) #:nodoc:
461 461
         sql_commands = ["ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"]
462  
-        unless options[:default].nil?
  462
+        if options_include_default?(options)
463 463
           remove_default_constraint(table_name, column_name)
464  
-          sql_commands << "ALTER TABLE #{table_name} ADD CONSTRAINT DF_#{table_name}_#{column_name} DEFAULT #{quote(options[:default])} FOR #{column_name}"
  464
+          sql_commands << "ALTER TABLE #{table_name} ADD CONSTRAINT DF_#{table_name}_#{column_name} DEFAULT #{quote(options[:default], options[:column])} FOR #{column_name}"
465 465
         end
466 466
         sql_commands.each {|c|
467 467
           execute(c)
2  activerecord/lib/active_record/connection_adapters/sybase_adapter.rb
@@ -381,7 +381,7 @@ def remove_index(table_name, options = {})
381 381
       end
382 382
 
383 383
       def add_column_options!(sql, options) #:nodoc:
384  
-        sql << " DEFAULT #{quote(options[:default], options[:column])}" unless options[:default].nil?
  384
+        sql << " DEFAULT #{quote(options[:default], options[:column])}" if options_include_default?(options)
385 385
 
386 386
         if check_null_for_column?(options[:column], sql)
387 387
           sql << (options[:null] == false ? " NOT NULL" : " NULL")
17  activerecord/test/migration_test.rb
@@ -94,8 +94,10 @@ def test_create_table_adds_id
94 94
     end
95 95
 
96 96
     def test_create_table_with_not_null_column
97  
-      Person.connection.create_table :testings do |t|
98  
-        t.column :foo, :string, :null => false
  97
+      assert_nothing_raised do
  98
+        Person.connection.create_table :testings do |t|
  99
+          t.column :foo, :string, :null => false
  100
+        end
99 101
       end
100 102
 
101 103
       assert_raises(ActiveRecord::StatementInvalid) do
@@ -423,6 +425,17 @@ def test_change_column
423 425
       assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
424 426
       assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => true }
425 427
     end
  428
+    
  429
+    def test_change_column_with_nil_default
  430
+      Person.connection.add_column "people", "contributor", :boolean, :default => true
  431
+      Person.reset_column_information
  432
+      assert Person.new.contributor?
  433
+      
  434
+      assert_nothing_raised { Person.connection.change_column "people", "contributor", :boolean, :default => nil }
  435
+      Person.reset_column_information
  436
+      assert !Person.new.contributor?
  437
+      assert_nil Person.new.contributor
  438
+    end
426 439
 
427 440
     def test_change_column_with_new_default
428 441
       Person.connection.add_column "people", "administrator", :boolean, :default => true

0 notes on commit 6019c26

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