diff --git a/changelogs/fragments/879-fix-conn-limit-zero.yml b/changelogs/fragments/879-fix-conn-limit-zero.yml new file mode 100644 index 00000000..59332031 --- /dev/null +++ b/changelogs/fragments/879-fix-conn-limit-zero.yml @@ -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). diff --git a/plugins/modules/postgresql_db.py b/plugins/modules/postgresql_db.py index 913bd0ae..5976b80f 100644 --- a/plugins/modules/postgresql_db.py +++ b/plugins/modules/postgresql_db.py @@ -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']: @@ -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 diff --git a/tests/integration/targets/postgresql_db/tasks/main.yml b/tests/integration/targets/postgresql_db/tasks/main.yml index 6d0389a6..3511d198 100644 --- a/tests/integration/targets/postgresql_db/tasks/main.yml +++ b/tests/integration/targets/postgresql_db/tasks/main.yml @@ -50,3 +50,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 diff --git a/tests/integration/targets/postgresql_db/tasks/postgresql_db_conn_limit_zero.yml b/tests/integration/targets/postgresql_db/tasks/postgresql_db_conn_limit_zero.yml new file mode 100644 index 00000000..23b42c1e --- /dev/null +++ b/tests/integration/targets/postgresql_db/tasks/postgresql_db_conn_limit_zero.yml @@ -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 }}"