Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules: fix reset comment #656

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions plugins/modules/postgresql_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn_
else:
changed = False

if db_info['comment'] is None:
# For the resetting comment feature (comment: '') to work correctly
db_info['comment'] = ''

if owner and owner != db_info['owner']:
changed = set_owner(cursor, db, owner)

Expand All @@ -440,6 +444,11 @@ def db_matches(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn
return False
else:
db_info = get_db_info(cursor, db)

if db_info['comment'] is None:
# For the resetting comment feature (comment: '') to work correctly
db_info['comment'] = ''

if (encoding and get_encoding_id(cursor, encoding) != db_info['encoding_id']):
return False
elif lc_collate and lc_collate != db_info['lc_collate']:
Expand Down
15 changes: 10 additions & 5 deletions plugins/modules/postgresql_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,16 @@ def main():
else:
module.fail_json(msg="Extension %s is not available" % ext)

if comment is not None and comment != get_comment(cursor, 'extension', ext):
if module.check_mode:
changed = True
else:
changed = set_comment(cursor, comment, 'extension', ext, executed_queries)
if comment is not None:
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)

elif state == "absent":
if curr_version:
Expand Down
13 changes: 9 additions & 4 deletions plugins/modules/postgresql_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ def schema_create(cursor, schema, owner, comment):
if owner and owner != schema_info['owner']:
changed = set_owner(cursor, schema, owner)

if comment is not None and comment != schema_info['comment']:
changed = set_comment(cursor, comment, 'schema', schema, executed_queries) or changed
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

return changed

Expand All @@ -220,8 +222,11 @@ def schema_matches(cursor, schema, owner, comment):
schema_info = get_schema_info(cursor, schema)
if owner and owner != schema_info['owner']:
return False
if comment is not None and comment != schema_info['comment']:
return False
if comment is not None:
# For the resetting comment feature (comment: '') to work correctly
current_comment = schema_info['comment'] if schema_info['comment'] is not None else ''
if comment != current_comment:
return False

return True

Expand Down
3 changes: 1 addition & 2 deletions plugins/modules/postgresql_tablespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ def get_info(self):
# Location exists:
self.location = res[0]["loc_string"]

if res[0]["comment"]:
self.comment = res[0]["comment"]
self.comment = res[0]["comment"] if res[0]["comment"] is not None else ''

def create(self, location):
"""Create tablespace.
Expand Down
5 changes: 4 additions & 1 deletion plugins/modules/postgresql_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,10 @@ def get_valid_flags_by_version(srv_version):

def add_comment(cursor, user, comment):
"""Add comment on user."""
if comment != get_comment(cursor, 'role', 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)
return True
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@
- result.query_result[0]['comment'] == None


- name: Reset the comment again
<<: *task_parameters
postgresql_db:
state: present
name: comment_db
login_user: "{{ pg_user }}"
comment: ''

- name: Assert the result
assert:
that:
- result is not changed
- result.executed_commands == []


- name: Clean up
<<: *task_parameters
postgresql_db:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@
- result.query_result[0]['comment'] == None


- name: Reset the comment again
become_user: '{{ pg_user }}'
become: true
postgresql_ext:
login_db: postgres
login_port: 5432
name: postgis
comment: ''
ignore_errors: true
register: result

- assert:
that:
- result is not changed
- result.queries == []


- name: postgresql_ext - drop extension postgis
become_user: '{{ pg_user }}'
become: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,23 @@
- result.query_result[0]['comment'] == None


- name: Reset the comment again
become_user: "{{ pg_user }}"
become: true
postgresql_schema:
login_user: "{{ pg_user }}"
database: "{{ db_name }}"
name: comment_schema
comment: ''
register: result

- name: Check return values
assert:
that:
- result is not changed
- result.queries == []


- name: Drop schema
become_user: "{{ pg_user }}"
become: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,23 @@
- result.query_result[0]['comment'] == None


- name: Reset the comment again
become_user: '{{ pg_user }}'
become: true
postgresql_tablespace:
db: postgres
login_user: '{{ pg_user }}'
name: acme
location: /ssd
comment: ''
register: result

- assert:
that:
- result is not changed
- result.queries == []


# Clean up
- name: Drop tablespace
become_user: '{{ pg_user }}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@
that:
- result.rowcount == 1
- result.query_result[0].comment == None

- name: Reset the comment again
<<: *task_parameters
postgresql_user:
<<: *pg_parameters
name: '{{ test_user }}'
comment: ''

- assert:
that:
- result is not changed
- result.queries == []
# End comment argument testing

- name: Try to create role again in check_mode
Expand Down