Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/0-postgresql_query_fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- postgresql_query - cannot handle .sql file with \n at end of file (https://github.com/ansible-collections/community.postgresql/issues/180).
14 changes: 13 additions & 1 deletion plugins/modules/postgresql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ def set_search_path(cursor, search_path):
cursor.execute('SET search_path TO %s' % search_path)


def insane_query(string):
for c in string:
if c not in (' ', '\n', '', '\t'):
return False

return True


def main():
argument_spec = postgres_common_argument_spec()
argument_spec.update(
Expand Down Expand Up @@ -424,7 +432,11 @@ def main():
module.deprecate(msg=depr_msg, version="3.0.0", collection_name="community.postgresql")

if ';' in query:
query_list = [q for q in query.split(';') if q != '\n']
for q in query.split(';'):
if insane_query(q):
continue
else:
query_list.append(q)
else:
query_list.append(query)
else:
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/postgresql_query/files/test0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ SELECT version();

SELECT story FROM test_table
WHERE id = %s OR story = 'Данные';


Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@
positional_args:
- 1
encoding: UTF-8
as_single_query: no
register: result
ignore_errors: true
when: sql_file_created

- assert:
that:
- result is not changed
- result.query == "SELECT version();\n\nSELECT story FROM test_table\n WHERE id = 1 OR story = 'Данные';\n"
- result.query == "\n\nSELECT story FROM test_table\n WHERE id = 1 OR story = 'Данные'"
- result.query_result[0].story == 'first'
- result.query_all_results[0][0].story == 'first'
- result.rowcount == 1
- result.rowcount == 2
- result.statusmessage == 'SELECT 1' or result.statusmessage == 'SELECT'
when: sql_file_created

Expand Down