From f7389c4b07ceeb036436e065898e411b247bca78 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 8 Nov 2023 22:29:07 +0000 Subject: [PATCH] Fixed #34457 -- Restored output for makemigrations --check. Co-authored-by: David Sanders Co-authored-by: Natalia <124304+nessita@users.noreply.github.com> --- .../management/commands/makemigrations.py | 8 +++--- docs/ref/django-admin.txt | 2 +- docs/releases/4.2.8.txt | 3 ++- tests/migrations/test_commands.py | 25 +++++++++++++++---- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index 35661d4997..22498af3c0 100644 --- a/django/core/management/commands/makemigrations.py +++ b/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]) diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 86cac790fa..7f3fa271c8 100644 --- a/docs/ref/django-admin.txt +++ b/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``. .. django-admin-option:: --scriptable diff --git a/docs/releases/4.2.8.txt b/docs/releases/4.2.8.txt index b8bf62cdc9..5804589408 100644 --- a/docs/releases/4.2.8.txt +++ b/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`). diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 387cef924a..a9c1cdf893 100644 --- a/tests/migrations/test_commands.py +++ b/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): """