1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

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:
Harm Geerts
2021-02-15 14:42:00 +01:00
committed by Mariusz Felisiak
parent 3119a6deca
commit 06e5f7ae16
3 changed files with 38 additions and 3 deletions

View File

@@ -280,9 +280,14 @@ def get_unique_databases_and_mirrors(aliases=None):
# we only need to create the test database once.
item = test_databases.setdefault(
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:
dependencies[alias] = test_settings['DEPENDENCIES']