Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Ensure_indexes on both old & new style mongoengine
Browse files Browse the repository at this point in the history
fixes #916
  • Loading branch information
barnabycourt committed May 1, 2015
1 parent 7a62d4e commit 62c7b60
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
4 changes: 0 additions & 4 deletions server/pulp/plugins/loader/manager.py
Expand Up @@ -4,8 +4,6 @@
import logging
import pkg_resources

from mongoengine import signals

from pulp.common import error_codes
from pulp.plugins.loader import exceptions as loader_exceptions
from pulp.server.db.model import ContentUnit
Expand Down Expand Up @@ -65,8 +63,6 @@ def _load_unit_models(self):

# Attach all the signals
model_class.attach_signals()
signals.post_init.connect(model_class.post_init_signal, sender=model_class)
signals.pre_save.connect(model_class.pre_save_signal, sender=model_class)

_logger.debug(_("Unit Model Loading Completed"))

Expand Down
28 changes: 23 additions & 5 deletions server/pulp/server/db/manage.py
Expand Up @@ -115,16 +115,34 @@ def ensure_database_indexes():
MongoEngine based models require a manual check as they will only create indexes if the
collection does not already exist.
"""
model.RepositoryContentUnit.ensure_indexes()
model.Repository.ensure_indexes()
model.ReservedResource.ensure_indexes()
model.TaskStatus.ensure_indexes()
model.Worker.ensure_indexes()

_ensure_indexes(model.RepositoryContentUnit)
_ensure_indexes(model.Repository)
_ensure_indexes(model.ReservedResource)
_ensure_indexes(model.TaskStatus)
_ensure_indexes(model.Worker)

# Load all the model classes that the server knows about and ensure their inexes as well
plugin_manager = PluginManager()
for model_class in plugin_manager.unit_models.itervalues():
_ensure_indexes(model_class)


def _ensure_indexes(model_class):
"""
Internal helper method to support to support both the old and new mongoengine style
of ensuring indexes on a document model
:param model_class: document model
:type model_class: mongoenigne.Document
"""
if hasattr(model_class, 'ensure_indexes'):
model_class.ensure_indexes()
else:
# Using a private method as there is no public method to access
# the index list in mongoengine 0.7.10, as soon as we can update
# the mongoengine dependency on all platforms this should be removed.
model_class.objects._ensure_indexes()


def main():
Expand Down
37 changes: 30 additions & 7 deletions server/test/unit/server/db/test_manage.py
Expand Up @@ -105,22 +105,45 @@ def clean(self):
super(self.__class__, self).clean()
types_db.clean()

@patch.object(manage, '_ensure_indexes')
@patch.object(manage, 'PluginManager')
@patch.object(manage, 'model')
def test_ensure_index(self, mock_model, mock_plugin_manager):
def test_ensure_database_indexes(self, mock_model, mock_plugin_manager, mock_ensure):
"""
Make sure that the ensure_indexes method is called for all
the appropriate platform models
"""
test_model = MagicMock()
mock_plugin_manager.return_value.unit_models.itervalues.return_value = [test_model]
manage.ensure_database_indexes()
self.assertTrue(mock_model.Repository.ensure_indexes.called)
self.assertTrue(mock_model.RepositoryContentUnit.ensure_indexes.called)
self.assertTrue(mock_model.ReservedResource.ensure_indexes.called)
self.assertTrue(mock_model.TaskStatus.ensure_indexes.called)
self.assertTrue(mock_model.Worker.ensure_indexes.called)
test_model.ensure_indexes.assert_called_once_with()
expected = [call(mock_model.RepositoryContentUnit),
call(mock_model.Repository),
call(mock_model.ReservedResource),
call(mock_model.TaskStatus),
call(mock_model.Worker),
call(test_model)]
self.assertEquals(mock_ensure.call_args_list, expected)

def test_ensure_indexes_old_style(self):
"""
Test the old style mongoengine index assertion where ensure_index is defined
on the queryset
"""
class ModelHelper(object):
def __init__(self):
self.objects = MagicMock()
mock_model = ModelHelper()
manage._ensure_indexes(mock_model)
mock_model.objects._ensure_indexes.assert_called_once_with()

def test_ensure_indexes_new_style(self):
"""
Test the new style mongoengine index assertion where ensure_index is defined
on the Document
"""
mock_model = MagicMock()
manage._ensure_indexes(mock_model)
mock_model.ensure_indexes.assert_called_once_with()

@patch.object(manage, 'ensure_database_indexes')
@patch('logging.config.fileConfig')
Expand Down

0 comments on commit 62c7b60

Please sign in to comment.