Skip to content

Commit

Permalink
Merge a19e798 into f5ba129
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed May 31, 2013
2 parents f5ba129 + a19e798 commit 2176ac2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/mongoid/railties/database.rake
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ namespace :db do
::Rails::Mongoid.create_indexes
end

desc "Remove indexes that exist in the database but aren't specified on the models"
task :remove_undefined_indexes => :environment do
::Rails.application.eager_load!
::Rails::Mongoid.remove_undefined_indexes
end

desc "Remove the indexes defined on your mongoid models without questions!"
task :remove_indexes => :environment do
::Rails.application.eager_load!
Expand Down
47 changes: 47 additions & 0 deletions lib/rails/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,53 @@ def create_indexes
end.compact
end

# Return the list of indexes by model that exist in the database but aren't
# specified on the models.
#
# @example Return the list of unused indexes.
# Rails::Mongoid.undefined_indexes
#
# @return Hash{Class => Array(Hash)} The list of undefined indexes by model.
def undefined_indexes
undefined_by_model = {}

::Mongoid.models.each do |model|
unless model.embedded?
model.collection.indexes.each do |index|
# ignore default index
unless index['name'] == '_id_'
key = index['key'].symbolize_keys
spec = model.index_specification(key)
unless spec
# index not specified
undefined_by_model[model] ||= []
undefined_by_model[model] << index
end
end
end
end
end

undefined_by_model
end

# Remove indexes that exist in the database but aren't specified on the
# models.
#
# @example Remove undefined indexes.
# Rails::Mongoid.remove_undefined_indexes
#
# @return Hash{Class => Array(Hash)} The list of indexes that were removed by model.
def remove_undefined_indexes
undefined_indexes.each do |model, indexes|
indexes.each do |index|
key = index['key'].symbolize_keys
model.collection.indexes.drop(key)
logger.info("MONGOID: Removing index: #{index['name']} on #{model}.")
end
end
end

# Remove indexes for each model given the provided globs and the class is
# not embedded.
#
Expand Down
49 changes: 49 additions & 0 deletions spec/rails/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,55 @@
end
end

describe ".undefined_indexes" do

before(:each) do
Rails::Mongoid.create_indexes
end

subject do
Rails::Mongoid.undefined_indexes
end

it { should eq({}) }

context "with extra index on model collection" do
before(:each) do
User.collection.indexes.create(account_expires: 1)
end

its(:keys) { should eq([User]) }

it "should have single index returned" do
names = subject[User].map{ |index| index['name'] }
expect(names).to eq(['account_expires_1'])
end
end
end

describe ".remove_undefined_indexes" do

let(:logger) do
stub
end

let(:indexes) do
User.collection.indexes
end

before(:each) do
Rails::Mongoid.create_indexes
indexes.create(account_expires: 1)
Rails::Mongoid.remove_undefined_indexes
end

subject do
Rails::Mongoid.undefined_indexes
end

it { should eq({}) }
end

describe ".remove_indexes" do

let(:logger) do
Expand Down

0 comments on commit 2176ac2

Please sign in to comment.