提交 09625279 编写于 作者: M Matthew Draper

Merge pull request #21317 from greysteil/support-postgres-drop-index-concurrently

Support dropping indexes concurrently in Postgres
* Support dropping indexes concurrently in Postgres.
See http://www.postgresql.org/docs/9.4/static/sql-dropindex.html for more
details.
*Grey Baker*
* Deprecate passing conditions to `ActiveRecord::Relation#delete_all`
and `ActiveRecord::Relation#destroy_all`
......
......@@ -607,10 +607,7 @@ def add_index(table_name, column_name, options = {})
# remove_index :accounts, name: :by_branch_party
#
def remove_index(table_name, options = {})
remove_index!(table_name, index_name_for_remove(table_name, options))
end
def remove_index!(table_name, index_name) #:nodoc:
index_name = index_name_for_remove(table_name, options)
execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
end
......
......@@ -447,8 +447,15 @@ def add_index(table_name, column_name, options = {}) #:nodoc:
execute "CREATE #{index_type} INDEX #{index_algorithm} #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} #{index_using} (#{index_columns})#{index_options}"
end
def remove_index!(table_name, index_name) #:nodoc:
execute "DROP INDEX #{quote_table_name(index_name)}"
def remove_index(table_name, options = {}) #:nodoc:
index_name = index_name_for_remove(table_name, options)
algorithm =
if options.key?(:algorithm)
index_algorithms.fetch(options[:algorithm]) do
raise ArgumentError.new("Algorithm must be one of the following: #{index_algorithms.keys.map(&:inspect).join(', ')}")
end
end
execute "DROP INDEX #{algorithm} #{quote_table_name(index_name)}"
end
# Renames an index of a table. Raises error if length of new
......
......@@ -375,7 +375,8 @@ def primary_key(table_name) #:nodoc:
pks[0]['name']
end
def remove_index!(table_name, index_name) #:nodoc:
def remove_index(table_name, options = {}) #:nodoc:
index_name = index_name_for_remove(table_name, options)
exec_query "DROP INDEX #{quote_column_name(index_name)}"
end
......
......@@ -51,6 +51,15 @@ def test_add_index
assert_equal expected, add_index(:people, :last_name, :unique => true, :where => "state = 'active'", :using => :gist)
end
def test_remove_index
expected = %(DROP INDEX CONCURRENTLY "index_people_on_last_name")
assert_equal expected, remove_index(:people, name: "index_people_on_last_name", algorithm: :concurrently)
assert_raise ArgumentError do
add_index(:people, :last_name, algorithm: :copy)
end
end
private
def method_missing(method_symbol, *arguments)
ActiveRecord::Base.connection.send(method_symbol, *arguments)
......
......@@ -321,11 +321,11 @@ def test_dump_indexes_for_schema_multiple_schemas_in_search_path
def test_with_uppercase_index_name
@connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"
assert_nothing_raised { @connection.remove_index! "things", "#{SCHEMA_NAME}.things_Index"}
assert_nothing_raised { @connection.remove_index "things", name: "#{SCHEMA_NAME}.things_Index"}
@connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"
with_schema_search_path SCHEMA_NAME do
assert_nothing_raised { @connection.remove_index! "things", "things_Index"}
assert_nothing_raised { @connection.remove_index "things", name: "things_Index"}
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册