Create a gist now

Instantly share code, notes, and snippets.

Playing around with removing mongo indexes in standard active record migrations.
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