From 01bf679e59850bb7b3e6395f1683bd1463ed9969 Mon Sep 17 00:00:00 2001
From: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Date: Fri, 8 Oct 2021 15:51:04 +0200
Subject: [PATCH] Fixed #33022 -- Fixed isolation of
 migrations.test_executor.ExecutorTests.test_custom_user().

---
 tests/migrations/test_executor.py | 60 +++++++++++++++++--------------
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git a/tests/migrations/test_executor.py b/tests/migrations/test_executor.py
index f8c321b0f3..ca2c03cac5 100644
--- a/tests/migrations/test_executor.py
+++ b/tests/migrations/test_executor.py
@@ -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={