# Buggy code
def alter_db(name, character_set=None, collate=None, **connection_args):
    """
    Modify database using ``ALTER DATABASE %(dbname)s CHARACTER SET %(charset)s
    COLLATE %(collation)s;`` query.

    CLI Example:

    .. code-block:: bash

        salt '*' mysql.alter_db testdb charset='latin1'
    """
    dbc = _connect(**connection_args)
    if dbc is None:
        return []
    cur = dbc.cursor()
    existing = db_get(name, **connection_args)
    qry = "ALTER DATABASE {0} CHARACTER SET {1} COLLATE {2};".format(
        name.replace("%", r"\%").replace("_", r"\_"),
        character_set or existing.get("character_set"),
        collate or existing.get("collate"),
    )
    args = {}
    _execute(cur, qry, args)


# Traceback (ProgrammingError)
2018-07-11 12:56:40,667 [salt.loaded.int.render.yaml:68  ][DEBUG   ][18693] Results of YAML rendering:
OrderedDict([(u'awesome-db_mysqldb', OrderedDict([(u'mysql_database.present', [OrderedDict([(u'name', u'awesome-db')]), OrderedDict([(u'character_set', u'utf8')]), OrderedDict([(u'collate', u'utf8_general_ci')])])])), (u'awesome-db_user', OrderedDict([(u'mysql_user.present', [OrderedDict([(u'name', u'awesome-db')]), OrderedDict([(u'host', u'localhost')]), OrderedDict([(u'password', u'AWESOME_PASSWD')])])])), (u'awesome-db_awesome-db_fullaccess', OrderedDict([(u'mysql_grants.present', [OrderedDict([(u'database', u'awesome-db.*')]), OrderedDict([(u'user', u'awesome-db')]), OrderedDict([(u'host', u'localhost')]), OrderedDict([(u'grant', u'all privileges')])])]))])
2018-07-11 12:56:40,668 [salt.template    :26  ][PROFILE ][18693] Time (in seconds) to render '/var/cache/salt/minion/files/gnt/mysql.sls' using 'yaml' renderer: 0.00781297683716
2018-07-11 12:56:40,671 [salt.utils.lazy  :100 ][DEBUG   ][18693] LazyLoaded config.option
2018-07-11 12:56:40,681 [salt.utils.lazy  :100 ][DEBUG   ][18693] LazyLoaded mysql.db_exists
2018-07-11 12:56:40,682 [salt.utils.lazy  :100 ][DEBUG   ][18693] LazyLoaded mysql_database.present
2018-07-11 12:56:40,682 [salt.state       :1789][INFO    ][18693] Running state [awesome-db] at time 12:56:40.682877
2018-07-11 12:56:40,683 [salt.state       :1822][INFO    ][18693] Executing state mysql_database.present for [awesome-db]
2018-07-11 12:56:40,687 [salt.loaded.int.module.mysql:547 ][DEBUG   ][18693] Doing query: SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=%(dbname)s; args: {u'dbname': u'awesome-db'}
2018-07-11 12:56:40,689 [salt.loaded.int.states.mysql_database:60  ][DEBUG   ][18693] character set differes from utf8 : latin1
2018-07-11 12:56:40,689 [salt.loaded.int.states.mysql_database:64  ][DEBUG   ][18693] collate set differs from utf8_general_ci : latin1_swedish_ci
2018-07-11 12:56:40,692 [salt.loaded.int.module.mysql:547 ][DEBUG   ][18693] Doing query: SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=%(dbname)s; args: {u'dbname': u'awesome-db'}
2018-07-11 12:56:40,692 [salt.loaded.int.module.mysql:544 ][DEBUG   ][18693] Doing query: ALTER DATABASE awesome-db CHARACTER SET utf8 COLLATE utf8_general_ci;
2018-07-11 12:56:40,695 [salt.state       :310 ][ERROR   ][18693] An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1905, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1830, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/states/mysql_database.py", line 69, in present
**connection_args)
File "/usr/lib/python2.7/dist-packages/salt/modules/mysql.py", line 960, in alter_db
_execute(cur, qry, args)
File "/usr/lib/python2.7/dist-packages/salt/modules/mysql.py", line 545, in _execute
return cur.execute(qry)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-partners CHARACTER SET utf8 COLLATE utf8_general_ci' at line 1")

# Fixed code
def alter_db(name, character_set=None, collate=None, **connection_args):
    """
    Modify database using ``ALTER DATABASE %(dbname)s CHARACTER SET %(charset)s
    COLLATE %(collation)s;`` query.

    CLI Example:

    .. code-block:: bash

        salt '*' mysql.alter_db testdb charset='latin1'
    """
    dbc = _connect(**connection_args)
    if dbc is None:
        return []
    cur = dbc.cursor()
    existing = db_get(name, **connection_args)
    qry = "ALTER DATABASE `{0}` CHARACTER SET {1} COLLATE {2};".format(
        name.replace("%", r"\%").replace("_", r"\_"),
        character_set or existing.get("character_set"),
        collate or existing.get("collate"),
    )
    args = {}
    _execute(cur, qry, args)