Skip to content

Commit 9f8a17f

Browse files
authored
postgresql_info: fix failure when db/login_db are not specified (ansible-collections#798)
1 parent 48010d1 commit 9f8a17f

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- postgresql_info - fix failure when a default database is used (neither ``db`` nor ``login_db`` are specified) (https://github.com/ansible-collections/community.postgresql/issues/794).

plugins/modules/postgresql_info.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ def __init__(self, module, db_conn_obj):
230230
self.module = module
231231
self.db_obj = db_conn_obj
232232
self.cursor = db_conn_obj.connect()
233-
self.default_db = module.params['db'] or \
234-
module.params['login_db'] or \
235-
module.params['database']
233+
self.default_db = self.__get_current_db()
236234
self.pg_info = {
237235
"version": {},
238236
"in_recovery": None,
@@ -722,6 +720,13 @@ def __exec_sql(self, query):
722720
self.cursor.close()
723721
return False
724722

723+
def __get_current_db(self):
724+
"""Get current DB"""
725+
# The context is to get the user's default database.
726+
# Should be executed right after logging in
727+
# https://github.com/ansible-collections/community.postgresql/issues/794
728+
return self.__exec_sql('SELECT current_database() AS db')[0]['db']
729+
725730
# ===========================================
726731
# Module execution.
727732
#

tests/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
- name: postgresql_info - check filter param passed by list
149149
<<: *task_parameters
150150
postgresql_info:
151-
<<: *pg_parameters
151+
login_user: '{{ pg_user }}'
152152
login_port: '{{ replica_port }}'
153153
filter:
154154
- ver*

0 commit comments

Comments
 (0)