From e47b8293a793653bd7df795c1d9f0381f3f22cf7 Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Sun, 14 Jul 2019 12:32:20 +0200 Subject: [PATCH] Fixed #30636 -- Fixed options ordering when cloning test database on MySQL. --defaults-file must be given before other options. --- django/db/backends/mysql/creation.py | 3 +-- tests/backends/mysql/test_creation.py | 33 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py index 0e5bfca7b9e..e51de0faa1a 100644 --- a/django/db/backends/mysql/creation.py +++ b/django/db/backends/mysql/creation.py @@ -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 diff --git a/tests/backends/mysql/test_creation.py b/tests/backends/mysql/test_creation.py index 2f6351d9bc6..01e59d87586 100644 --- a/tests/backends/mysql/test_creation.py +++ b/tests/backends/mysql/test_creation.py @@ -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