Fixed #32732 -- Removed usage of deprecated 'db' and 'passwd' connection options in MySQL backend.

The 'db' and 'passwd' connection options have been deprecated, use
'database' and 'password' instead (available since mysqlclient >= 1.3.8).

This also allows the 'database' option in DATABASES['OPTIONS'] on MySQL.
This commit is contained in:
Nick Pope 2021-05-10 21:32:10 +01:00 committed by Mariusz Felisiak
parent d06c5b3581
commit 1061f52436
4 changed files with 38 additions and 27 deletions

View File

@ -200,9 +200,9 @@ class DatabaseWrapper(BaseDatabaseWrapper):
if settings_dict['USER']: if settings_dict['USER']:
kwargs['user'] = settings_dict['USER'] kwargs['user'] = settings_dict['USER']
if settings_dict['NAME']: if settings_dict['NAME']:
kwargs['db'] = settings_dict['NAME'] kwargs['database'] = settings_dict['NAME']
if settings_dict['PASSWORD']: if settings_dict['PASSWORD']:
kwargs['passwd'] = settings_dict['PASSWORD'] kwargs['password'] = settings_dict['PASSWORD']
if settings_dict['HOST'].startswith('/'): if settings_dict['HOST'].startswith('/'):
kwargs['unix_socket'] = settings_dict['HOST'] kwargs['unix_socket'] = settings_dict['HOST']
elif settings_dict['HOST']: elif settings_dict['HOST']:

View File

@ -8,7 +8,10 @@ class DatabaseClient(BaseDatabaseClient):
def settings_to_cmd_args_env(cls, settings_dict, parameters): def settings_to_cmd_args_env(cls, settings_dict, parameters):
args = [cls.executable_name] args = [cls.executable_name]
env = None env = None
db = settings_dict['OPTIONS'].get('db', settings_dict['NAME']) database = settings_dict['OPTIONS'].get(
'database',
settings_dict['OPTIONS'].get('db', settings_dict['NAME']),
)
user = settings_dict['OPTIONS'].get('user', settings_dict['USER']) user = settings_dict['OPTIONS'].get('user', settings_dict['USER'])
password = settings_dict['OPTIONS'].get( password = settings_dict['OPTIONS'].get(
'password', 'password',
@ -51,7 +54,7 @@ class DatabaseClient(BaseDatabaseClient):
args += ["--ssl-key=%s" % client_key] args += ["--ssl-key=%s" % client_key]
if charset: if charset:
args += ['--default-character-set=%s' % charset] args += ['--default-character-set=%s' % charset]
if db: if database:
args += [db] args += [database]
args.extend(parameters) args.extend(parameters)
return args, env return args, env

View File

@ -9,4 +9,4 @@ Django 3.2.3 fixes several bugs in 3.2.2.
Bugfixes Bugfixes
======== ========
* ... * Prepared for ``mysqlclient`` > 2.0.3 support (:ticket:`32732`).

View File

@ -50,6 +50,9 @@ class MySqlDbshellCommandTestCase(SimpleTestCase):
'optiondbname', 'optiondbname',
] ]
expected_env = {'MYSQL_PWD': 'optionpassword'} expected_env = {'MYSQL_PWD': 'optionpassword'}
for keys in [('database', 'password'), ('db', 'passwd')]:
with self.subTest(keys=keys):
database, password = keys
self.assertEqual( self.assertEqual(
self.settings_to_cmd_args_env({ self.settings_to_cmd_args_env({
'NAME': 'settingdbname', 'NAME': 'settingdbname',
@ -58,9 +61,9 @@ class MySqlDbshellCommandTestCase(SimpleTestCase):
'HOST': 'settinghost', 'HOST': 'settinghost',
'PORT': settings_port, 'PORT': settings_port,
'OPTIONS': { 'OPTIONS': {
'db': 'optiondbname', database: 'optiondbname',
'user': 'optionuser', 'user': 'optionuser',
'passwd': 'optionpassword', password: 'optionpassword',
'host': 'optionhost', 'host': 'optionhost',
'port': options_port, 'port': options_port,
}, },
@ -68,23 +71,28 @@ class MySqlDbshellCommandTestCase(SimpleTestCase):
(expected_args, expected_env), (expected_args, expected_env),
) )
def test_options_password(self): def test_options_non_deprecated_keys_preferred(self):
expected_args = [ expected_args = [
'mysql', 'mysql',
'--user=someuser', '--user=someuser',
'--host=somehost', '--host=somehost',
'--port=444', '--port=444',
'somedbname', 'optiondbname',
] ]
expected_env = {'MYSQL_PWD': 'optionpassword'} expected_env = {'MYSQL_PWD': 'optionpassword'}
self.assertEqual( self.assertEqual(
self.settings_to_cmd_args_env({ self.settings_to_cmd_args_env({
'NAME': 'somedbname', 'NAME': 'settingdbname',
'USER': 'someuser', 'USER': 'someuser',
'PASSWORD': 'settingpassword', 'PASSWORD': 'settingpassword',
'HOST': 'somehost', 'HOST': 'somehost',
'PORT': 444, 'PORT': 444,
'OPTIONS': {'password': 'optionpassword'}, 'OPTIONS': {
'database': 'optiondbname',
'db': 'deprecatedoptiondbname',
'password': 'optionpassword',
'passwd': 'deprecatedoptionpassword',
},
}), }),
(expected_args, expected_env), (expected_args, expected_env),
) )