Browse Source

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`.
Moayad Mardini 10 years ago
parent
commit
2e613ea5c5

+ 7 - 1
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):
         """

+ 3 - 1
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
 

+ 31 - 0
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())