Skip to content

Commit

Permalink
module_utils: get rid of deprecated alias database for conn param dbn…
Browse files Browse the repository at this point in the history
…ame when psycopg2 is 2.7+ (ansible-collections#196)

* module_utils: get rid of deprecated alias database for conn param dbname when psycopg2 is 2.7+

* Fix units, add fragment
  • Loading branch information
Andersson007 authored Feb 28, 2022
1 parent dd17dcd commit 476da36
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1-postgresq_connection_fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- module core functions - get rid of the deprecated psycopg2 connection alias ``database`` in favor of ``dbname`` when psycopg2 is 2.7+ (https://github.com/ansible-collections/community.postgresql/pull/196).
30 changes: 21 additions & 9 deletions plugins/module_utils/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,28 @@ def get_conn_params(module, params_dict, warn_db_default=True):
}

# Might be different in the modules:
if params_dict.get('db'):
params_map['db'] = 'database'
elif params_dict.get('database'):
params_map['database'] = 'database'
elif params_dict.get('login_db'):
params_map['login_db'] = 'database'
if LooseVersion(psycopg2.__version__) >= LooseVersion('2.7.0'):
if params_dict.get('db'):
params_map['db'] = 'dbname'
elif params_dict.get('database'):
params_map['database'] = 'dbname'
elif params_dict.get('login_db'):
params_map['login_db'] = 'dbname'
else:
if warn_db_default:
module.warn('Database name has not been passed, '
'used default database to connect to.')
else:
if warn_db_default:
module.warn('Database name has not been passed, '
'used default database to connect to.')
if params_dict.get('db'):
params_map['db'] = 'database'
elif params_dict.get('database'):
params_map['database'] = 'database'
elif params_dict.get('login_db'):
params_map['login_db'] = 'database'
else:
if warn_db_default:
module.warn('Database name has not been passed, '
'used default database to connect to.')

kw = dict((params_map[k], v) for (k, v) in iteritems(params_dict)
if k in params_map and v != '' and v is not None)
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/plugins/module_utils/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,14 @@ class TestGetConnParams():

"""Namespace for testing get_conn_params() function."""

def test_get_conn_params_def(self, m_ansible_module):
def test_get_conn_params_def(self, m_ansible_module, m_psycopg2, monkeypatch):
"""Test get_conn_params(), warn_db_default kwarg is default."""
monkeypatch.setattr(pg, 'psycopg2', m_psycopg2)
assert pg.get_conn_params(m_ansible_module, INPUT_DICT) == EXPECTED_DICT
assert m_ansible_module.warn_msg == 'Database name has not been passed, used default database to connect to.'

def test_get_conn_params_warn_db_def_false(self, m_ansible_module):
def test_get_conn_params_warn_db_def_false(self, m_ansible_module, m_psycopg2, monkeypatch):
"""Test get_conn_params(), warn_db_default kwarg is False."""
monkeypatch.setattr(pg, 'psycopg2', m_psycopg2)
assert pg.get_conn_params(m_ansible_module, INPUT_DICT, warn_db_default=False) == EXPECTED_DICT
assert m_ansible_module.warn_msg == ''

0 comments on commit 476da36

Please sign in to comment.