diff --git a/django/core/management/commands/optimizemigration.py b/django/core/management/commands/optimizemigration.py index 7651c39866..2064dfbf3c 100644 --- a/django/core/management/commands/optimizemigration.py +++ b/django/core/management/commands/optimizemigration.py @@ -1,3 +1,4 @@ +import shutil import sys from django.apps import apps @@ -111,6 +112,13 @@ class Command(BaseCommand): " See the comment at the top of the optimized migration for " "details." ) + if shutil.which("black"): + self.stdout.write( + self.style.WARNING( + "Optimized migration couldn't be formatted using the " + '"black" command. You can call it manually.' + ) + ) with open(writer.path, "w", encoding="utf-8") as fh: fh.write(migration_file_string) run_formatters([writer.path]) diff --git a/django/core/management/commands/squashmigrations.py b/django/core/management/commands/squashmigrations.py index 2d6e0ebfa3..a46edb3b7b 100644 --- a/django/core/management/commands/squashmigrations.py +++ b/django/core/management/commands/squashmigrations.py @@ -1,4 +1,5 @@ import os +import shutil from django.apps import apps from django.conf import settings @@ -244,6 +245,13 @@ class Command(BaseCommand): " See the comment at the top of the squashed migration for " "details." ) + if shutil.which("black"): + self.stdout.write( + self.style.WARNING( + "Squashed migration couldn't be formatted using the " + '"black" command. You can call it manually.' + ) + ) def find_migration(self, loader, app_label, name): try: diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 27a1ff6519..30136aab6b 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -2729,6 +2729,12 @@ class SquashMigrationsTests(MigrationTestBase): "0001_squashed_0002_second.py", ) self.assertTrue(os.path.exists(squashed_migration_file)) + black_warning = "" + if HAS_BLACK: + black_warning = ( + "Squashed migration couldn't be formatted using the " + '"black" command. You can call it manually.\n' + ) self.assertEqual( out.getvalue(), f"Will squash the following migrations:\n" @@ -2746,7 +2752,8 @@ class SquashMigrationsTests(MigrationTestBase): f" Your migrations contained functions that must be manually copied " f"over,\n" f" as we could not safely copy their implementation.\n" - f" See the comment at the top of the squashed migration for details.\n", + f" See the comment at the top of the squashed migration for details.\n" + + black_warning, ) @@ -2905,15 +2912,21 @@ class OptimizeMigrationTests(MigrationTestBase): with open(optimized_migration_file) as fp: content = fp.read() self.assertIn("replaces = [", content) + black_warning = "" + if HAS_BLACK: + black_warning = ( + "Optimized migration couldn't be formatted using the " + '"black" command. You can call it manually.\n' + ) self.assertEqual( out.getvalue(), - f"Optimizing from 3 operations to 2 operations.\n" - f"Manual porting required\n" - f" Your migrations contained functions that must be manually copied over," - f"\n" - f" as we could not safely copy their implementation.\n" - f" See the comment at the top of the optimized migration for details.\n" - f"Optimized migration {optimized_migration_file}\n", + "Optimizing from 3 operations to 2 operations.\n" + "Manual porting required\n" + " Your migrations contained functions that must be manually copied over,\n" + " as we could not safely copy their implementation.\n" + " See the comment at the top of the optimized migration for details.\n" + + black_warning + + f"Optimized migration {optimized_migration_file}\n", ) def test_fails_squash_migration_manual_porting(self):