Skip to content

Commit

Permalink
Merge pull request pulp#2470 from asmacdo/1738-old-indexes
Browse files Browse the repository at this point in the history
Create mongoengine indexes only after migrations
  • Loading branch information
asmacdo committed Mar 3, 2016
2 parents a4b1a07 + 36cb83a commit ec59a2a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
11 changes: 7 additions & 4 deletions server/pulp/plugins/loader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,15 +432,18 @@ def load_content_types(types_dir=_TYPES_DIR, dry_run=False, drop_indices=False):

# to handle node.json only
descriptors = _load_type_descriptors(types_dir)
definitions = parser.parse(descriptors)
pre_mongoengine_definitions = parser.parse(descriptors)

# get information about content unit types from entry points
definitions += _generate_plugin_definitions()
mongoengine_definitions = _generate_plugin_definitions()

if dry_run:
return _check_content_definitions(definitions)
return _check_content_definitions(pre_mongoengine_definitions + mongoengine_definitions)
else:
database.update_database(definitions, drop_indices=drop_indices)
database.update_database(pre_mongoengine_definitions, drop_indices=drop_indices,
create_indexes=True)
database.update_database(mongoengine_definitions, drop_indices=drop_indices,
create_indexes=False)

# initialization methods -------------------------------------------------------

Expand Down
38 changes: 21 additions & 17 deletions server/pulp/plugins/types/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,21 @@ def __str__(self):
return 'MissingDefinitions [%s]' % ', '.join(self.missing_type_ids)


def update_database(definitions, error_on_missing_definitions=False, drop_indices=False):
def update_database(definitions, error_on_missing_definitions=False, drop_indices=False,
create_indexes=True):
"""
Brings the database up to date with the types defined in the given
descriptors.
@param definitions: set of all definitions
@type definitions: list of L{TypeDefinitions}
:param definitions: set of all definitions
:type definitions: list of L{TypeDefinitions}
@param error_on_missing_definitions: if True, an exception will be raised
:param error_on_missing_definitions: if True, an exception will be raised
if there is one or more type collections already in the database
that are not represented in the given descriptors; defaults to False
@type error_on_missing_definitions: bool
:type error_on_missing_definitions: bool
:param create_indexes: If True, ensure indexes of pre-mongoengine collections
:type create_indexes: bool
"""

all_type_ids = [d.id for d in definitions]
Expand Down Expand Up @@ -101,19 +104,20 @@ def update_database(definitions, error_on_missing_definitions=False, drop_indice
error_defs.append(type_def)
continue

try:
_update_unit_key(type_def)
except Exception:
_logger.exception('Exception updating unit key for type [%s]' % type_def.id)
error_defs.append(type_def)
continue
if create_indexes:
try:
_update_unit_key(type_def)
except Exception:
_logger.exception('Exception updating unit key for type [%s]' % type_def.id)
error_defs.append(type_def)
continue

try:
_update_search_indexes(type_def)
except Exception:
_logger.exception('Exception updating search indexes for type [%s]' % type_def.id)
error_defs.append(type_def)
continue
try:
_update_search_indexes(type_def)
except Exception:
_logger.exception('Exception updating search indexes for type [%s]' % type_def.id)
error_defs.append(type_def)
continue

if len(error_defs) > 0:
raise UpdateFailed(error_defs)
Expand Down
8 changes: 4 additions & 4 deletions server/test/unit/plugins/types/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_update_clean_database(self):

# Test
defs = [DEF_1, DEF_2, DEF_3, DEF_4]
types_db.update_database(defs)
types_db.update_database(defs, create_indexes=True)

# Verify
all_collection_names = types_db.all_type_collection_names()
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_update_no_changes(self):
# Test
# no real reason for this, just felt better than using the previous list
same_defs = [DEF_4, DEF_3, DEF_2, DEF_1]
types_db.update_database(same_defs)
types_db.update_database(same_defs, create_indexes=True)

# Verify
all_collection_names = types_db.all_type_collection_names()
Expand All @@ -81,11 +81,11 @@ def test_update_missing_no_error(self):

# Setup
defs = [DEF_1, DEF_2, DEF_3]
types_db.update_database(defs)
types_db.update_database(defs, create_indexes=True)

# Test
new_defs = [DEF_4]
types_db.update_database(new_defs)
types_db.update_database(new_defs, create_indexes=True)

# Verify
all_collection_names = types_db.all_type_collection_names()
Expand Down

0 comments on commit ec59a2a

Please sign in to comment.