# Buggy code
def grant_exists(
    grant,
    database,
    user,
    host="localhost",
    grant_option=False,
    escape=True,
    **connection_args,
):
    """
    Checks to see if a grant exists in the database

    CLI Example:

    .. code-block:: bash

        salt '*' mysql.grant_exists \
             'SELECT,INSERT,UPDATE,...' 'database.*' 'frank' 'localhost'
    """
    try:
        target = __grant_generate(grant, database, user, host, grant_option, escape)
    except Exception:
        log.error("Error during grant generation.")
        return False

    grants = user_grants(user, host, **connection_args)

    if grants is False:
        log.error(
            "Grant does not exist or may not be ordered properly. In some cases, "
            "this could also indicate a connection error. Check your configuration."
        )
        return False

    target_tokens = None
    for grant in grants:
        try:
            if not target_tokens:  # Avoid the overhead of re-calc in loop
                target_tokens = _grant_to_tokens(target)
            grant_tokens = _grant_to_tokens(grant)
            if (
                grant_tokens["user"] == target_tokens["user"]
                and grant_tokens["database"] == target_tokens["database"]
                and grant_tokens["host"] == target_tokens["host"]
                and set(grant_tokens["grant"]) >= set(target_tokens["grant"])
            ):
                return True
            else:
                log.debug(
                    "grants mismatch '{0}'<>'{1}'".format(grant_tokens, target_tokens)
                )

        except Exception as exc:  # Fallback to strict parsing
            log.exception(exc)
            if grants is not False and target in grants:
                log.debug("Grant exists.")
                return True

    log.debug("Grant does not exist, or is perhaps not ordered properly?")
    return False


# Traceback (TypeError)
Function: mysql_grants.present
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1560, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/states/mysql_grants.py", line 187, in present
grant, database, user, host, grant_option, escape, ssl_option, **connection_args
File "/usr/lib/python2.7/dist-packages/salt/modules/mysql.py", line 1666, in grant_add
_execute(cur, qry['qry'], qry['args'])
File "/usr/lib/python2.7/dist-packages/salt/modules/mysql.py", line 505, in _execute
return cur.execute(qry, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 159, in execute
query = query % db.literal(args)
TypeError: * wants int

# Fixed code
def grant_exists(
    grant,
    database,
    user,
    host="localhost",
    grant_option=False,
    escape=True,
    **connection_args,
):
    """
    Checks to see if a grant exists in the database

    CLI Example:

    .. code-block:: bash

        salt '*' mysql.grant_exists \
             'SELECT,INSERT,UPDATE,...' 'database.*' 'frank' 'localhost'
    """
    try:
        target = __grant_generate(grant, database, user, host, grant_option, escape)
    except Exception:
        log.error("Error during grant generation.")
        return False

    grants = user_grants(user, host, **connection_args)

    if grants is False:
        log.error(
            "Grant does not exist or may not be ordered properly. In some cases, "
            "this could also indicate a connection error. Check your configuration."
        )
        return False

    target_tokens = None
    for grant in grants:
        try:
            if not target_tokens:  # Avoid the overhead of re-calc in loop
                target_tokens = _grant_to_tokens(target)
            grant_tokens = _grant_to_tokens(grant)
            grant_tokens_database = (
                grant_tokens["database"]
                .replace('"', "")
                .replace("\\", "")
                .replace("`", "")
            )
            target_tokens_database = (
                target_tokens["database"]
                .replace('"', "")
                .replace("\\", "")
                .replace("`", "")
            )
            if (
                grant_tokens["user"] == target_tokens["user"]
                and grant_tokens_database == target_tokens_database
                and grant_tokens["host"] == target_tokens["host"]
                and set(grant_tokens["grant"]) >= set(target_tokens["grant"])
            ):
                return True
            else:
                log.debug(
                    "grants mismatch '{0}'<>'{1}'".format(grant_tokens, target_tokens)
                )

        except Exception as exc:  # Fallback to strict parsing
            log.exception(exc)
            if grants is not False and target in grants:
                log.debug("Grant exists.")
                return True

    log.debug("Grant does not exist, or is perhaps not ordered properly?")
    return False