Created
February 9, 2011 13:46
-
-
Save chrisroos/818493 to your computer and use it in GitHub Desktop.
Playing around with removing mongo indexes in standard active record migrations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RemoveIndexFromMongoCollection < ActiveRecord::Migration | |
def self.up | |
remove_mongo_index 'my_collection', 'my_index' | |
end | |
def self.down | |
# Intentionally blank | |
end | |
private | |
def self.remove_mongo_index(collection, name, raise_on_missing_indexes=false) | |
begin | |
Mongoid.database.collection(collection).drop_index(name) | |
rescue Mongo::MongoDBError => e | |
if e.message =~ /index not found/ | |
message = "Couldn't find the '#{name}' index in the '#{collection}' collection." | |
raise_on_missing_indexes ? raise("ERROR: #{message}") : puts("WARNING: #{message}") | |
else | |
raise e | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment