generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the "%" character in database name
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]>
- Loading branch information
Showing
5 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bugfixes: | ||
mysql_db - Fix mismatch when database name contins a ``%`` character (https://github.com/ansible-collections/community.mysql/pull/227). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tests/integration/targets/test_mysql_db/tasks/db_create_special.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# test code for mysql_db module with database name containing special chars | ||
# (c) 2021, Nicolas Payart <[email protected]> | ||
|
||
# This file is part of Ansible | ||
# | ||
# Ansible is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Ansible is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
# ============================================================ | ||
- name: "Set db_name to db%" | ||
set_fact: | ||
db_name: "db%" | ||
|
||
- name: remove database if it exists | ||
command: > | ||
"{{ mysql_command }} -sse 'drop database {{ db_name }}'" | ||
ignore_errors: True | ||
|
||
- name: make sure the test database is not there | ||
command: "{{ mysql_command }} {{ db_name }}" | ||
register: mysql_db_check | ||
failed_when: "'1049' not in mysql_db_check.stderr" | ||
|
||
- name: test state=present for a database name (expect changed=true) | ||
mysql_db: | ||
login_user: '{{ mysql_user }}' | ||
login_password: '{{ mysql_password }}' | ||
login_host: 127.0.0.1 | ||
login_port: '{{ mysql_primary_port }}' | ||
name: '{{ db_name }}' | ||
state: present | ||
register: result | ||
|
||
- name: assert output message that database exist | ||
assert: | ||
that: | ||
- result is changed | ||
- result.db == '{{ db_name }}' | ||
- result.executed_commands == ["CREATE DATABASE `{{ db_name }}`"] | ||
|
||
- name: run command to test state=present for a database name (expect db_name in stdout) | ||
command: "{{ mysql_command }} -e \"show databases like '{{ db_name | regex_replace(\"([%_\\\\])\", \"\\\\\\1\") }}'\"" | ||
register: result | ||
|
||
- name: assert database exist | ||
assert: | ||
that: | ||
- "'{{ db_name }}' in result.stdout" | ||
|
||
# ============================================================ | ||
- name: test state=absent for a database name (expect changed=true) | ||
mysql_db: | ||
login_user: '{{ mysql_user }}' | ||
login_password: '{{ mysql_password }}' | ||
login_host: 127.0.0.1 | ||
login_port: '{{ mysql_primary_port }}' | ||
name: '{{ db_name }}' | ||
state: absent | ||
register: result | ||
|
||
- name: assert output message that database does not exist | ||
assert: | ||
that: | ||
- result is changed | ||
- result.db == '{{ db_name }}' | ||
- result.executed_commands == ["DROP DATABASE `{{ db_name }}`"] | ||
|
||
- name: run command to test state=absent for a database name (expect db_name not in stdout) | ||
command: "{{ mysql_command }} -e \"show databases like '{{ db_name | regex_replace(\"([%_\\\\])\", \"\\\\\\1\") }}'\"" | ||
register: result | ||
|
||
- name: assert database does not exist | ||
assert: | ||
that: | ||
- "'{{ db_name }}' not in result.stdout" | ||
|
21 changes: 21 additions & 0 deletions
21
tests/integration/targets/test_mysql_db/tasks/db_name_special.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# test code for the mysql_db module with database name containing the '%' character | ||
|
||
# This file is part of Ansible | ||
# | ||
# Ansible is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Ansible is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
- name: "Main test for database name with the '%' char" | ||
include_tasks: main.yml | ||
vars: | ||
db_name: "db%" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,3 +324,6 @@ | |
when: ansible_python.version_info[0] >= 3 | ||
|
||
- include: issue-28.yml | ||
|
||
- include: db_create_special.yml | ||
|