Skip to content

Instantly share code, notes, and snippets.

@adamrunner
Created March 22, 2018 19:45
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 adamrunner/0214876f6afd1bdf9398589eb16947f5 to your computer and use it in GitHub Desktop.
Save adamrunner/0214876f6afd1bdf9398589eb16947f5 to your computer and use it in GitHub Desktop.
class Mongoid::Migration
# renames a collection in mongodb, can be told to drop the target collection first
def self.rename_collection(old_name, new_name, drop_target=false)
client = Mongoid::Clients.default
current_db = client.database
admin_db = Mongo::Database.new(client, Mongo::Database::ADMIN, current_db.options)
admin_db.command(renameCollection: "#{current_db.name}.#{old_name}",
to: "#{current_db.name}.#{new_name}",
dropTarget: drop_target)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment