Skip to content

Commit

Permalink
Fix queries handling for comment feature (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 authored Dec 19, 2023
1 parent f573c02 commit d587143
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 29 deletions.
5 changes: 3 additions & 2 deletions plugins/module_utils/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def get_comment(cursor, obj_type, obj_name):
return cursor.fetchone()['comment']


def set_comment(cursor, comment, obj_type, obj_name, executed_queries=None):
def set_comment(cursor, comment, obj_type, obj_name, check_mode=True, executed_queries=None):
"""Get DB object's comment.
Args:
Expand All @@ -564,7 +564,8 @@ def set_comment(cursor, comment, obj_type, obj_name, executed_queries=None):
"""
query = 'COMMENT ON %s "%s" IS ' % (obj_type.upper(), obj_name)

cursor.execute(query + '%(comment)s', {'comment': comment})
if not check_mode:
cursor.execute(query + '%(comment)s', {'comment': comment})

if executed_queries is not None:
executed_queries.append(cursor.mogrify(query + '%(comment)s', {'comment': comment}))
Expand Down
9 changes: 5 additions & 4 deletions plugins/modules/postgresql_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def db_delete(cursor, db, force=False):
return False


def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn_limit, tablespace, comment):
def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn_limit, tablespace, comment, check_mode):
params = dict(enc=encoding, collate=lc_collate, ctype=lc_ctype, conn_limit=conn_limit, tablespace=tablespace)
if not db_exists(cursor, db):
query_fragments = ['CREATE DATABASE "%s"' % db]
Expand All @@ -398,7 +398,7 @@ def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn_
executed_commands.append(cursor.mogrify(query, params))
cursor.execute(query, params)
if comment:
set_comment(cursor, comment, 'database', db, executed_commands)
set_comment(cursor, comment, 'database', db, check_mode, executed_commands)
return True
else:
db_info = get_db_info(cursor, db)
Expand Down Expand Up @@ -434,7 +434,7 @@ def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn_
changed = set_tablespace(cursor, db, tablespace)

if comment is not None and comment != db_info['comment']:
changed = set_comment(cursor, comment, 'database', db, executed_commands)
changed = set_comment(cursor, comment, 'database', db, check_mode, executed_commands)

return changed

Expand Down Expand Up @@ -765,7 +765,8 @@ def main():

elif state == "present":
try:
changed = db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn_limit, tablespace, comment)
changed = db_create(cursor, db, owner, template, encoding, lc_collate,
lc_ctype, conn_limit, tablespace, comment, module.check_mode)
except SQLParseError as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())

Expand Down
6 changes: 1 addition & 5 deletions plugins/modules/postgresql_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,8 @@ def main():
current_comment = get_comment(cursor, 'extension', ext)
# For the resetting comment feature (comment: '') to work correctly
current_comment = current_comment if current_comment is not None else ''

if comment != current_comment:
if module.check_mode:
changed = True
else:
changed = set_comment(cursor, comment, 'extension', ext, executed_queries)
changed = set_comment(cursor, comment, 'extension', ext, module.check_mode, executed_queries)

elif state == "absent":
if curr_version:
Expand Down
11 changes: 4 additions & 7 deletions plugins/modules/postgresql_publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ def create(self, tables, params, owner, comment, check_mode=True):
self.__pub_set_owner(owner, check_mode=check_mode)

if comment is not None:
if not check_mode:
set_comment(self.cursor, comment, 'publication', self.name, self.executed_queries)
set_comment(self.cursor, comment, 'publication',
self.name, check_mode, self.executed_queries)

return changed

Expand Down Expand Up @@ -424,11 +424,8 @@ def update(self, tables, params, owner, comment, check_mode=True):
changed = self.__pub_set_owner(owner, check_mode=check_mode)

if comment is not None and comment != self.attrs['comment']:
if not check_mode:
changed = set_comment(self.cursor, comment, 'publication',
self.name, self.executed_queries)
else:
changed = True
changed = set_comment(self.cursor, comment, 'publication',
self.name, check_mode, self.executed_queries)

return changed

Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/postgresql_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def schema_create(cursor, schema, owner, comment):
cursor.execute(query)
executed_queries.append(query)
if comment is not None:
set_comment(cursor, comment, 'schema', schema, executed_queries)
set_comment(cursor, comment, 'schema', schema, False, executed_queries)
return True
else:
schema_info = get_schema_info(cursor, schema)
Expand All @@ -210,7 +210,7 @@ def schema_create(cursor, schema, owner, comment):
if comment is not None:
current_comment = schema_info['comment'] if schema_info['comment'] is not None else ''
if comment != current_comment:
changed = set_comment(cursor, comment, 'schema', schema, executed_queries) or changed
changed = set_comment(cursor, comment, 'schema', schema, False, executed_queries) or changed

return changed

Expand Down
3 changes: 1 addition & 2 deletions plugins/modules/postgresql_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,7 @@ def set_comment(self, comment, check_mode=True):
Returns:
True if success, False otherwise.
"""
if not check_mode:
set_comment(self.cursor, comment, 'subscription', self.name, self.executed_queries)
set_comment(self.cursor, comment, 'subscription', self.name, check_mode, self.executed_queries)

return True

Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/postgresql_tablespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def set_owner(self, new_owner):
query = 'ALTER TABLESPACE "%s" OWNER TO "%s"' % (self.name, new_owner)
return exec_sql(self, query, return_bool=True)

def set_comment(self, comment):
def set_comment(self, comment, check_mode):
"""Set tablespace comment.
Return True if success, otherwise, return False.
Expand All @@ -341,7 +341,7 @@ def set_comment(self, comment):
return False

return set_comment(self.cursor, comment, 'tablespace', self.name,
self.executed_queries)
check_mode, self.executed_queries)

def rename(self, newname):
"""Rename tablespace.
Expand Down Expand Up @@ -515,7 +515,7 @@ def main():
changed = tblspace.set_settings(settings) or changed

if comment is not None:
changed = tblspace.set_comment(comment) or changed
changed = tblspace.set_comment(comment, module.check_mode) or changed

# Update tablespace information in the class
tblspace.get_info()
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/postgresql_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,13 +908,13 @@ def get_valid_flags_by_version(srv_version):
]


def add_comment(cursor, user, comment):
def add_comment(cursor, user, comment, check_mode):
"""Add comment on user."""
current_comment = get_comment(cursor, 'role', user)
# For the resetting comment feature (comment: '') to work correctly
current_comment = current_comment if current_comment is not None else ''
if comment != current_comment:
set_comment(cursor, comment, 'role', user, executed_queries)
set_comment(cursor, comment, 'role', user, check_mode, executed_queries)
return True
else:
return False
Expand Down Expand Up @@ -1015,7 +1015,7 @@ def main():

if comment is not None:
try:
changed = add_comment(cursor, user, comment) or changed
changed = add_comment(cursor, user, comment, module.check_mode) or changed
except Exception as e:
module.fail_json(msg='Unable to add comment on role: %s' % to_native(e),
exception=traceback.format_exc())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
- assert:
that:
- result is changed
- result.queries == []
- result.queries == ["COMMENT ON EXTENSION \"postgis\" IS ''"]

- name: Check the comment didn't change
become_user: '{{ pg_user }}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
- assert:
that:
- result is changed
- result.queries == ["COMMENT ON PUBLICATION \"{{ test_pub }}\" IS 'Made by me'"]

- name: Check the comment didn't change
<<: *task_parameters
Expand Down Expand Up @@ -192,6 +193,7 @@
- assert:
that:
- result is not changed
- result.queries == []

# Test
- name: postgresql_publication - drop publication, check_mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
- assert:
that:
- result is changed
- result.queries == ["COMMENT ON SUBSCRIPTION \"test\" IS ''"]

- name: Check the comment is the same
<<: *task_parameters
Expand Down Expand Up @@ -166,6 +167,7 @@
- assert:
that:
- result is not changed
- result.queries == []

- name: Reset the comment again in check mode
<<: *task_parameters
Expand All @@ -179,6 +181,7 @@
- assert:
that:
- result is not changed
- result.queries == []

###################
# Test mode: absent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@
- assert:
that:
- result is changed
- result.queries == ["COMMENT ON TABLESPACE \"acme\" IS 'Test comment 2'"]

- name: Check the comment didn't change
become_user: '{{ pg_user }}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
- assert:
that:
- result is changed
- result.queries == ["COMMENT ON ROLE \"{{ test_user }}\" IS '{{ test_comment1 }}'"]

- name: check the comment didn't change
<<: *task_parameters
Expand Down Expand Up @@ -138,6 +139,7 @@
- assert:
that:
- result is changed
- result.queries == ["COMMENT ON ROLE \"{{ test_user }}\" IS '{{ test_comment2 }}'"]

- name: check the comment didn't change
<<: *task_parameters
Expand Down

0 comments on commit d587143

Please sign in to comment.