Ruby-on-rails 如何创建迁移以仅在索引存在时删除索引,而不是在不存在时抛出异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21795023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a migration to remove an index only if it exists, rather than throwing an exception if it doesn't?
提问by TheFooProgrammer
Right now, the current migration might fail, if the bookstable doesn't have created_ator updated_atfields:
现在,如果books表没有created_at或updated_at字段,当前迁移可能会失败:
class AddTimestampIndexes < ActiveRecord::Migration
def up
remove_index :books, :created_at
remove_index :books, :updated_at
add_index :books, :created_at
add_index :books, :updated_at
end
def down
remove_index :books, :created_at
remove_index :books, :updated_at
end
end
Does remove_indextake any options to silently proceed if it fails to remove the index rather than raising an error?
remove_index如果无法删除索引而不是引发错误,是否采取任何选择静默处理?
回答by Jon
You can use the index_exists?method within your migration to test whether the index you need to remove is actually there.
您可以index_exists?在迁移中使用该方法来测试您需要删除的索引是否确实存在。
Take a look at the documentation here: http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/index_exists%3F
看看这里的文档:http: //apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/index_exists%3F
I've not tested it, but you should be able to use something like this:
我还没有测试过,但你应该能够使用这样的东西:
class AddTimestampIndexes < ActiveRecord::Migration
def up
remove_index :books, :created_at if index_exists?(:books, :created_at)
remove_index :books, :updated_at if index_exists?(:books, :updated_at)
add_index :books, :created_at
add_index :books, :updated_at
end
def down
remove_index :books, :created_at
remove_index :books, :updated_at
end
end
Although, by the looks of things, you really only want to create them if they don't exist? This might be more appropriate for your migration:
虽然,从表面上看,你真的只想在它们不存在的情况下创造它们?这可能更适合您的迁移:
class AddTimestampIndexes < ActiveRecord::Migration
def up
add_index :books, :created_at unless index_exists?(:books, :created_at)
add_index :books, :updated_at unless index_exists?(:books, :updated_at)
end
def down
remove_index :books, :created_at
remove_index :books, :updated_at
end
end
回答by dvvrt
There is also index_name_exists?(table_name, index_name)method which let's you check for an index by it's name. It's helpful for checking for existence of multi-column indexes.
还有一种index_name_exists?(table_name, index_name)方法可以让您通过名称检查索引。它有助于检查多列索引的存在。
Documentation - index_name_exists
文档 - index_name_exists

