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']:
kwargs['user'] = settings_dict['USER']
if settings_dict['NAME']:
kwargs['db'] = settings_dict['NAME']
kwargs['database'] = settings_dict['NAME']
if settings_dict['PASSWORD']:
kwargs['passwd'] = settings_dict['PASSWORD']
kwargs['password'] = settings_dict['PASSWORD']
if settings_dict['HOST'].startswith('/'):
kwargs['unix_socket'] = 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):
args = [cls.executable_name]
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'])
password = settings_dict['OPTIONS'].get(
'password',
@ -51,7 +54,7 @@ class DatabaseClient(BaseDatabaseClient):
args += ["--ssl-key=%s" % client_key]
if charset:
args += ['--default-character-set=%s' % charset]
if db:
args += [db]
if database:
args += [database]
args.extend(parameters)
return args, env

View File

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

View File

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