Skip to content

Commit

Permalink
Add IF EXISTS clause to DROP USER statement (ansible-collections#307)
Browse files Browse the repository at this point in the history
* Add IF EXISTS clause to DROP USER statement

* Add a changelog fragment

* Fix exception

(cherry picked from commit 3a452fa)
  • Loading branch information
Andersson007 committed Mar 15, 2022
1 parent fecc986 commit f9208f6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/307-mysql_user_add_if_exists_to_drop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "mysql_user - fix the possibility for a race condition that breaks certain (circular) replication configurations when ``DROP USER`` is executed on multiple nodes in the replica set. Adding ``IF EXISTS`` avoids the need to use ``sql_log_bin: no`` making the statement always replication safe (https://github.com/ansible-collections/community.mysql/pull/287)."
5 changes: 4 additions & 1 deletion plugins/module_utils/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,10 @@ def user_delete(cursor, user, host, host_all, check_mode):
hostnames = [host]

for hostname in hostnames:
cursor.execute("DROP USER %s@%s", (user, hostname))
try:
cursor.execute("DROP USER IF EXISTS %s@%s", (user, hostname))
except Exception:
cursor.execute("DROP USER %s@%s", (user, hostname))

return True

Expand Down

0 comments on commit f9208f6

Please sign in to comment.