diff --git a/changelogs/fragments/996-postgresql_privs_fix_function_handling.yml b/changelogs/fragments/996-postgresql_privs_fix_function_handling.yml new file mode 100644 index 0000000000..975cf3cd59 --- /dev/null +++ b/changelogs/fragments/996-postgresql_privs_fix_function_handling.yml @@ -0,0 +1,2 @@ +bugfixes: +- postgresql_privs - fix the module mistakes a procedure for a function (https://github.com/ansible-collections/community.general/issues/994). diff --git a/plugins/modules/database/postgresql/postgresql_privs.py b/plugins/modules/database/postgresql/postgresql_privs.py index 20cade9217..1bdccf1ed3 100644 --- a/plugins/modules/database/postgresql/postgresql_privs.py +++ b/plugins/modules/database/postgresql/postgresql_privs.py @@ -514,6 +514,7 @@ class Connection(object): self.connection = psycopg2.connect(**kw) self.cursor = self.connection.cursor() + self.pg_version = self.connection.server_version def commit(self): self.connection.commit() @@ -561,10 +562,15 @@ class Connection(object): def get_all_functions_in_schema(self, schema): if not self.schema_exists(schema): raise Error('Schema "%s" does not exist.' % schema) - query = """SELECT p.proname, oidvectortypes(p.proargtypes) - FROM pg_catalog.pg_proc p - JOIN pg_namespace n ON n.oid = p.pronamespace - WHERE nspname = %s""" + + query = ("SELECT p.proname, oidvectortypes(p.proargtypes) " + "FROM pg_catalog.pg_proc p " + "JOIN pg_namespace n ON n.oid = p.pronamespace " + "WHERE nspname = %s") + + if self.pg_version >= 110000: + query += " and p.prokind = 'f'" + self.cursor.execute(query, (schema,)) return ["%s(%s)" % (t[0], t[1]) for t in self.cursor.fetchall()] @@ -1120,7 +1126,7 @@ def main(): except psycopg2.Error as e: conn.rollback() - module.fail_json(msg=to_native(e.message)) + module.fail_json(msg=to_native(e)) if module.check_mode or not changed: conn.rollback() diff --git a/tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml b/tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml index 50bb6026e3..4b12bf2e83 100644 --- a/tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml +++ b/tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml @@ -653,6 +653,35 @@ login_user: "{{ db_user3 }}" login_password: password +# Issue https://github.com/ansible-collections/community.general/issues/994 +- name: Create a procedure for tests + postgresql_query: + query: "CREATE PROCEDURE mock_procedure() LANGUAGE SQL AS $$ SELECT 1; $$;" + db: "{{ db_name }}" + login_user: "{{ db_user3 }}" + login_password: password + when: postgres_version_resp.stdout is version('11', '>=') + +# Issue https://github.com/ansible-collections/community.general/issues/994 +- name: Try to run module against a procedure, not function + postgresql_privs: + type: function + state: present + privs: ALL + roles: "{{ db_user2 }}" + objs: ALL_IN_SCHEMA + schema: public + db: "{{ db_name }}" + login_user: "{{ db_user3 }}" + login_password: password + register: result + when: postgres_version_resp.stdout is version('11', '>=') + +- assert: + that: + - result is not changed + when: postgres_version_resp.stdout is version('11', '>=') + ################################################# # Test ALL_IN_SCHEMA for 'partioned tables type # #################################################