Fixed #30636 -- Fixed options ordering when cloning test database on MySQL.

--defaults-file must be given before other options.
This commit is contained in:
Yann Sionneau 2019-07-14 12:32:20 +02:00 committed by Mariusz Felisiak
parent c1b94e32fb
commit e47b8293a7
2 changed files with 34 additions and 2 deletions

View File

@ -56,8 +56,7 @@ class DatabaseCreation(BaseDatabaseCreation):
def _clone_db(self, source_database_name, target_database_name):
dump_args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)[1:]
dump_args[-1] = source_database_name
dump_cmd = ['mysqldump', '--routines', '--events'] + dump_args
dump_cmd = ['mysqldump', *dump_args[:-1], '--routines', '--events', source_database_name]
load_cmd = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
load_cmd[-1] = target_database_name

View File

@ -1,3 +1,4 @@
import subprocess
import unittest
from io import StringIO
from unittest import mock
@ -50,3 +51,35 @@ class DatabaseCreationTests(SimpleTestCase):
with mock.patch.object(DatabaseCreation, '_clone_db') as _clone_db:
creation._clone_test_db('suffix', verbosity=0, keepdb=True)
_clone_db.assert_not_called()
def test_clone_test_db_options_ordering(self):
creation = DatabaseCreation(connection)
try:
saved_settings = connection.settings_dict
connection.settings_dict = {
'NAME': 'source_db',
'USER': '',
'PASSWORD': '',
'PORT': '',
'HOST': '',
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': 'my.cnf',
},
}
with mock.patch.object(subprocess, 'Popen') as mocked_popen:
creation._clone_db('source_db', 'target_db')
mocked_popen.assert_has_calls([
mock.call(
[
'mysqldump',
'--defaults-file=my.cnf',
'--routines',
'--events',
'source_db',
],
stdout=subprocess.PIPE,
),
])
finally:
connection.settings_dict = saved_settings