Skip to content

Commit 037c472

Browse files
postgresql_query: fix the module cannot handle files with \n at the end (#192)
Co-authored-by: Douglas J Hunley <[email protected]>
1 parent 7235c03 commit 037c472

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- postgresql_query - cannot handle .sql file with \n at end of file (https://github.com/ansible-collections/community.postgresql/issues/180).

plugins/modules/postgresql_query.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ def set_search_path(cursor, search_path):
363363
cursor.execute('SET search_path TO %s' % search_path)
364364

365365

366+
def insane_query(string):
367+
for c in string:
368+
if c not in (' ', '\n', '', '\t'):
369+
return False
370+
371+
return True
372+
373+
366374
def main():
367375
argument_spec = postgres_common_argument_spec()
368376
argument_spec.update(
@@ -424,7 +432,11 @@ def main():
424432
module.deprecate(msg=depr_msg, version="3.0.0", collection_name="community.postgresql")
425433

426434
if ';' in query:
427-
query_list = [q for q in query.split(';') if q != '\n']
435+
for q in query.split(';'):
436+
if insane_query(q):
437+
continue
438+
else:
439+
query_list.append(q)
428440
else:
429441
query_list.append(query)
430442
else:

tests/integration/targets/postgresql_query/files/test0.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ SELECT version();
22

33
SELECT story FROM test_table
44
WHERE id = %s OR story = 'Данные';
5+
6+

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@
5959
positional_args:
6060
- 1
6161
encoding: UTF-8
62+
as_single_query: no
6263
register: result
6364
ignore_errors: true
6465
when: sql_file_created
6566

6667
- assert:
6768
that:
6869
- result is not changed
69-
- result.query == "SELECT version();\n\nSELECT story FROM test_table\n WHERE id = 1 OR story = 'Данные';\n"
70+
- result.query == "\n\nSELECT story FROM test_table\n WHERE id = 1 OR story = 'Данные'"
7071
- result.query_result[0].story == 'first'
71-
- result.query_all_results[0][0].story == 'first'
72-
- result.rowcount == 1
72+
- result.rowcount == 2
7373
- result.statusmessage == 'SELECT 1' or result.statusmessage == 'SELECT'
7474
when: sql_file_created
7575

0 commit comments

Comments
 (0)