Skip to content

Commit bd95b43

Browse files
authored
postgresql_query: a list of queries can be passed (#394)
* postgresql_query: a list of queries can be passed * forgot to gitadd the fragment * fix typo
1 parent 162fd14 commit bd95b43

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- postgresql_query - a list of queries can be passed as the ``query`` argument's value, the results will be stored in the ``query_all_results`` return value (is not deprecated anymore, as well as ``query_list``) (https://github.com/ansible-collections/community.postgresql/issues/312).

plugins/modules/postgresql_query.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
options:
2424
query:
2525
description:
26-
- SQL query to run. Variables can be escaped with psycopg2 syntax
26+
- SQL query string or list of queries to run. Variables can be escaped with psycopg2 syntax
2727
U(http://initd.org/psycopg/docs/usage.html).
28-
type: str
28+
type: raw
2929
positional_args:
3030
description:
3131
- List of values to be passed as positional arguments to the query.
@@ -132,6 +132,14 @@
132132
db: acme
133133
query: SELECT version()
134134
135+
# The result of each query will be stored in query_all_results return value
136+
- name: Run several queries against acme db
137+
community.postgresql.postgresql_query:
138+
db: acme
139+
query:
140+
- SELECT version()
141+
- SELECT id FROM accounts
142+
135143
- name: Select query to db acme with positional arguments and non-default credentials
136144
community.postgresql.postgresql_query:
137145
db: acme
@@ -268,8 +276,6 @@
268276
sample: [{"Column": "Value1"},{"Column": "Value2"}]
269277
query_list:
270278
description:
271-
- This return value has been B(deprecated) and will be removed in
272-
community.postgresql 3.0.0.
273279
- List of executed queries.
274280
Useful when reading several queries from a file.
275281
returned: always
@@ -278,10 +284,8 @@
278284
sample: ['SELECT * FROM foo', 'SELECT * FROM bar']
279285
query_all_results:
280286
description:
281-
- This return value has been B(deprecated) and will be removed in
282-
community.postgresql 3.0.0.
283287
- List containing results of all queries executed (one sublist for every query).
284-
Useful when reading several queries from a file.
288+
Useful when running a list of queries.
285289
returned: always
286290
type: list
287291
elements: list
@@ -340,7 +344,7 @@ def insane_query(string):
340344
def main():
341345
argument_spec = postgres_common_argument_spec()
342346
argument_spec.update(
343-
query=dict(type='str'),
347+
query=dict(type='raw'),
344348
db=dict(type='str', aliases=['login_db']),
345349
positional_args=dict(type='list', elements='raw'),
346350
named_args=dict(type='dict'),
@@ -370,6 +374,9 @@ def main():
370374
search_path = module.params["search_path"]
371375
as_single_query = module.params["as_single_query"]
372376

377+
if query and not isinstance(query, (str, list)):
378+
module.fail_json(msg="query argument must be of type string or list")
379+
373380
if not trust_input:
374381
# Check input for potentially dangerous elements:
375382
check_input(module, session_role)
@@ -411,7 +418,10 @@ def main():
411418
except Exception as e:
412419
module.fail_json(msg="Cannot read file '%s' : %s" % (path_to_script, to_native(e)))
413420
else:
414-
query_list.append(query)
421+
if isinstance(query, str):
422+
query_list.append(query)
423+
else: # if it's a list
424+
query_list = query
415425

416426
# Ensure psycopg2 libraries are available before connecting to DB:
417427
ensure_required_libs(module)

tests/integration/targets/postgresql_query/tasks/postgresql_query_initial.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,22 @@
583583
- result.rowcount == 1
584584
- result.query_result[0]["make_interval"] == "0:00:03"
585585
when: postgres_version_resp.stdout is version('10', '>=')
586+
587+
##############################################################################
588+
# Issue https://github.com/ansible-collections/community.postgresql/issues/312
589+
- name: Run several queries
590+
become_user: '{{ pg_user }}'
591+
become: true
592+
postgresql_query:
593+
<<: *pg_parameters
594+
query:
595+
- SELECT 1
596+
- SELECT 1
597+
- SELECT 1
598+
register: result
599+
600+
- assert:
601+
that:
602+
- result.rowcount == 3
603+
- result.query_result == [{"?column?": 1}]
604+
- 'result.query_all_results == [[{"?column?": 1}], [{"?column?": 1}], [{"?column?": 1}]]'

0 commit comments

Comments
 (0)