mysql_query: fix failing when single-row query contains commas

This commit is contained in:
Andrew Klychkov 2020-11-01 20:55:42 +03:00
parent 0dfd63a4cb
commit a86282cd5e
3 changed files with 20 additions and 3 deletions

View file

@ -20,8 +20,7 @@ options:
query:
description:
- SQL query to run. Multiple queries can be passed using YAML list syntax.
type: list
elements: str
type: raw
required: yes
positional_args:
description:
@ -123,7 +122,7 @@ DDL_QUERY_KEYWORDS = ('CREATE', 'DROP', 'ALTER', 'RENAME', 'TRUNCATE')
def main():
argument_spec = mysql_common_argument_spec()
argument_spec.update(
query=dict(type='list', elements='str', required=True),
query=dict(type='raw', required=True),
login_db=dict(type='str'),
positional_args=dict(type='list'),
named_args=dict(type='dict'),
@ -183,6 +182,9 @@ def main():
query_result = []
executed_queries = []
rowcount = []
if isinstance(query, str):
query = [query]
for q in query:
try:
cursor.execute(q, arguments)

View file

@ -6,6 +6,7 @@ db_name: data
test_db: testdb
test_table1: test1
test_table2: test2
test_table3: test3
test_script_path: /tmp/test.sql
user_name_1: 'db_user1'

View file

@ -238,6 +238,20 @@
that:
- result.rowcount == [0]
- name: Create {{ test_table3 }}
mysql_query:
<<: *mysql_params
login_db: '{{ test_db }}'
query: 'CREATE TABLE {{ test_table3 }} (id int, story text)'
register: result
- name: Select from {{ test_table3 }}
mysql_query:
<<: *mysql_params
login_db: '{{ test_db }}'
query: 'SELECT id, story FROM {{ test_table3 }}'
register: result
- name: Drop db {{ test_db }}
mysql_query:
<<: *mysql_params