From 1a7b6909ac030d2c4dae2664b08ee0bce9c4c915 Mon Sep 17 00:00:00 2001 From: JunKi Yoon Date: Tue, 27 Sep 2022 21:10:18 +0900 Subject: [PATCH] Fixed #34052 -- Made migrate --check don't emit signals and output when up to date. --- django/core/management/commands/migrate.py | 24 +++++++++++++--------- tests/migrate_signals/tests.py | 12 +++++++++++ tests/migrations/test_commands.py | 20 ++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/django/core/management/commands/migrate.py b/django/core/management/commands/migrate.py index 1298cfaa30..1541843066 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -240,23 +240,27 @@ class Command(BaseCommand): self.stdout.write(" No migrations to prune.") plan = executor.migration_plan(targets) - exit_dry = plan and options["check_unapplied"] if options["plan"]: self.stdout.write("Planned operations:", self.style.MIGRATE_LABEL) if not plan: self.stdout.write(" No planned migration operations.") - for migration, backwards in plan: - self.stdout.write(str(migration), self.style.MIGRATE_HEADING) - for operation in migration.operations: - message, is_error = self.describe_operation(operation, backwards) - style = self.style.WARNING if is_error else None - self.stdout.write(" " + message, style) - if exit_dry: + else: + for migration, backwards in plan: + self.stdout.write(str(migration), self.style.MIGRATE_HEADING) + for operation in migration.operations: + message, is_error = self.describe_operation( + operation, backwards + ) + style = self.style.WARNING if is_error else None + self.stdout.write(" " + message, style) + if options["check_unapplied"]: + sys.exit(1) + return + if options["check_unapplied"]: + if plan: sys.exit(1) return - if exit_dry: - sys.exit(1) if options["prune"]: return diff --git a/tests/migrate_signals/tests.py b/tests/migrate_signals/tests.py index f5b3cb506f..01b62dc3c0 100644 --- a/tests/migrate_signals/tests.py +++ b/tests/migrate_signals/tests.py @@ -156,3 +156,15 @@ class MigrateSignalTests(TransactionTestCase): ], ["migrate_signals.Signal"], ) + # Migrating with an empty plan and --check doesn't emit signals. + pre_migrate_receiver = Receiver(signals.pre_migrate) + post_migrate_receiver = Receiver(signals.post_migrate) + management.call_command( + "migrate", + database=MIGRATE_DATABASE, + verbosity=MIGRATE_VERBOSITY, + interactive=MIGRATE_INTERACTIVE, + check_unapplied=True, + ) + self.assertEqual(pre_migrate_receiver.call_counter, 0) + self.assertEqual(post_migrate_receiver.call_counter, 0) diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 61a914f8ce..9900c7a35d 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -355,6 +355,26 @@ class MigrateTests(MigrationTestBase): self.assertTableNotExists("migrations_tribble") self.assertTableNotExists("migrations_book") + @override_settings( + INSTALLED_APPS=[ + "migrations.migrations_test_apps.migrated_app", + ] + ) + def test_migrate_check_migrated_app(self): + out = io.StringIO() + try: + call_command("migrate", "migrated_app", verbosity=0) + call_command( + "migrate", + "migrated_app", + stdout=out, + check_unapplied=True, + ) + self.assertEqual(out.getvalue(), "") + finally: + # Unmigrate everything. + call_command("migrate", "migrated_app", "zero", verbosity=0) + @override_settings( MIGRATION_MODULES={ "migrations": "migrations.test_migrations_plan",