Skip to content

Instantly share code, notes, and snippets.

@chrisroos
Created February 9, 2011 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisroos/818493 to your computer and use it in GitHub Desktop.
Save chrisroos/818493 to your computer and use it in GitHub Desktop.
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