in index-tool/migrationtools/documentdb_index_tool.py [0:0]
def _restore_indexes(self, connection, metadata):
"""Restore compatible indexes to a DocumentDB instance"""
for db_name in metadata:
for collection_name in metadata[db_name]:
for index_name in metadata[db_name][collection_name][
self.INDEXES]:
# convert the keys dict to a list of tuples as pymongo requires
index_keys = metadata[db_name][collection_name][
self.INDEXES][index_name][self.INDEX_KEY]
keys_to_create = []
index_options = {}
index_options[self.INDEX_NAME] = index_name
for key in index_keys:
index_direction = index_keys[key]
if type(index_direction) is float:
index_direction = int(index_direction)
elif type(index_direction) is dict and '$numberInt' in index_direction:
index_direction = int(index_direction['$numberInt'])
elif type(index_direction) is dict and '$numberDouble' in index_direction:
index_direction = int(float(index_direction['$numberDouble']))
keys_to_create.append((key, index_direction))
for k in metadata[db_name][collection_name][
self.INDEXES][index_name]:
if k != self.INDEX_KEY and k != self.INDEX_VERSION:
# this key is an additional index option
index_options[k] = metadata[db_name][
collection_name][self.INDEXES][index_name][k]
if self.args.dry_run is True:
logging.info(
"(dry run) %s.%s: would attempt to add index: %s",
db_name, collection_name, index_name)
logging.info(" (dry run) index options: %s", index_options)
logging.info(" (dry run) index keys: %s", keys_to_create)
else:
logging.debug("Adding index %s -> %s", keys_to_create,
index_options)
database = connection[db_name]
collection = database[collection_name]
collection.create_index(keys_to_create,
**index_options)
logging.info("%s.%s: added index: %s", db_name,
collection_name, index_name)