From 80d38de52bb2721a7b44fce4057bcff571afc23a Mon Sep 17 00:00:00 2001
From: Jacob Walls <jacobtylerwalls@gmail.com>
Date: Sun, 25 Sep 2022 19:53:55 -0400
Subject: [PATCH] Fixed #34051 -- Made makemigrations --check exit before
 making migrations.

---
 django/core/management/commands/makemigrations.py | 9 ++++++---
 docs/ref/django-admin.txt                         | 5 +++++
 docs/releases/4.2.txt                             | 3 +++
 tests/migrations/test_commands.py                 | 3 ++-
 4 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py
index 2e97d9064b..284a95409f 100644
--- a/django/core/management/commands/makemigrations.py
+++ b/django/core/management/commands/makemigrations.py
@@ -70,7 +70,10 @@ class Command(BaseCommand):
             "--check",
             action="store_true",
             dest="check_changes",
-            help="Exit with a non-zero status if model changes are missing migrations.",
+            help=(
+                "Exit with a non-zero status if model changes are missing migrations "
+                "and don't actually write them."
+            ),
         )
         parser.add_argument(
             "--scriptable",
@@ -248,12 +251,12 @@ class Command(BaseCommand):
                 else:
                     self.log("No changes detected")
         else:
+            if check_changes:
+                sys.exit(1)
             if self.update:
                 self.write_to_last_migration_files(changes)
             else:
                 self.write_migration_files(changes)
-            if check_changes:
-                sys.exit(1)
 
     def write_to_last_migration_files(self, changes):
         loader = MigrationLoader(connections[DEFAULT_DB_ALIAS])
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index af27e0afb5..aa4c4e6ff8 100644
--- a/docs/ref/django-admin.txt
+++ b/docs/ref/django-admin.txt
@@ -825,6 +825,11 @@ Generate migration files without Django version and timestamp header.
 Makes ``makemigrations`` exit with a non-zero status when model changes without
 migrations are detected.
 
+.. versionchanged:: 4.2
+
+    In older versions, the missing migrations were also created when using the
+    ``--check`` option.
+
 .. django-admin-option:: --scriptable
 
 .. versionadded:: 4.1
diff --git a/docs/releases/4.2.txt b/docs/releases/4.2.txt
index 2520d14361..6cfd0865f6 100644
--- a/docs/releases/4.2.txt
+++ b/docs/releases/4.2.txt
@@ -322,6 +322,9 @@ Miscellaneous
 * The ``autofocus`` HTML attribute in the admin search box is removed as it can
   be confusing for screen readers.
 
+* The :option:`makemigrations --check` option no longer creates missing
+  migration files.
+
 .. _deprecated-features-4.2:
 
 Features deprecated in 4.2
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index 9900c7a35d..0117d1e4aa 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -2391,9 +2391,10 @@ class MakeMigrationsTests(MigrationTestBase):
         makemigrations --check should exit with a non-zero status when
         there are changes to an app requiring migrations.
         """
-        with self.temporary_migration_module():
+        with self.temporary_migration_module() as tmpdir:
             with self.assertRaises(SystemExit):
                 call_command("makemigrations", "--check", "migrations", verbosity=0)
+            self.assertFalse(os.path.exists(tmpdir))
 
         with self.temporary_migration_module(
             module="migrations.test_migrations_no_changes"