Browse Source

[5.0.x] Fixed #34457 -- Restored output for makemigrations --check.

Co-authored-by: David Sanders <shang.xiao.sanders@gmail.com>
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>

Backport of f7389c4b07ceeb036436e065898e411b247bca78 from main
Adam Johnson 1 year ago
parent
commit
8eba6efbf0

+ 5 - 3
django/core/management/commands/makemigrations.py

@@ -72,7 +72,7 @@ class Command(BaseCommand):
             dest="check_changes",
             help=(
                 "Exit with a non-zero status if model changes are missing migrations "
-                "and don't actually write them."
+                "and don't actually write them. Implies --dry-run."
             ),
         )
         parser.add_argument(
@@ -114,6 +114,8 @@ class Command(BaseCommand):
             raise CommandError("The migration name must be a valid Python identifier.")
         self.include_header = options["include_header"]
         check_changes = options["check_changes"]
+        if check_changes:
+            self.dry_run = True
         self.scriptable = options["scriptable"]
         self.update = options["update"]
         # If logs and prompts are diverted to stderr, remove the ERROR style.
@@ -251,12 +253,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])

+ 1 - 1
docs/ref/django-admin.txt

@@ -742,7 +742,7 @@ Generate migration files without Django version and timestamp header.
 .. django-admin-option:: --check
 
 Makes ``makemigrations`` exit with a non-zero status when model changes without
-migrations are detected.
+migrations are detected. Implies ``--dry-run``.
 
 .. versionchanged:: 4.2
 

+ 2 - 1
docs/releases/4.2.8.txt

@@ -9,4 +9,5 @@ Django 4.2.8 fixes several bugs in 4.2.7.
 Bugfixes
 ========
 
-* ...
+* Fixed a regression in Django 4.2 that caused :option:`makemigrations --check`
+  to stop displaying pending migrations (:ticket:`34457`).

+ 20 - 5
tests/migrations/test_commands.py

@@ -2392,20 +2392,35 @@ class MakeMigrationsTests(MigrationTestBase):
                 "makemigrations", "migrations", "--name", "invalid name", "--empty"
             )
 
-    def test_makemigrations_check(self):
+    def test_makemigrations_check_with_changes(self):
         """
         makemigrations --check should exit with a non-zero status when
         there are changes to an app requiring migrations.
         """
+        out = io.StringIO()
         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.assertRaises(SystemExit) as cm:
+                call_command(
+                    "makemigrations",
+                    "--check",
+                    "migrations",
+                    stdout=out,
+                )
+            self.assertEqual(os.listdir(tmpdir), ["__init__.py"])
+        self.assertEqual(cm.exception.code, 1)
+        self.assertIn("Migrations for 'migrations':", out.getvalue())
 
+    def test_makemigrations_check_no_changes(self):
+        """
+        makemigrations --check should exit with a zero status when there are no
+        changes.
+        """
+        out = io.StringIO()
         with self.temporary_migration_module(
             module="migrations.test_migrations_no_changes"
         ):
-            call_command("makemigrations", "--check", "migrations", verbosity=0)
+            call_command("makemigrations", "--check", "migrations", stdout=out)
+        self.assertEqual("No changes detected in app 'migrations'\n", out.getvalue())
 
     def test_makemigrations_migration_path_output(self):
         """