Skip to content

Commit 2200517

Browse files
authored
postgresql_privs: fix the module mistakes a procedure for a function (#996)
* postgresql_privs: fix the module mistakes a procedure for a function * add changelog fragment * fix
1 parent 104f6a3 commit 2200517

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- postgresql_privs - fix the module mistakes a procedure for a function (https://github.com/ansible-collections/community.general/issues/994).

plugins/modules/database/postgresql/postgresql_privs.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ def __init__(self, params, module):
514514

515515
self.connection = psycopg2.connect(**kw)
516516
self.cursor = self.connection.cursor()
517+
self.pg_version = self.connection.server_version
517518

518519
def commit(self):
519520
self.connection.commit()
@@ -561,10 +562,15 @@ def get_all_sequences_in_schema(self, schema):
561562
def get_all_functions_in_schema(self, schema):
562563
if not self.schema_exists(schema):
563564
raise Error('Schema "%s" does not exist.' % schema)
564-
query = """SELECT p.proname, oidvectortypes(p.proargtypes)
565-
FROM pg_catalog.pg_proc p
566-
JOIN pg_namespace n ON n.oid = p.pronamespace
567-
WHERE nspname = %s"""
565+
566+
query = ("SELECT p.proname, oidvectortypes(p.proargtypes) "
567+
"FROM pg_catalog.pg_proc p "
568+
"JOIN pg_namespace n ON n.oid = p.pronamespace "
569+
"WHERE nspname = %s")
570+
571+
if self.pg_version >= 110000:
572+
query += " and p.prokind = 'f'"
573+
568574
self.cursor.execute(query, (schema,))
569575
return ["%s(%s)" % (t[0], t[1]) for t in self.cursor.fetchall()]
570576

@@ -1120,7 +1126,7 @@ def main():
11201126

11211127
except psycopg2.Error as e:
11221128
conn.rollback()
1123-
module.fail_json(msg=to_native(e.message))
1129+
module.fail_json(msg=to_native(e))
11241130

11251131
if module.check_mode or not changed:
11261132
conn.rollback()

tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,35 @@
653653
login_user: "{{ db_user3 }}"
654654
login_password: password
655655

656+
# Issue https://github.com/ansible-collections/community.general/issues/994
657+
- name: Create a procedure for tests
658+
postgresql_query:
659+
query: "CREATE PROCEDURE mock_procedure() LANGUAGE SQL AS $$ SELECT 1; $$;"
660+
db: "{{ db_name }}"
661+
login_user: "{{ db_user3 }}"
662+
login_password: password
663+
when: postgres_version_resp.stdout is version('11', '>=')
664+
665+
# Issue https://github.com/ansible-collections/community.general/issues/994
666+
- name: Try to run module against a procedure, not function
667+
postgresql_privs:
668+
type: function
669+
state: present
670+
privs: ALL
671+
roles: "{{ db_user2 }}"
672+
objs: ALL_IN_SCHEMA
673+
schema: public
674+
db: "{{ db_name }}"
675+
login_user: "{{ db_user3 }}"
676+
login_password: password
677+
register: result
678+
when: postgres_version_resp.stdout is version('11', '>=')
679+
680+
- assert:
681+
that:
682+
- result is not changed
683+
when: postgres_version_resp.stdout is version('11', '>=')
684+
656685
#################################################
657686
# Test ALL_IN_SCHEMA for 'partioned tables type #
658687
#################################################

0 commit comments

Comments
 (0)