Skip to content
This repository
Browse code

Add more options to column_exists? method

Also fix failures in check options for nil
  • Loading branch information...
commit 59e23e2ff22c0deb770af768c4cc04bd1b2c2ad7 1 parent d79ca92
Aleksey Magusev authored June 30, 2012
10  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -68,10 +68,12 @@ def columns(table_name) end
68 68
       #  column_exists?(:suppliers, :name, :string, :limit => 100)
69 69
       def column_exists?(table_name, column_name, type = nil, options = {})
70 70
         columns(table_name).any?{ |c| c.name == column_name.to_s &&
71  
-                                      (!type                 || c.type == type) &&
72  
-                                      (!options[:limit]      || c.limit == options[:limit]) &&
73  
-                                      (!options[:precision]  || c.precision == options[:precision]) &&
74  
-                                      (!options[:scale]      || c.scale == options[:scale]) }
  71
+                                      (!type                     || c.type == type) &&
  72
+                                      (!options.key?(:limit)     || c.limit == options[:limit]) &&
  73
+                                      (!options.key?(:precision) || c.precision == options[:precision]) &&
  74
+                                      (!options.key?(:scale)     || c.scale == options[:scale]) &&
  75
+                                      (!options.key?(:default)   || c.default == options[:default]) &&
  76
+                                      (!options.key?(:null)      || c.null == options[:null]) }
75 77
       end
76 78
 
77 79
       # Creates a new table with the name +table_name+. +table_name+ may either
18  activerecord/test/cases/migration/change_schema_test.rb
@@ -291,14 +291,20 @@ def test_column_exists_with_type
291 291
 
292 292
       def test_column_exists_with_definition
293 293
         connection.create_table :testings do |t|
294  
-          t.column :foo, :string, :limit => 100
295  
-          t.column :bar, :decimal, :precision => 8, :scale => 2
  294
+          t.column :foo, :string, limit: 100
  295
+          t.column :bar, :decimal, precision: 8, scale: 2
  296
+          t.column :taggable_id, :integer, null: false 
  297
+          t.column :taggable_type, :string, default: 'Photo'
296 298
         end
297 299
 
298  
-        assert connection.column_exists?(:testings, :foo, :string, :limit => 100)
299  
-        refute connection.column_exists?(:testings, :foo, :string, :limit => 50)
300  
-        assert connection.column_exists?(:testings, :bar, :decimal, :precision => 8, :scale => 2)
301  
-        refute connection.column_exists?(:testings, :bar, :decimal, :precision => 10, :scale => 2)
  300
+        assert connection.column_exists?(:testings, :foo, :string, limit: 100)
  301
+        refute connection.column_exists?(:testings, :foo, :string, limit: nil)
  302
+        assert connection.column_exists?(:testings, :bar, :decimal, precision: 8, scale: 2)
  303
+        refute connection.column_exists?(:testings, :bar, :decimal, precision: nil, scale: nil)
  304
+        assert connection.column_exists?(:testings, :taggable_id, :integer, null: false)
  305
+        refute connection.column_exists?(:testings, :taggable_id, :integer, null: true)
  306
+        assert connection.column_exists?(:testings, :taggable_type, :string, default: 'Photo')
  307
+        refute connection.column_exists?(:testings, :taggable_type, :string, default: nil)
302 308
       end
303 309
 
304 310
       def test_column_exists_on_table_with_no_options_parameter_supplied

0 notes on commit 59e23e2

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