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

Allow the "%" character in database name #227

Merged
merged 1 commit into from
Oct 21, 2021

Conversation

koleo
Copy link
Contributor

@koleo koleo commented Oct 11, 2021

The naming rules for MySQL/MariaDB identifiers, when quoted, allow the % character.

However, currently, the use of the % character in database names results in mismatch or missing databases.

SUMMARY
  • Rewrite query to identify the databases in the catalog using information_schema instead of SHOW DATABASES LIKE
  • Escape the % character in CREATE DATABASE query (because of the Python format string)
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

mysql_db

ADDITIONAL INFORMATION
  • Test playbook
# test_db_present.yaml
---
- hosts: localhost
  connection: local
  tasks:
    - mysql_db:
        name: "db%"
        state: present
        login_unix_socket: /var/run/mysqld/mysqld.sock
  • Actual behavior (before this PR)
$ ansible-playbook test_db_present.yaml

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [localhost]

TASK [mysql_db] ********************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ValueError: unsupported format character '`' (0x60) at index 20
fatal: [localhost]: FAILED! => {"changed": false, "msg": "error creating database: unsupported format character '`' (0x60) at index 20"}

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
  • New behavior (after this PR)
$ ansible-playbook test_db_present.yaml

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [localhost]

TASK [mysql_db] ********************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Check database list

$ mysql -e "SHOW DATABASES"
+--------------------+
| Database           |
+--------------------+
| db%                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

@Andersson007
Copy link
Collaborator

Andersson007 commented Oct 12, 2021

