diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-01-30 12:20:02 -0500 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-01-30 12:20:02 -0500 |
commit | 33848384f37b0acefc025ec72fc7896b3a3fdf88 (patch) | |
tree | 811314c18c0063ce26ca4aa18ab25581d8837da1 | |
parent | b789f6dff4e67204f66175155a74c5f74d93c92e (diff) | |
parent | 617a693458437d11cea28344598cf264a9c8c47b (diff) | |
download | rails-33848384f37b0acefc025ec72fc7896b3a3fdf88.tar.gz rails-33848384f37b0acefc025ec72fc7896b3a3fdf88.tar.bz2 rails-33848384f37b0acefc025ec72fc7896b3a3fdf88.zip |
Merge pull request #23349 from kamipo/refactor_column_exists
Refactor `column_exists?` in `SchemaStatements`
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index efa427fa88..c00b629632 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -112,13 +112,14 @@ module ActiveRecord # def column_exists?(table_name, column_name, type = nil, options = {}) column_name = column_name.to_s - columns(table_name).any?{ |c| c.name == column_name && - (!type || c.type == type) && - (!options.key?(:limit) || c.limit == options[:limit]) && - (!options.key?(:precision) || c.precision == options[:precision]) && - (!options.key?(:scale) || c.scale == options[:scale]) && - (!options.key?(:default) || c.default == options[:default]) && - (!options.key?(:null) || c.null == options[:null]) } + checks = [] + checks << lambda { |c| c.name == column_name } + checks << lambda { |c| c.type == type } if type + [:limit, :precision, :scale, :default, :null].each do |attr| + checks << lambda { |c| c.send(attr) == options[attr] } if options.key?(attr) + end + + columns(table_name).any? { |c| checks.all? { |check| check[c] } } end # Returns just a table's primary key |