From 2e613ea5c52d4d12e071987178af18c0371d6e2f Mon Sep 17 00:00:00 2001 From: Moayad Mardini Date: Mon, 26 May 2014 23:09:17 +0300 Subject: [PATCH] Fixed #22675 -- makemigrations --dry-run to output migrations to stdout. `makemigrations --dry-run` will output the complete migrations file that would be written if it's used along with `--verbosity 3`. --- .../management/commands/makemigrations.py | 8 ++++- docs/ref/django-admin.txt | 4 ++- tests/migrations/test_commands.py | 31 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index 97661ea8b5..839592a22a 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -123,8 +123,8 @@ class Command(BaseCommand): self.stdout.write(" %s:\n" % (self.style.MIGRATE_LABEL(writer.filename),)) for operation in migration.operations: self.stdout.write(" - %s\n" % operation.describe()) - # Write it if not self.dry_run: + # Write the migrations file to the disk. migrations_directory = os.path.dirname(writer.path) if not directory_created.get(app_label, False): if not os.path.isdir(migrations_directory): @@ -137,6 +137,12 @@ class Command(BaseCommand): migration_string = writer.as_string() with open(writer.path, "wb") as fh: fh.write(migration_string) + elif self.verbosity == 3: + # Alternatively, makemigrations --dry-run --verbosity 3 + # will output the migrations to stdout rather than saving + # the file to the disk. + self.stdout.write(self.style.MIGRATE_HEADING("Full migrations file '%s':" % writer.filename) + "\n") + self.stdout.write("%s\n" % writer.as_string()) def handle_merge(self, loader, conflicts): """ diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index d3c3177858..6bb67f01fc 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -663,7 +663,9 @@ your migrations. .. django-admin-option:: --dry-run The ``--dry-run`` option shows what migrations would be made without -actually writing any migrations files to disk. +actually writing any migrations files to disk. Using this option along with +``--verbosity 3`` will also show the complete migrations files that would be +written. .. django-admin-option:: --merge diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 2f78ee27a4..4783ffa0c3 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -382,3 +382,34 @@ class MakeMigrationsTests(MigrationTestBase): call_command("makemigrations", "migrations", dry_run=True, stdout=stdout) # Output the expected changes directly, without asking for defaults self.assertIn("Add field silly_date to sillymodel", stdout.getvalue()) + + @override_system_checks([]) + @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_default"}) + def test_makemigrations_dry_run_verbosity_3(self): + """ + Ticket #22675 -- Allow `makemigrations --dry-run` to output the + migrations file to stdout (with verbosity == 3). + """ + + class SillyModel(models.Model): + silly_field = models.BooleanField(default=False) + silly_char = models.CharField(default="") + + class Meta: + app_label = "migrations" + + stdout = six.StringIO() + call_command("makemigrations", "migrations", dry_run=True, stdout=stdout, verbosity=3) + + # Normal --dry-run output + self.assertIn("- Add field silly_char to sillymodel", stdout.getvalue()) + + # Additional output caused by verbosity 3 + # The complete migrations file that would be written + self.assertIn("# -*- coding: utf-8 -*-", stdout.getvalue()) + self.assertIn("class Migration(migrations.Migration):", stdout.getvalue()) + self.assertIn("dependencies = [", stdout.getvalue()) + self.assertIn("('migrations', '0001_initial'),", stdout.getvalue()) + self.assertIn("migrations.AddField(", stdout.getvalue()) + self.assertIn("model_name='sillymodel',", stdout.getvalue()) + self.assertIn("name='silly_char',", stdout.getvalue())