Module: Mongoid::Tasks::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/mongoid/tasks/database.rb

Instance Method Summary collapse

Instance Method Details

#create_indexes(models = ::Mongoid.models) ⇒ Array<Class>

Create indexes for each model given the provided globs and the class is not embedded.

Examples:

Create all the indexes.

Mongoid::Tasks::Database.create_indexes

Returns:

  • (Array<Class>)

    The indexed models.

Since:

  • 2.1.0



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/tasks/database.rb', line 16

def create_indexes(models = ::Mongoid.models)
  models.each do |model|
    next if model.index_specifications.empty?
    if !model.embedded? || model.cyclic?
      model.create_indexes
      logger.info("MONGOID: Created indexes on #{model}:")
      model.index_specifications.each do |spec|
        logger.info("MONGOID: Index: #{spec.key}, Options: #{spec.options}")
      end
      model
    else
      logger.info("MONGOID: Index ignored on: #{model}, please define in the root model.")
      nil
    end
  end.compact
end

#remove_indexes(models = ::Mongoid.models) ⇒ Array<Class>

Remove indexes for each model given the provided globs and the class is not embedded.

Examples:

Remove all the indexes.

Mongoid::Tasks::Database.remove_indexes

Returns:

  • (Array<Class>)

    The un-indexed models.



90
91
92
93
94
95
96
97
98
99
# File 'lib/mongoid/tasks/database.rb', line 90

def remove_indexes(models = ::Mongoid.models)
  models.each do |model|
    next if model.embedded?
    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(', ')}.")
    model
  end.compact
end

#remove_undefined_indexes(models = ::Mongoid.models) ⇒ Hash{Class => Array(Hash)}

Remove indexes that exist in the database but aren’t specified on the models.

Examples:

Remove undefined indexes.

Mongoid::Tasks::Database.remove_undefined_indexes

Returns:

  • (Hash{Class => Array(Hash)})

    The list of indexes that were removed by model.

Since:

  • 4.0.0



72
73
74
75
76
77
78
79
80
# File 'lib/mongoid/tasks/database.rb', line 72

def remove_undefined_indexes(models = ::Mongoid.models)
  undefined_indexes(models).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

#undefined_indexes(models = ::Mongoid.models) ⇒ Object

Return the list of indexes by model that exist in the database but aren’t specified on the models.

Examples:

Return the list of unused indexes.

Mongoid::Tasks::Database.undefined_indexes


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mongoid/tasks/database.rb', line 40

def undefined_indexes(models = ::Mongoid.models)
  undefined_by_model = {}

  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