@koleo hi, thanks for the PR and welcome to the project!
Could you please

  1. add a changelog fragment https://docs.ansible.com/ansible/latest/community/development_process.html#creating-a-changelog-fragment
  2. add integration tests to tests/integration/targets/test_mysql_db/*, i.e.:
  • add a task that add a db with a name containing % and then
  • a check with mysql_query module that the DB is actually present
  1. if any questions appear, feel free to ask. BTW there's info how to run integration tests locally if needed https://github.com/ansible/community-docs/blob/main/create_pr_quick_start_guide.rst#test-your-changes

@codecov
Copy link

codecov bot commented Oct 12, 2021

Codecov Report

Merging #227 (1c5a23b) into main (f47d463) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #227   +/-   ##
=======================================
  Coverage   77.60%   77.60%           
=======================================
  Files          24       24           
  Lines        2170     2170           
  Branches      510      510           
=======================================
  Hits         1684     1684           
  Misses        319      319           
  Partials      167      167           
Impacted Files Coverage Δ
plugins/modules/mysql_db.py 74.56% <100.00%> (ø)

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 f47d463...1c5a23b. Read the comment docs.

@Andersson007
Copy link
Collaborator

Andersson007 commented Oct 12, 2021

@koleo could you also please rebase the PR? I've just updated the test matrix, would be nice to test it in the updated CI

@koleo
Copy link
Contributor Author

koleo commented Oct 12, 2021

@Andersson007 thanks for the tips. I will complete the missing steps and rebase soon.

@Andersson007
Copy link
Collaborator

@koleo cool, thanks:) When the PR is ready for review again, please let me know

@koleo koleo force-pushed the fix-mysql-db-create branch 2 times, most recently from ae7de23 to 1472889 Compare October 13, 2021 18:10
@koleo
Copy link
Contributor Author

koleo commented Oct 13, 2021

Hi @Andersson007 , so I pushed an update with Fragment, tests and rebase.

The test file is inspired by the existing create/drop database tests from main.yml with a specific db_name containing the % character. This is a duplication of code but refactoring the current test files would be a hard work and should be part of a new PR IMHO.

@Andersson007
Copy link
Collaborator

Andersson007 commented Oct 14, 2021

@koleo hi, i think

  1. we should do similar changes (i.e. replacement) for the other states (absent, dump, import) and test them. I tried to drop a database without an additional % and it failed. All the states should support this kind of name.
  2. we could use the mysql_query module instead of command (though I'm not strictly against checking with command, just a fyi)

@Andersson007
Copy link
Collaborator

Andersson007 commented Oct 14, 2021

i think it would be much faster if you run the tests locally before pushing using this guide

There are also sanity issues (you can see them by clicking "Details" in the test report box near failed tasks).

ERROR: Found 2 yamllint issue(s) which need to be resolved:
ERROR: tests/integration/targets/test_mysql_db/tasks/db_create_special.yml:86:1: empty-lines: too many blank lines (1 > 0)
ERROR: tests/integration/targets/test_mysql_db/tasks/main.yml:329:1: empty-lines: too many blank lines (1 > 0)

@koleo koleo force-pushed the fix-mysql-db-create branch from 1472889 to 3f8c0d2 Compare October 14, 2021 21:26
The naming rules for MySQL/MariaDB identifiers, when quoted, allow the
`%` character.

However, currently, the use of the `%` character in database names
results in mismatch or missing databases.

- Rewrite query to identify the databases in the catalog using
  `information_schema` instead of `SHOW DATABASES LIKE`
- Escape the `%` character in `CREATE DATABASE` query.

Signed-off-by: Nicolas Payart <[email protected]>
@koleo koleo force-pushed the fix-mysql-db-create branch from 3f8c0d2 to 1c5a23b Compare October 14, 2021 21:30
@koleo
Copy link
Contributor Author

koleo commented Oct 16, 2021

@Andersson007 it's ready for a review.
My last changes in tests folder:

  • Move the main.yml content to state_present_absent.yml leaving only the mysql_command and the include parts.
  • include state_present_absent.yml (with default db_name)
  • include state_present_absent.yml for db_name with % char
  • include state_dump_import.yml for db_name with % char
  • Fix a few things in original main code (now in state_present_absent.yml) like quoting dbname in SHOW CREATE DATABASE commands or escaping special chars in LIKE queries (=> escape chars %, _, \)

@Andersson007
Copy link
Collaborator

@koleo thanks, i'll try to review it tomorrow

@bmalynovytch @Jorge-Rodriguez could you please take a look at this PR too

@Andersson007
Copy link
Collaborator

@koleo thanks for working on this! Let's wait a couple of days for @bmalynovytch and @Jorge-Rodriguez 's feedback.
If no objections, I'll merge the PR max on Monday. Thank you

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.

Changes related to the described PR look good 👍
I didn't deep dive in the tests though.

@Andersson007 Andersson007 merged commit 6b12435 into ansible-collections:main Oct 21, 2021
@Andersson007
Copy link
Collaborator

@koleo thanks for the contribution! @bmalynovytch thanks for reviewing!

Andersson007 pushed a commit to Andersson007/community.mysql that referenced this pull request Nov 29, 2021
The naming rules for MySQL/MariaDB identifiers, when quoted, allow the
`%` character.

However, currently, the use of the `%` character in database names
results in mismatch or missing databases.

- Rewrite query to identify the databases in the catalog using
  `information_schema` instead of `SHOW DATABASES LIKE`
- Escape the `%` character in `CREATE DATABASE` query.

Signed-off-by: Nicolas Payart <[email protected]>
(cherry picked from commit 6b12435)
Andersson007 added a commit that referenced this pull request Nov 29, 2021
* Allow the "%" character in database name (#227)

The naming rules for MySQL/MariaDB identifiers, when quoted, allow the
`%` character.

However, currently, the use of the `%` character in database names
results in mismatch or missing databases.

- Rewrite query to identify the databases in the catalog using
  `information_schema` instead of `SHOW DATABASES LIKE`
- Escape the `%` character in `CREATE DATABASE` query.

Signed-off-by: Nicolas Payart <[email protected]>
(cherry picked from commit 6b12435)

* mysql_db: Fix assert in tests suite (#239)

Signed-off-by: Nicolas Payart <[email protected]>
(cherry picked from commit 5522e45)

* mysql_db: Improve tests (#240)

- Define variables "db_names" and "db_formats" in defaults
- Use of the "vars" option in includes instead of default parameters
  that might be overridden by a previous task
- Use of the "loop" option in includes instead of duplicating include
  tasks
- Use a nested loop on db_names and db_formats in state_dump_import test

Signed-off-by: Nicolas Payart <[email protected]>
(cherry picked from commit e4de13a)

* MAINTAINERS file: Add new maintainer (#248)

(cherry picked from commit d411a8e)

Co-authored-by: Nicolas PAYART <[email protected]>
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