Skip to content

Commit

Permalink
postgresql_privs: fix the module mistakes a procedure for a function (#…
Browse files Browse the repository at this point in the history
…996) (#1006)

* postgresql_privs: fix the module mistakes a procedure for a function

* add changelog fragment

* fix

(cherry picked from commit 2200517)

Co-authored-by: Andrew Klychkov <[email protected]>
  • Loading branch information
patchback[bot] and Andersson007 authored Sep 29, 2020
1 parent c00147e commit db24f98
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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).
16 changes: 11 additions & 5 deletions plugins/modules/database/postgresql/postgresql_privs.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ def __init__(self, params, module):

self.connection = psycopg2.connect(**kw)
self.cursor = self.connection.cursor()
self.pg_version = self.connection.server_version

def commit(self):
self.connection.commit()
Expand Down Expand Up @@ -561,10 +562,15 @@ def get_all_sequences_in_schema(self, schema):
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()]

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
#################################################
Expand Down

0 comments on commit db24f98

Please sign in to comment.