diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-02-13 12:10:53 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-13 12:10:53 -0300 |
commit | 1c968b484bffb2a7b206b47434f7cf61d10e85a2 (patch) | |
tree | 548b80e13cb1835f023975e158c5cbbdbc55bef2 /activerecord/lib | |
parent | fab91c684be248a36446b6c389865594707e7d57 (diff) | |
parent | 4e35d9fd4fbe7d20bae0939cc90e19d767e30528 (diff) | |
download | rails-1c968b484bffb2a7b206b47434f7cf61d10e85a2.tar.gz rails-1c968b484bffb2a7b206b47434f7cf61d10e85a2.tar.bz2 rails-1c968b484bffb2a7b206b47434f7cf61d10e85a2.zip |
Merge pull request #26655 from kamipo/fix_remove_expression_index
Fix `remove_index` to be able to remove expression indexes
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 28 |
1 files changed, 16 insertions, 12 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 c61aae85d3..bdcdfe4982 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -768,7 +768,7 @@ module ActiveRecord raise ArgumentError, "You must specify the index name" end else - index_name(table_name, column: options) + index_name(table_name, index_name_options(options)) end end @@ -1123,18 +1123,14 @@ module ActiveRecord end def add_index_options(table_name, column_name, comment: nil, **options) # :nodoc: - if column_name.is_a?(String) && /\W/.match?(column_name) - column_names = column_name - else - column_names = Array(column_name) - end + column_names = index_column_names(column_name) options.assert_valid_keys(:unique, :order, :name, :where, :length, :internal, :using, :algorithm, :type) index_type = options[:type].to_s if options.key?(:type) index_type ||= options[:unique] ? "UNIQUE" : "" index_name = options[:name].to_s if options.key?(:name) - index_name ||= index_name(table_name, index_name_options(column_names)) + index_name ||= index_name(table_name, column_names) if options.key?(:algorithm) algorithm = index_algorithms.fetch(options[:algorithm]) { @@ -1214,13 +1210,13 @@ module ActiveRecord if options.is_a?(Hash) checks << lambda { |i| i.name == options[:name].to_s } if options.key?(:name) - column_names = Array(options[:column]).map(&:to_s) + column_names = index_column_names(options[:column]) else - column_names = Array(options).map(&:to_s) + column_names = index_column_names(options) end - if column_names.any? - checks << lambda { |i| i.columns.join("_and_") == column_names.join("_and_") } + if column_names.present? + checks << lambda { |i| index_name(table_name, i.columns) == index_name(table_name, column_names) } end raise ArgumentError, "No name or columns specified" if checks.none? @@ -1267,8 +1263,16 @@ module ActiveRecord AlterTable.new create_table_definition(name) end + def index_column_names(column_names) + if column_names.is_a?(String) && /\W/.match?(column_names) + column_names + else + Array(column_names) + end + end + def index_name_options(column_names) - if column_names.is_a?(String) + if column_names.is_a?(String) && /\W/.match?(column_names) column_names = column_names.scan(/\w+/).join("_") end |