Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql_user: refactor to reduce execute() calls #76

Merged

Conversation

steveteahan
Copy link
Contributor

SUMMARY

This module does not currently log the SQL statements that it executes.
A change was proposed to add this functionality, but it would require
modifications in many sections of the code due to how many cursor.execute()
statements there currently are. This change simply consolidates the
number of execute() calls where it is trivial to do so.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

mysql_user

ADDITIONAL INFORMATION

Without this change, logging the queries in my change proposed in #75 would either look like:

# repeating the query text and prone to error on changes
log_query(*mogrify("CREATE USER %s@%s IDENTIFIED BY PASSWORD %s", (user, host, password), tls_requires))
cursor.execute(*mogrify("CREATE USER %s@%s IDENTIFIED BY PASSWORD %s", (user, host, password), tls_requires))

or

# avoids repeating the query, but turns 1 line into 3 everywhere where the query will be logged (7 times in `user_add` alone)
query_with_args = mogrify("CREATE USER %s@%s IDENTIFIED BY PASSWORD %s", (user, host, password), tls_requires)
log_query(*query_with_args)
cursor.execute(*query_with_args)

Overall, even if I were to create a execute_and_log method, the proposed changes would still be an improvement in readability.

@steveteahan steveteahan force-pushed the mysql-user-query-refact branch from 051df16 to e3a91cb Compare December 27, 2020 00:46
@codecov
Copy link

codecov bot commented Dec 27, 2020

Codecov Report

Merging #76 (389eef0) into main (0690771) will increase coverage by 0.06%.
The diff coverage is 95.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #76      +/-   ##
==========================================
+ Coverage   75.01%   75.07%   +0.06%     
==========================================
  Files          12       12              
  Lines        1641     1645       +4     
  Branches      423      423              
==========================================
+ Hits         1231     1235       +4     
  Misses        267      267              
  Partials      143      143              
Impacted Files Coverage Δ
plugins/modules/mysql_user.py 78.57% <95.00%> (+0.18%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0690771...389eef0. Read the comment docs.

This module does not currently log the SQL statements that it executes.
A change was proposed to add this functionality, but it would require
modifications in many sections of the code due to how many cursor.execute()
statements there currently are. This change simply consolidates the
number of execute() calls where it is trivial to do so.
@steveteahan steveteahan force-pushed the mysql-user-query-refact branch from e3a91cb to 9900b11 Compare December 27, 2020 01:01
@steveteahan
Copy link
Contributor Author

It doesn't look like the plugin parameters had any testing. The encrypted parameter test was dropped here: https://github.com/ansible/ansible/pull/25825/files

I'm assuming that I'll hit this issue in #75 even without refactoring due to the number of plugin-related branches. I'm not very familiar with other auth plugins, but I can take a look at what it'd take to fix the encrypted test and add a tests for plugins.

@Andersson007
Copy link
Collaborator

from my personal experience: we should cover all changed parts:), i.e. to keep CI green

steveteahan added a commit to steveteahan/community.mysql that referenced this pull request Dec 30, 2020
The purpose of this change was originally to expand test coverage to
unblock ansible-collections#76, but an issue was detected with the encrypted parameter on
MySQL 8.0 in the process of writing the tests. Additionally,
user_password_update_test.yml had been disabled at some point, so I
opted to replace it with two new files that will focus on the password
and plugin auth paths.
steveteahan added a commit to steveteahan/community.mysql that referenced this pull request Dec 30, 2020
The purpose of this change was originally to expand test coverage to
unblock ansible-collections#76, but an issue was detected with the encrypted parameter on
MySQL 8.0 in the process of writing the tests. Additionally,
user_password_update_test.yml had been disabled at some point, so I
opted to replace it with two new files that will focus on the password
and plugin auth paths.
@steveteahan
Copy link
Contributor Author

I believe this should be unblocked as soon as #79 is merged. That PR adds test coverage that was missing for the majority of password and plugin authentication paths.

Andersson007 pushed a commit that referenced this pull request Jan 14, 2021
* mysql_user: fixed encrypted option for MySQL 8.0 and test coverage

The purpose of this change was originally to expand test coverage to
unblock #76, but an issue was detected with the encrypted parameter on
MySQL 8.0 in the process of writing the tests. Additionally,
user_password_update_test.yml had been disabled at some point, so I
opted to replace it with two new files that will focus on the password
and plugin auth paths.

* Updated tests to cover a couple of missing branches

* Skip tests that rely on sha256_password if pymysql < 0.9

* Cover the case where pymysql isn't installed for plugin tests

* Added better plugin auth checking to tests and other minor changes

* Fixed version detection to explicitly handle MariaDB

* Removed unneeded import from previous change

* Remove whitespace that was introduced by change that was removed

* Added unit tests for missing coverage
@Andersson007
Copy link
Collaborator

@steveteahan could you please rebase the branch and fix the conflicts

@Andersson007
Copy link
Collaborator

@steveteahan if the tests get green, will try to review the PR tomorrow (if nothing super urgent on my day work), thanks for working on this

@Andersson007
Copy link
Collaborator

@bmalynovytch what do you think? It's a kinda preparation for #75

Copy link
Contributor

@bmalynovytch bmalynovytch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmalynovytch what do you think? It's a kinda preparation for #75

Looks good to me, although I didn't test it.

@Andersson007 Andersson007 merged commit b25fb59 into ansible-collections:main Jan 15, 2021
@Andersson007
Copy link
Collaborator

@steveteahan thanks for the refactoring!
@bmalynovytch thanks for reviewing! Now i can be sure we will fix hypothetical consequences together;)
Merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants