Skip to content

Commit ab0cfce

Browse files
postgresql_db: fix conn_limit 0 bug (#880) (#881)
(cherry picked from commit 64aaa3d) Co-authored-by: Andrew Klychkov <[email protected]>
1 parent 45116af commit ab0cfce

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- postgresql_db - Fix connection limit not being set when value is "0" (https://github.com/ansible-collections/community.postgresql/issues/879).

plugins/modules/postgresql_db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def db_update(cursor, db, owner, encoding, lc_collate, lc_ctype, icu_locale, loc
479479
if owner and owner != db_info['owner']:
480480
changed = set_owner(cursor, db, owner)
481481

482-
if conn_limit and conn_limit != str(db_info['conn_limit']):
482+
if conn_limit != '' and conn_limit != str(db_info['conn_limit']):
483483
changed = set_conn_limit(cursor, db, conn_limit)
484484

485485
if tablespace and tablespace != db_info['tablespace']:
@@ -513,7 +513,7 @@ def db_matches(cursor, db, owner, template, encoding, lc_collate, lc_ctype, icu_
513513
return False
514514
elif owner and owner != db_info['owner']:
515515
return False
516-
elif conn_limit and conn_limit != str(db_info['conn_limit']):
516+
elif conn_limit != '' and conn_limit != str(db_info['conn_limit']):
517517
return False
518518
elif tablespace and tablespace != db_info['tablespace']:
519519
return False

tests/integration/targets/postgresql_db/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@
4848

4949
# Test the comment feature
5050
- import_tasks: postgresql_db_comment.yml
51+
52+
# Test connection limit zero fix (issue #879)
53+
- import_tasks: postgresql_db_conn_limit_zero.yml
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
# Test for connection limit zero fix (issue #879)
3+
- name: Test connection limit zero - Create DB with conn_limit 0
4+
become_user: "{{ pg_user }}"
5+
become: true
6+
postgresql_db:
7+
name: test_conn_limit_zero
8+
state: present
9+
conn_limit: "0"
10+
login_user: "{{ pg_user }}"
11+
register: result
12+
13+
- assert:
14+
that:
15+
- result is changed
16+
- result.executed_commands == ['CREATE DATABASE "test_conn_limit_zero" CONNECTION LIMIT 0']
17+
18+
- name: Test connection limit zero - Verify conn_limit is actually set to 0
19+
become_user: "{{ pg_user }}"
20+
become: true
21+
postgresql_query:
22+
query: "SELECT datconnlimit AS conn_limit FROM pg_database WHERE datname = 'test_conn_limit_zero'"
23+
register: result
24+
25+
- assert:
26+
that:
27+
- result.rowcount == 1
28+
- result.query_result[0]['conn_limit'] == 0
29+
30+
- name: Test connection limit zero - Run the same task again (should not change)
31+
become_user: "{{ pg_user }}"
32+
become: true
33+
postgresql_db:
34+
name: test_conn_limit_zero
35+
state: present
36+
conn_limit: "0"
37+
login_user: "{{ pg_user }}"
38+
register: result
39+
40+
- assert:
41+
that:
42+
- result is not changed
43+
44+
- name: Test connection limit zero - Change conn_limit to 100
45+
become_user: "{{ pg_user }}"
46+
become: true
47+
postgresql_db:
48+
name: test_conn_limit_zero
49+
state: present
50+
conn_limit: "100"
51+
login_user: "{{ pg_user }}"
52+
register: result
53+
54+
- assert:
55+
that:
56+
- result is changed
57+
- result.executed_commands == ['ALTER DATABASE "test_conn_limit_zero" CONNECTION LIMIT 100']
58+
59+
- name: Test connection limit zero - Verify conn_limit is changed to 100
60+
become_user: "{{ pg_user }}"
61+
become: true
62+
postgresql_query:
63+
query: "SELECT datconnlimit AS conn_limit FROM pg_database WHERE datname = 'test_conn_limit_zero'"
64+
register: result
65+
66+
- assert:
67+
that:
68+
- result.rowcount == 1
69+
- result.query_result[0]['conn_limit'] == 100
70+
71+
- name: Test connection limit zero - Change conn_limit back to 0
72+
become_user: "{{ pg_user }}"
73+
become: true
74+
postgresql_db:
75+
name: test_conn_limit_zero
76+
state: present
77+
conn_limit: "0"
78+
login_user: "{{ pg_user }}"
79+
register: result
80+
81+
- assert:
82+
that:
83+
- result is changed
84+
- result.executed_commands == ['ALTER DATABASE "test_conn_limit_zero" CONNECTION LIMIT 0']
85+
86+
- name: Test connection limit zero - Verify conn_limit is back to 0
87+
become_user: "{{ pg_user }}"
88+
become: true
89+
postgresql_query:
90+
query: "SELECT datconnlimit AS conn_limit FROM pg_database WHERE datname = 'test_conn_limit_zero'"
91+
register: result
92+
93+
- assert:
94+
that:
95+
- result.rowcount == 1
96+
- result.query_result[0]['conn_limit'] == 0
97+
98+
- name: Test connection limit zero - Cleanup test DB
99+
become_user: "{{ pg_user }}"
100+
become: true
101+
postgresql_db:
102+
name: test_conn_limit_zero
103+
state: absent
104+
login_user: "{{ pg_user }}"

0 commit comments

Comments
 (0)