From f410e61a5d37780d54e76a32df314f3314ba8d00 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Thu, 17 Feb 2022 11:02:49 +0100 Subject: [PATCH 1/2] postgresql_query: fix the module cannot handle files with \n at the end --- changelogs/fragments/0-postgresql_query_fix.yml | 2 ++ plugins/modules/postgresql_query.py | 14 +++++++++++++- .../targets/postgresql_query/files/test0.sql | 2 ++ .../tasks/postgresql_query_initial.yml | 6 +++--- 4 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/0-postgresql_query_fix.yml diff --git a/changelogs/fragments/0-postgresql_query_fix.yml b/changelogs/fragments/0-postgresql_query_fix.yml new file mode 100644 index 00000000..3e452479 --- /dev/null +++ b/changelogs/fragments/0-postgresql_query_fix.yml @@ -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). diff --git a/plugins/modules/postgresql_query.py b/plugins/modules/postgresql_query.py index 39c975d2..9cdf4708 100644 --- a/plugins/modules/postgresql_query.py +++ b/plugins/modules/postgresql_query.py @@ -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( @@ -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: diff --git a/tests/integration/targets/postgresql_query/files/test0.sql b/tests/integration/targets/postgresql_query/files/test0.sql index fb9ce516..e8a5ca03 100644 --- a/tests/integration/targets/postgresql_query/files/test0.sql +++ b/tests/integration/targets/postgresql_query/files/test0.sql @@ -2,3 +2,5 @@ SELECT version(); SELECT story FROM test_table WHERE id = %s OR story = 'Данные'; + + diff --git a/tests/integration/targets/postgresql_query/tasks/postgresql_query_initial.yml b/tests/integration/targets/postgresql_query/tasks/postgresql_query_initial.yml index 646f4c2c..8aae782c 100644 --- a/tests/integration/targets/postgresql_query/tasks/postgresql_query_initial.yml +++ b/tests/integration/targets/postgresql_query/tasks/postgresql_query_initial.yml @@ -59,6 +59,7 @@ positional_args: - 1 encoding: UTF-8 + as_single_query: no register: result ignore_errors: true when: sql_file_created @@ -66,10 +67,9 @@ - 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 From 9abfa4ade50ed809790d16f3f15ef67af69271a3 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Fri, 18 Feb 2022 08:47:24 +0300 Subject: [PATCH 2/2] Update plugins/modules/postgresql_query.py Co-authored-by: Douglas J Hunley --- plugins/modules/postgresql_query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/postgresql_query.py b/plugins/modules/postgresql_query.py index 9cdf4708..fdac4085 100644 --- a/plugins/modules/postgresql_query.py +++ b/plugins/modules/postgresql_query.py @@ -365,7 +365,7 @@ def set_search_path(cursor, search_path): def insane_query(string): for c in string: - if c not in (' ', '\n', '', '\t', ','): + if c not in (' ', '\n', '', '\t'): return False return True