Module: Mongoid::Tasks::Database
Instance Method Summary collapse
-
#create_indexes(models = ::Mongoid.models) ⇒ Array<Class>
Create indexes for each model given the provided globs and the class is not embedded.
-
#remove_indexes(models = ::Mongoid.models) ⇒ Array<Class>
Remove indexes for each model given the provided globs and the class is not embedded.
-
#remove_undefined_indexes(models = ::Mongoid.models) ⇒ Hash{Class => Array(Hash)}
Remove indexes that exist in the database but aren’t specified on the models.
-
#undefined_indexes(models = ::Mongoid.models) ⇒ Array<Hash>
Return the list of indexes by model that exist in the database but aren’t specified on the models.
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.
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. || 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.}") 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.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mongoid/tasks/database.rb', line 97 def remove_indexes(models = ::Mongoid.models) models.each do |model| next if model. begin model.remove_indexes rescue Mongo::Error::OperationFailure next end 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mongoid/tasks/database.rb', line 75 def remove_undefined_indexes(models = ::Mongoid.models) undefined_indexes(models).each do |model, indexes| indexes.each do |index| key = index['key'].symbolize_keys collection = model.collection collection.indexes(session: model.send(:_session)).drop_one(key) logger.info( "MONGOID: Removed index '#{index['name']}' on collection " + "'#{collection.name}' in database '#{collection.database.name}'." ) end end end |
#undefined_indexes(models = ::Mongoid.models) ⇒ Array<Hash>
Return the list of indexes by model that exist in the database but aren’t specified on the models.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mongoid/tasks/database.rb', line 41 def undefined_indexes(models = ::Mongoid.models) undefined_by_model = {} models.each do |model| unless model. begin model.collection.indexes(session: model.send(:_session)).each do |index| # ignore default index unless index['name'] == '_id_' key = index['key'].symbolize_keys spec = model.index_specification(key, index['name']) unless spec # index not specified undefined_by_model[model] ||= [] undefined_by_model[model] << index end end end rescue Mongo::Error::OperationFailure; end end end undefined_by_model end |