diff options
author | Matthew Draper <matthew@trebex.net> | 2015-12-18 14:13:46 +1030 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2015-12-18 14:13:46 +1030 |
commit | c316ce9ba4d67045029c8463dc7e04ea04e9ddad (patch) | |
tree | cc7d3712208d40faf00c07eb4e61915f9de974de /activerecord/lib/active_record/migration/compatibility.rb | |
parent | 4a58aef7e3035e775b4cd6cc65575c8ed02cd8af (diff) | |
parent | 8ceb883b0630e0d010fbef0c621cc9690b0bcad6 (diff) | |
download | rails-c316ce9ba4d67045029c8463dc7e04ea04e9ddad.tar.gz rails-c316ce9ba4d67045029c8463dc7e04ea04e9ddad.tar.bz2 rails-c316ce9ba4d67045029c8463dc7e04ea04e9ddad.zip |
Merge pull request #19456 from greysteil/index-exists-behaviour
Ignore index name in `index_exists?` when not passed a name to check for
Diffstat (limited to 'activerecord/lib/active_record/migration/compatibility.rb')
-rw-r--r-- | activerecord/lib/active_record/migration/compatibility.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/migration/compatibility.rb b/activerecord/lib/active_record/migration/compatibility.rb index 4c8db8a2d5..831bfa2df3 100644 --- a/activerecord/lib/active_record/migration/compatibility.rb +++ b/activerecord/lib/active_record/migration/compatibility.rb @@ -28,6 +28,42 @@ module ActiveRecord options[:null] = true if options[:null].nil? super end + + def index_exists?(table_name, column_name, options = {}) + column_names = Array(column_name).map(&:to_s) + options[:name] = + if options.key?(:name).present? + options[:name].to_s + else + index_name(table_name, column: column_names) + end + super + end + + def remove_index(table_name, options = {}) + index_name = index_name_for_remove(table_name, options) + execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}" + end + + private + + def index_name_for_remove(table_name, options = {}) + index_name = index_name(table_name, options) + + unless index_name_exists?(table_name, index_name, true) + if options.is_a?(Hash) && options.has_key?(:name) + options_without_column = options.dup + options_without_column.delete :column + index_name_without_column = index_name(table_name, options_without_column) + + return index_name_without_column if index_name_exists?(table_name, index_name_without_column, false) + end + + raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist" + end + + index_name + end end class V4_2 < V5_0 |