Fixed #33022 -- Fixed isolation of migrations.test_executor.ExecutorTests.test_custom_user().

This commit is contained in:
Mariusz Felisiak 2021-10-08 15:51:04 +02:00 committed by GitHub
parent bf4be3711a
commit 01bf679e59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 26 deletions

View File

@ -10,6 +10,7 @@ from django.db.migrations.state import ProjectState
from django.test import (
SimpleTestCase, modify_settings, override_settings, skipUnlessDBFeature,
)
from django.test.utils import isolate_lru_cache
from .test_base import MigrationTestBase
@ -344,32 +345,39 @@ class ExecutorTests(MigrationTestBase):
Regression test for #22325 - references to a custom user model defined in the
same app are not resolved correctly.
"""
executor = MigrationExecutor(connection)
self.assertTableNotExists("migrations_author")
self.assertTableNotExists("migrations_tribble")
# Migrate forwards
executor.migrate([("migrations", "0001_initial")])
self.assertTableExists("migrations_author")
self.assertTableExists("migrations_tribble")
# Make sure the soft-application detection works (#23093)
# Change table_names to not return auth_user during this as
# it wouldn't be there in a normal run, and ensure migrations.Author
# exists in the global app registry temporarily.
old_table_names = connection.introspection.table_names
connection.introspection.table_names = lambda c: [x for x in old_table_names(c) if x != "auth_user"]
migrations_apps = executor.loader.project_state(("migrations", "0001_initial")).apps
global_apps.get_app_config("migrations").models["author"] = migrations_apps.get_model("migrations", "author")
try:
migration = executor.loader.get_migration("auth", "0001_initial")
self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
finally:
connection.introspection.table_names = old_table_names
del global_apps.get_app_config("migrations").models["author"]
# And migrate back to clean up the database
executor.loader.build_graph()
executor.migrate([("migrations", None)])
self.assertTableNotExists("migrations_author")
self.assertTableNotExists("migrations_tribble")
with isolate_lru_cache(global_apps.get_swappable_settings_name):
executor = MigrationExecutor(connection)
self.assertTableNotExists('migrations_author')
self.assertTableNotExists('migrations_tribble')
# Migrate forwards
executor.migrate([('migrations', '0001_initial')])
self.assertTableExists('migrations_author')
self.assertTableExists('migrations_tribble')
# The soft-application detection works.
# Change table_names to not return auth_user during this as it
# wouldn't be there in a normal run, and ensure migrations.Author
# exists in the global app registry temporarily.
old_table_names = connection.introspection.table_names
connection.introspection.table_names = lambda c: [
x for x in old_table_names(c) if x != 'auth_user'
]
migrations_apps = executor.loader.project_state(
('migrations', '0001_initial'),
).apps
global_apps.get_app_config('migrations').models['author'] = (
migrations_apps.get_model('migrations', 'author')
)
try:
migration = executor.loader.get_migration('auth', '0001_initial')
self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
finally:
connection.introspection.table_names = old_table_names
del global_apps.get_app_config('migrations').models['author']
# Migrate back to clean up the database.
executor.loader.build_graph()
executor.migrate([('migrations', None)])
self.assertTableNotExists('migrations_author')
self.assertTableNotExists('migrations_tribble')
@override_settings(
MIGRATION_MODULES={