def db_ensure_indexes(model_classes=None):
    """
    This function ensures that indexes for all the models have been created and the
    extra indexes cleaned up.

    Note #1: When calling this method database connection already needs to be
    established.

    Note #2: This method blocks until all the index have been created (indexes
    are created in real-time and not in background).

    :param model_classes: DB model classes to ensure indexes for. If not specified, indexes are
                          ensured for all the models.
    :type model_classes: ``list``
    """
    LOG.debug("Ensuring database indexes...")

    if not model_classes:
        model_classes = get_model_classes()

    for model_class in model_classes:
        class_name = model_class.__name__

        # Note: We need to ensure / create new indexes before removing extra ones
        try:
            model_class.ensure_indexes()
        except OperationFailure as e:
            # Special case for "uid" index. MongoDB 3.4 has dropped "_types" index option so we
            # need to re-create the index to make it work and avoid "index with different options
            # already exists" error.
            # Note: This condition would only be encountered when upgrading existing StackStorm
            # installation from MongoDB 3.2 to 3.4.
            msg = six.text_type(e)
            if "already exists with different options" in msg and "uid_1" in msg:
                drop_obsolete_types_indexes(model_class=model_class)
            else:
                raise e
        except Exception as e:
            tb_msg = traceback.format_exc()
            msg = 'Failed to ensure indexes for model "%s": %s' % (
                class_name,
                six.text_type(e),
            )
            msg += "\n\n" + tb_msg
            exc_cls = type(e)
            raise exc_cls(msg)

        if model_class.__name__ in INDEX_CLEANUP_MODEL_NAMES_BLACKLIST:
            LOG.debug(
                'Skipping index cleanup for blacklisted model "%s"...' % (class_name)
            )
            continue

        removed_count = cleanup_extra_indexes(model_class=model_class)
        if removed_count:
            LOG.debug(
                'Removed "%s" extra indexes for model "%s"'
                % (removed_count, class_name)
            )

    LOG.debug(
        "Indexes are ensured for models: %s"
        % ", ".join(sorted((model_class.__name__ for model_class in model_classes)))
    )

    This function ensures that indexes for all the models have been created and the
    extra indexes cleaned up.

    Note #1: When calling this method database connection already needs to be
    established.

    Note #2: This method blocks until all the index have been created (indexes
    are created in real-time and not in background).

    :param model_classes: DB model classes to ensure indexes for. If not specified, indexes are
                          ensured for all the models.
    :type model_classes: ``list``
    
st2common/st2common/models/db/__init__.py