Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4074

Mongoid::Tasks.remove_indexes claims to remove indexes that aren't actually removed

    • Type: Icon: Task Task
    • Resolution: Done
    • 5.0.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      In the output logged by Mongoid::Tasks.remove_indexes, it includes all indexes defined on the collection.

                  indexes = model.collection.indexes.map{ |doc| doc["name"] }
                  indexes.delete_one("_id_")
                  model.remove_indexes
                  logger.info("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.")
      

      However, internally, model.remove_index only removes indexes if there is an index specification defined on the model:

            def remove_indexes
              indexed_database_names.each do |database|
                collection = with(read: { mode: :primary }, database: database).collection
                begin
                  collection.indexes.each do |spec|
                    unless spec["name"] == "_id_"
                      collection.indexes.drop_one(spec["key"])
                    end
                  end
                rescue Mongo::Error::OperationFailure; end
              end and true
            end
      
            def indexed_database_names
              index_specifications.map do |spec|
                spec.options[:database] || database_name
              end.uniq
            end
      

      This means that removing all of the indexes from a model class will cause it to be excluded when indexes are removed.

      It isn't clear to me whether remove_indexes is meant to also remove_undefined_indexes implicitly. The log output and the docs at http://mongoid.org/en/mongoid/docs/indexing.html seem to imply that it will. It would make sense to me that it should always remove indexes from the primary database.

      cc: @sentience

            Assignee:
            Unassigned Unassigned
            Reporter:
            TimMoore TimMoore [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: