From 159b21f58b1047d0d58fcd808505d25059709c76 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sat, 23 Oct 2021 20:48:27 +0200
Subject: [PATCH] sql: use dbname parameter for connecting to database (#33589)

---
 tests/test_ctl.py        |  5 +++--
 tests/test_hobo.py       | 12 ++++++------
 wcs/ctl/check_hobos.py   |  2 ++
 wcs/ctl/delete_tenant.py |  2 ++
 wcs/sql.py               |  2 ++
 5 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/tests/test_ctl.py b/tests/test_ctl.py
index c2824f08a..566d2545f 100644
--- a/tests/test_ctl.py
+++ b/tests/test_ctl.py
@@ -291,7 +291,8 @@ def test_delete_tenant_with_sql(freezer):
     }
     delete_cmd.delete_tenant(pub, sub_options, [])
 
-    pgconn = psycopg2.connect(**pub.cfg['postgresql']['createdb-connection-params'])
+    connect_kwargs = {'dbname': 'postgres', 'user': pub.cfg['postgresql']['user']}
+    pgconn = psycopg2.connect(**connect_kwargs)
     cur = pgconn.cursor()
 
     cur.execute(
@@ -315,7 +316,7 @@ def test_delete_tenant_with_sql(freezer):
     }
     delete_cmd.delete_tenant(pub, sub_options, [])
 
-    pgconn = psycopg2.connect(**pub.cfg['postgresql']['createdb-connection-params'])
+    pgconn = psycopg2.connect(**connect_kwargs)
     pgconn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
     cur = pgconn.cursor()
 
diff --git a/tests/test_hobo.py b/tests/test_hobo.py
index adaf262dd..d7dc6bb54 100644
--- a/tests/test_hobo.py
+++ b/tests/test_hobo.py
@@ -446,23 +446,23 @@ def test_configure_postgresql():
     pub.initialize_sql = mock.Mock()
     with mock.patch('psycopg2.connect') as connect:
         hobo_cmd.configure_sql(service, pub)
-        assert connect.call_args_list[0][1] == {'user': 'test', 'database': 'postgres'}
-        assert connect.call_args_list[1][1] == {'user': 'fred', 'database': 'tests_wcs_wcs_example_net'}
+        assert connect.call_args_list[0][1] == {'user': 'test', 'dbname': 'postgres'}
+        assert connect.call_args_list[1][1] == {'user': 'fred', 'dbname': 'tests_wcs_wcs_example_net'}
     assert pub.initialize_sql.call_count == 1
 
     pub.reload_cfg()
     assert 'createdb-connection-params' in pub.cfg['postgresql']
     with mock.patch('psycopg2.connect') as connect:
         sql.get_connection(new=True)
-        assert connect.call_args_list[0][1] == {'user': 'fred', 'database': 'tests_wcs_wcs_example_net'}
+        assert connect.call_args_list[0][1] == {'user': 'fred', 'dbname': 'tests_wcs_wcs_example_net'}
 
     pub.cfg['postgresql']['database-template-name'] = 'very_long_' * 10 + '%s'
     with mock.patch('psycopg2.connect') as connect:
         hobo_cmd.configure_sql(service, pub)
-        assert connect.call_args_list[0][1] == {'user': 'test', 'database': 'postgres'}
+        assert connect.call_args_list[0][1] == {'user': 'test', 'dbname': 'postgres'}
         assert connect.call_args_list[1][1] == {
             'user': 'fred',
-            'database': 'very_long_very_long_very_long_c426_ng_very_long_wcs_example_net',
+            'dbname': 'very_long_very_long_very_long_c426_ng_very_long_wcs_example_net',
         }
-        assert len(connect.call_args_list[1][1]['database']) == 63
+        assert len(connect.call_args_list[1][1]['dbname']) == 63
     assert pub.initialize_sql.call_count == 2
diff --git a/wcs/ctl/check_hobos.py b/wcs/ctl/check_hobos.py
index 0ba36b697..37c0de34b 100644
--- a/wcs/ctl/check_hobos.py
+++ b/wcs/ctl/check_hobos.py
@@ -545,6 +545,8 @@ class CmdCheckHobos(Command):
                 if v and isinstance(v, str):
                     createdb_cfg[k] = v
 
+        if 'database' in createdb_cfg:
+            createdb_cfg['dbname'] = createdb_cfg.pop('database')
         try:
             pgconn = psycopg2.connect(**createdb_cfg)
         except psycopg2.Error as e:
diff --git a/wcs/ctl/delete_tenant.py b/wcs/ctl/delete_tenant.py
index 26ab05e5f..707904162 100644
--- a/wcs/ctl/delete_tenant.py
+++ b/wcs/ctl/delete_tenant.py
@@ -65,6 +65,8 @@ class CmdDeleteTenant(Command):
             if not createdb_cfg:
                 createdb_cfg = postgresql_cfg
                 createdb = False
+            if 'database' in createdb_cfg:
+                createdb_cfg['dbname'] = createdb_cfg.pop('database')
             try:
                 pgconn = psycopg2.connect(**createdb_cfg)
             except psycopg2.Error as e:
diff --git a/wcs/sql.py b/wcs/sql.py
index 904410611..b14cc8047 100644
--- a/wcs/sql.py
+++ b/wcs/sql.py
@@ -375,6 +375,8 @@ def get_connection(new=False, isolate=False):
             value = get_cfg('postgresql', {}).get(param)
             if value:
                 postgresql_cfg[param] = value
+        if 'database' in postgresql_cfg:
+            postgresql_cfg['dbname'] = postgresql_cfg.pop('database')
         try:
             pgconn = psycopg2.connect(**postgresql_cfg)
         except psycopg2.Error:
-- 
2.33.0

