mirror of https://github.com/django/django.git
Fixed #29052 -- Made test database creation preserve alias order and prefer the "default" database.
This fixes flushing test databases when two aliases point to the same database. Use a list() to store the test database aliases so the order remains stable by following the order of the connections. Also, always use the "default" database alias as the first alias to accommodate `migrate`. Previously `migrate` could be executed on a secondary alias which caused truncating the "default" database.
This commit is contained in:
parent
3119a6deca
commit
06e5f7ae16
1
AUTHORS
1
AUTHORS
|
@ -361,6 +361,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Hang Park <hangpark@kaist.ac.kr>
|
Hang Park <hangpark@kaist.ac.kr>
|
||||||
Hannes Ljungberg <hannes.ljungberg@gmail.com>
|
Hannes Ljungberg <hannes.ljungberg@gmail.com>
|
||||||
Hannes Struß <x@hannesstruss.de>
|
Hannes Struß <x@hannesstruss.de>
|
||||||
|
Harm Geerts <hgeerts@gmail.com>
|
||||||
Hasan Ramezani <hasan.r67@gmail.com>
|
Hasan Ramezani <hasan.r67@gmail.com>
|
||||||
Hawkeye
|
Hawkeye
|
||||||
Helen Sherwood-Taylor <helen@rrdlabs.co.uk>
|
Helen Sherwood-Taylor <helen@rrdlabs.co.uk>
|
||||||
|
|
|
@ -280,9 +280,14 @@ def get_unique_databases_and_mirrors(aliases=None):
|
||||||
# we only need to create the test database once.
|
# we only need to create the test database once.
|
||||||
item = test_databases.setdefault(
|
item = test_databases.setdefault(
|
||||||
connection.creation.test_db_signature(),
|
connection.creation.test_db_signature(),
|
||||||
(connection.settings_dict['NAME'], set())
|
(connection.settings_dict['NAME'], []),
|
||||||
)
|
)
|
||||||
item[1].add(alias)
|
# The default database must be the first because data migrations
|
||||||
|
# use the default alias by default.
|
||||||
|
if alias == DEFAULT_DB_ALIAS:
|
||||||
|
item[1].insert(0, alias)
|
||||||
|
else:
|
||||||
|
item[1].append(alias)
|
||||||
|
|
||||||
if 'DEPENDENCIES' in test_settings:
|
if 'DEPENDENCIES' in test_settings:
|
||||||
dependencies[alias] = test_settings['DEPENDENCIES']
|
dependencies[alias] = test_settings['DEPENDENCIES']
|
||||||
|
|
|
@ -14,7 +14,9 @@ from django.core.management.base import SystemCheckError
|
||||||
from django.test import TransactionTestCase, skipUnlessDBFeature
|
from django.test import TransactionTestCase, skipUnlessDBFeature
|
||||||
from django.test.runner import DiscoverRunner
|
from django.test.runner import DiscoverRunner
|
||||||
from django.test.testcases import connections_support_transactions
|
from django.test.testcases import connections_support_transactions
|
||||||
from django.test.utils import captured_stderr, dependency_ordered
|
from django.test.utils import (
|
||||||
|
captured_stderr, dependency_ordered, get_unique_databases_and_mirrors,
|
||||||
|
)
|
||||||
|
|
||||||
from .models import B, Person, Through
|
from .models import B, Person, Through
|
||||||
|
|
||||||
|
@ -336,6 +338,33 @@ class SetupDatabasesTests(unittest.TestCase):
|
||||||
self.runner_instance.teardown_databases(old_config)
|
self.runner_instance.teardown_databases(old_config)
|
||||||
mocked_db_creation.return_value.destroy_test_db.assert_called_once_with('dbname', 0, False)
|
mocked_db_creation.return_value.destroy_test_db.assert_called_once_with('dbname', 0, False)
|
||||||
|
|
||||||
|
def test_setup_test_database_aliases(self):
|
||||||
|
"""
|
||||||
|
The default database must be the first because data migrations
|
||||||
|
use the default alias by default.
|
||||||
|
"""
|
||||||
|
tested_connections = db.ConnectionHandler({
|
||||||
|
'other': {
|
||||||
|
'ENGINE': 'django.db.backends.dummy',
|
||||||
|
'NAME': 'dbname',
|
||||||
|
},
|
||||||
|
'default': {
|
||||||
|
'ENGINE': 'django.db.backends.dummy',
|
||||||
|
'NAME': 'dbname',
|
||||||
|
}
|
||||||
|
})
|
||||||
|
with mock.patch('django.test.utils.connections', new=tested_connections):
|
||||||
|
test_databases, _ = get_unique_databases_and_mirrors()
|
||||||
|
self.assertEqual(
|
||||||
|
test_databases,
|
||||||
|
{
|
||||||
|
('', '', 'django.db.backends.dummy', 'test_dbname'): (
|
||||||
|
'dbname',
|
||||||
|
['default', 'other'],
|
||||||
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
def test_destroy_test_db_restores_db_name(self):
|
def test_destroy_test_db_restores_db_name(self):
|
||||||
tested_connections = db.ConnectionHandler({
|
tested_connections = db.ConnectionHandler({
|
||||||
'default': {
|
'default': {
|
||||||
|
|
Loading…
Reference in New Issue