Skip to content

Commit 696d87c

Browse files
authored
log details about conflicts when creating mongo indexes (#3199)
* log details about conflicts when creating mongo indexes atm it's not clear what fails and how it can be fixed in db * handle feedback
1 parent e28b4ab commit 696d87c

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

superdesk/core/mongo/mongo_manager.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,12 @@ def create_collection_indexes(
208208
# Duplicate key for unique indexes are generally caused by invalid documents in the collection
209209
# such as multiple documents not having a value for the attribute used for the index
210210
# Log the error so it can be diagnosed and fixed
211-
logger.exception(err)
211+
logger.error(
212+
"Duplicate key error for index '%s' on collection '%s': %s",
213+
index_details.name,
214+
collection.name,
215+
err,
216+
)
212217

213218
if not ignore_duplicate_keys:
214219
raise
@@ -219,8 +224,25 @@ def create_collection_indexes(
219224

220225
# by default, drop the old index with old configuration and
221226
# create the index again with the new configuration.
222-
collection.drop_index(index_details.name)
223-
collection.create_index(keys, **kwargs)
227+
logger.warning(
228+
"Index '%s' on collection '%s' definition (keys/options) has changed, dropping and recreating it.",
229+
index_details.name,
230+
collection.name,
231+
)
232+
try:
233+
collection.drop_index(index_details.name)
234+
collection.create_index(keys, **kwargs)
235+
except OperationFailure as recreate_err:
236+
logger.exception(
237+
"Failed to recreate index '%s' on collection '%s': %s. "
238+
"Please create it manually with: keys=%s, options=%s",
239+
index_details.name,
240+
collection.name,
241+
recreate_err,
242+
keys,
243+
kwargs,
244+
)
245+
raise
224246
else:
225247
raise
226248

0 commit comments

Comments
 (0)