Skip to content
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
2 changes: 2 additions & 0 deletions changelogs/fragments/879-fix-conn-limit-zero.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- postgresql_db - Fix connection limit not being set when value is "0" (https://github.com/ansible-collections/community.postgresql/issues/879).
4 changes: 2 additions & 2 deletions plugins/modules/postgresql_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def db_update(cursor, db, owner, encoding, lc_collate, lc_ctype, icu_locale, loc
if owner and owner != db_info['owner']:
changed = set_owner(cursor, db, owner)

if conn_limit and conn_limit != str(db_info['conn_limit']):
if conn_limit != '' and conn_limit != str(db_info['conn_limit']):
changed = set_conn_limit(cursor, db, conn_limit)

if tablespace and tablespace != db_info['tablespace']:
Expand Down Expand Up @@ -513,7 +513,7 @@ def db_matches(cursor, db, owner, template, encoding, lc_collate, lc_ctype, icu_
return False
elif owner and owner != db_info['owner']:
return False
elif conn_limit and conn_limit != str(db_info['conn_limit']):
elif conn_limit != '' and conn_limit != str(db_info['conn_limit']):
return False
elif tablespace and tablespace != db_info['tablespace']:
return False
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/postgresql_db/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@

# Test the comment feature
- import_tasks: postgresql_db_comment.yml

# Test connection limit zero fix (issue #879)
- import_tasks: postgresql_db_conn_limit_zero.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
# Test for connection limit zero fix (issue #879)
- name: Test connection limit zero - Create DB with conn_limit 0
become_user: "{{ pg_user }}"
become: true
postgresql_db:
name: test_conn_limit_zero
state: present
conn_limit: "0"
login_user: "{{ pg_user }}"
register: result

- assert:
that:
- result is changed
- result.executed_commands == ['CREATE DATABASE "test_conn_limit_zero" CONNECTION LIMIT 0']

- name: Test connection limit zero - Verify conn_limit is actually set to 0
become_user: "{{ pg_user }}"
become: true
postgresql_query:
query: "SELECT datconnlimit AS conn_limit FROM pg_database WHERE datname = 'test_conn_limit_zero'"
register: result

- assert:
that:
- result.rowcount == 1
- result.query_result[0]['conn_limit'] == 0

- name: Test connection limit zero - Run the same task again (should not change)
become_user: "{{ pg_user }}"
become: true
postgresql_db:
name: test_conn_limit_zero
state: present
conn_limit: "0"
login_user: "{{ pg_user }}"
register: result

- assert:
that:
- result is not changed

- name: Test connection limit zero - Change conn_limit to 100
become_user: "{{ pg_user }}"
become: true
postgresql_db:
name: test_conn_limit_zero
state: present
conn_limit: "100"
login_user: "{{ pg_user }}"
register: result

- assert:
that:
- result is changed
- result.executed_commands == ['ALTER DATABASE "test_conn_limit_zero" CONNECTION LIMIT 100']

- name: Test connection limit zero - Verify conn_limit is changed to 100
become_user: "{{ pg_user }}"
become: true
postgresql_query:
query: "SELECT datconnlimit AS conn_limit FROM pg_database WHERE datname = 'test_conn_limit_zero'"
register: result

- assert:
that:
- result.rowcount == 1
- result.query_result[0]['conn_limit'] == 100

- name: Test connection limit zero - Change conn_limit back to 0
become_user: "{{ pg_user }}"
become: true
postgresql_db:
name: test_conn_limit_zero
state: present
conn_limit: "0"
login_user: "{{ pg_user }}"
register: result

- assert:
that:
- result is changed
- result.executed_commands == ['ALTER DATABASE "test_conn_limit_zero" CONNECTION LIMIT 0']

- name: Test connection limit zero - Verify conn_limit is back to 0
become_user: "{{ pg_user }}"
become: true
postgresql_query:
query: "SELECT datconnlimit AS conn_limit FROM pg_database WHERE datname = 'test_conn_limit_zero'"
register: result

- assert:
that:
- result.rowcount == 1
- result.query_result[0]['conn_limit'] == 0

- name: Test connection limit zero - Cleanup test DB
become_user: "{{ pg_user }}"
become: true
postgresql_db:
name: test_conn_limit_zero
state: absent
login_user: "{{ pg_user }}"