# Buggy code
def execute():
    frappe.reload_doc("desk", "doctype", "dashboard_chart")

    if not frappe.db.table_exists("Dashboard Chart"):
        return

    users_with_permission = frappe.get_all(
        "Has Role",
        fields=["parent"],
        filters={
            "role": ["in", ["System Manager", "Dashboard Manager"]],
            "parenttype": "User",
        },
        distinct=True,
        as_list=True,
    )

    users = tuple(
        [
            item if type(item) == str else item.encode("utf8")
            for sublist in users_with_permission
            for item in sublist
        ]
    )

    frappe.db.sql(
        """
		UPDATE
			`tabDashboard Chart`
		SET
			`tabDashboard Chart`.`is_public`=1
		WHERE
			`tabDashboard Chart`.owner in {users}
		""".format(users=users)
    )


# Traceback (pymysql.err.ProgrammingError)
Executing frappe.patches.v13_0.set_existing_dashboard_charts_as_public in v12up.localhost (_b0f4a3ee8e7b5021)
Syntax error in query:

UPDATE
`tabDashboard Chart`
SET
`tabDashboard Chart`.`is_public`=1
WHERE
`tabDashboard Chart`.owner in ('Administrator',)

Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/workspace/development/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 97, in <module>
main()
File "/workspace/development/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 18, in main
click.Group(commands=commands)(prog_name='bench')
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/workspace/development/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 25, in _func
ret = f(frappe._dict(ctx.obj), *args, **kwargs)
File "/workspace/development/frappe-bench/apps/frappe/frappe/commands/site.py", line 252, in migrate
migrate(context.verbose, rebuild_website=rebuild_website, skip_failing=skip_failing)
File "/workspace/development/frappe-bench/apps/frappe/frappe/migrate.py", line 49, in migrate
frappe.modules.patch_handler.run_all(skip_failing)
File "/workspace/development/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 41, in run_all
run_patch(patch)
File "/workspace/development/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 30, in run_patch
if not run_single(patchmodule = patch):
File "/workspace/development/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 71, in run_single
return execute_patch(patchmodule, method, methodargs)
File "/workspace/development/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 91, in execute_patch
frappe.get_attr(patchmodule.split()[0] + ".execute")()
File "/workspace/development/frappe-bench/apps/frappe/frappe/patches/v13_0/set_existing_dashboard_charts_as_public.py", line 28, in execute
""".format(users=users)
File "/workspace/development/frappe-bench/apps/frappe/frappe/database/database.py", line 173, in sql
self._cursor.execute(query)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 6")

# Fixed code
def execute():
    frappe.reload_doc("desk", "doctype", "dashboard_chart")

    if not frappe.db.table_exists("Dashboard Chart"):
        return

    users_with_permission = frappe.get_all(
        "Has Role",
        fields=["parent"],
        filters={
            "role": ["in", ["System Manager", "Dashboard Manager"]],
            "parenttype": "User",
        },
        distinct=True,
    )

    users = [item.parent for item in users_with_permission]
    charts = frappe.db.get_all("Dashboard Chart", filters={"owner": ["in", users]})

    for chart in charts:
        frappe.db.set_value("Dashboard Chart", chart.name, "is_public", 1)