From c578e5f243c6955dbdc4ba8a3318a39403e5b5e2 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Wed, 27 Aug 2025 21:13:36 +0200 Subject: [PATCH] postgresql_db: fix conn_limit 0 bug (#880) (cherry picked from commit 64aaa3dd20b9d8e38916e15c06fec6810ab75499) --- .../fragments/879-fix-conn-limit-zero.yml | 2 + plugins/modules/postgresql_db.py | 4 +- .../targets/postgresql_db/tasks/main.yml | 3 + .../tasks/postgresql_db_conn_limit_zero.yml | 104 ++++++++++++++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/879-fix-conn-limit-zero.yml create mode 100644 tests/integration/targets/postgresql_db/tasks/postgresql_db_conn_limit_zero.yml 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 671e561d..6b3eaa7b 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 2c5fc7c9..6db4c577 100644 --- a/tests/integration/targets/postgresql_db/tasks/main.yml +++ b/tests/integration/targets/postgresql_db/tasks/main.yml @@ -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 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 }}"