diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2016-01-30 20:26:19 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2016-01-30 20:26:19 +0900 |
commit | 617a693458437d11cea28344598cf264a9c8c47b (patch) | |
tree | 53e6c844fa00e04723d4e40861ebe4f5c6b7e3ec /activerecord/lib | |
parent | 6162c49e40582bf058a6bb82ccc0cfb8f92332b6 (diff) | |
download | rails-617a693458437d11cea28344598cf264a9c8c47b.tar.gz rails-617a693458437d11cea28344598cf264a9c8c47b.tar.bz2 rails-617a693458437d11cea28344598cf264a9c8c47b.zip |
Refactor `column_exists?` in `SchemaStatements`
Diffstat (limited to 'activerecord/lib')
-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 c7aff63228..1591de166e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -110,13 +110,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 |