summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Giannelos <johngiannelos@gmail.com>2015-03-03 00:13:14 +0200
committerTim Graham <timograham@gmail.com>2015-03-16 19:45:55 -0400
commite150bbd4d65b4b2c3897bdabdda94eba508747a2 (patch)
treec1f236c4badb19118bcb1134b5e72a04dd176d8e
parentfebcbbf89d978b60ffe13ab839dcc98639c3f522 (diff)
[1.8.x] Fixed #24427 -- Stopped writing migration files in dry run mode when merging.
Also added display of migration to stdout when verbosity=3. Backport of 8758a63ddbbf7a2626bd84d50cfe83b477e8de0a from master
-rw-r--r--django/core/management/commands/makemigrations.py19
-rw-r--r--docs/releases/1.7.7.txt4
-rw-r--r--tests/migrations/test_commands.py43
3 files changed, 62 insertions, 4 deletions
diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py
index cb78107d33..1b5addcafd 100644
--- a/django/core/management/commands/makemigrations.py
+++ b/django/core/management/commands/makemigrations.py
@@ -234,7 +234,18 @@ class Command(BaseCommand):
})
new_migration = subclass("%04i_merge" % (biggest_number + 1), app_label)
writer = MigrationWriter(new_migration)
- with open(writer.path, "wb") as fh:
- fh.write(writer.as_string())
- if self.verbosity > 0:
- self.stdout.write("\nCreated new merge migration %s" % writer.path)
+
+ if not self.dry_run:
+ # Write the merge migrations file to the disk
+ with open(writer.path, "wb") as fh:
+ fh.write(writer.as_string())
+ if self.verbosity > 0:
+ self.stdout.write("\nCreated new merge migration %s" % writer.path)
+ elif self.verbosity == 3:
+ # Alternatively, makemigrations --merge --dry-run --verbosity 3
+ # will output the merge migrations to stdout rather than saving
+ # the file to the disk.
+ self.stdout.write(self.style.MIGRATE_HEADING(
+ "Full merge migrations file '%s':" % writer.filename) + "\n"
+ )
+ self.stdout.write("%s\n" % writer.as_string())
diff --git a/docs/releases/1.7.7.txt b/docs/releases/1.7.7.txt
index 1c6b9325f4..8b4ae1575d 100644
--- a/docs/releases/1.7.7.txt
+++ b/docs/releases/1.7.7.txt
@@ -12,3 +12,7 @@ Bugfixes
* Fixed renaming of classes in migrations where renaming a subclass would
cause incorrect state to be recorded for objects that referenced the
superclass (:ticket:`24354`).
+
+* Stopped writing migration files in dry run mode when merging migration
+ conflicts. When ``makemigrations --merge`` is called with ``verbosity=3`` the
+ migration file is written to ``stdout`` (:ticket: `24427`).
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index f97ba05cb0..c8cf814e43 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -582,6 +582,49 @@ class MakeMigrationsTests(MigrationTestBase):
self.assertFalse(os.path.exists(merge_file))
self.assertIn("Created new merge migration", output)
+ @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"})
+ def test_makemigration_merge_dry_run(self):
+ """
+ Makes sure that makemigrations respects --dry-run option when fixing
+ migration conflicts (#24427).
+ """
+ out = six.StringIO()
+ call_command("makemigrations", "migrations", dry_run=True, merge=True, interactive=False, stdout=out)
+ merge_file = os.path.join(self.test_dir, '0003_merge.py')
+ self.assertFalse(os.path.exists(merge_file))
+ output = force_text(out.getvalue())
+ self.assertIn("Merging migrations", output)
+ self.assertIn("Branch 0002_second", output)
+ self.assertIn("Branch 0002_conflicting_second", output)
+ self.assertNotIn("Created new merge migration", output)
+
+ @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"})
+ def test_makemigration_merge_dry_run_verbosity_3(self):
+ """
+ Makes sure that `makemigrations --merge --dry-run` writes the merge
+ migration file to stdout with `verbosity == 3` (#24427).
+ """
+ out = six.StringIO()
+ call_command("makemigrations", "migrations", dry_run=True, merge=True, interactive=False,
+ stdout=out, verbosity=3)
+ merge_file = os.path.join(self.test_dir, '0003_merge.py')
+ self.assertFalse(os.path.exists(merge_file))
+ output = force_text(out.getvalue())
+ self.assertIn("Merging migrations", output)
+ self.assertIn("Branch 0002_second", output)
+ self.assertIn("Branch 0002_conflicting_second", output)
+ self.assertNotIn("Created new merge migration", output)
+
+ # Additional output caused by verbosity 3
+ # The complete merge migration file that would be written
+ self.assertIn("# -*- coding: utf-8 -*-", output)
+ self.assertIn("class Migration(migrations.Migration):", output)
+ self.assertIn("dependencies = [", output)
+ self.assertIn("('migrations', '0002_second')", output)
+ self.assertIn("('migrations', '0002_conflicting_second')", output)
+ self.assertIn("operations = [", output)
+ self.assertIn("]", output)
+
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_default"})
def test_makemigrations_dry_run(self):
"""