def _dump_indexes_from_server()

in index-tool/migrationtools/documentdb_index_tool.py [0:0]


    def _dump_indexes_from_server(self, connection, output_dir, dry_run=False):
        """
        Discover all indexes in a mongodb server and dump them
        to files using the mongodump format
        """

        logging.info("Retrieving indexes from server...")
        try:
            database_info = connection.admin.command({'listDatabases': 1})

            for database_doc in database_info['databases']:
                database_name = database_doc['name']
                logging.debug("Database: %s", database_name)

                if database_name in self.DATABASES_TO_SKIP:
                    continue

                database_path = os.path.join(output_dir, database_name)

                if dry_run is not True:
                    self._mkdir_p(database_path)

                # Write out each collection's stats in this database
                for collection_name in connection[
                        database_name].list_collection_names():
                    logging.debug("Collection: %s", collection_name)
                    collection_metadata = {}
                    collection_metadata[self.OPTIONS] = connection[
                        database_name][collection_name].options()
                    if 'viewOn' in collection_metadata[self.OPTIONS]:
                        # views cannot have indexes, skip to next collection
                        continue
                    collection_indexes = connection[database_name][
                        collection_name].list_indexes()
                    thisIndexes = []
                    for thisIndex in collection_indexes:
                        if "ns" not in thisIndex:
                            # mdb44+ eliminated the "ns" attribute
                            thisIndex["ns"] = "{}.{}".format(database_name,collection_name)
                        thisIndexes.append(thisIndex)
                    collection_metadata[self.INDEXES] = thisIndexes

                    collection_metadata_filename = "{}.{}".format(
                        collection_name, self.METADATA_FILE_SUFFIX_PATTERN)
                    collection_metadata_filepath = os.path.join(
                        database_path, collection_metadata_filename)

                    if dry_run is True:
                        logging.info("\n%s.%s\n%s",
                                     database_name, collection_name,
                                     dumps(collection_metadata))

                    else:
                        logging.debug(
                            "Writing collection metadata for collection: %s",
                            collection_name)
                        with open(collection_metadata_filepath,
                                  'wt') as collection_metadata_file:
                            collection_metadata_file.write(
                                dumps(collection_metadata,
                                      separators=(',', ':')))

            logging.info(
                "Completed writing index metadata to local folder: %s",
                output_dir)

        except Exception:
            logging.exception("Failed to dump indexes from server")
            sys.exit()