Skip to content

Commit 8758a63

Browse files
johngiantimgraham
authored andcommitted
Fixed #24427 -- Stopped writing migration files in dry run mode when merging.
Also added display of migration to stdout when verbosity=3.
1 parent 818182b commit 8758a63

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

django/core/management/commands/makemigrations.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,18 @@ def handle_merge(self, loader, conflicts):
239239
})
240240
new_migration = subclass("%04i_merge" % (biggest_number + 1), app_label)
241241
writer = MigrationWriter(new_migration)
242-
with open(writer.path, "wb") as fh:
243-
fh.write(writer.as_string())
244-
if self.verbosity > 0:
245-
self.stdout.write("\nCreated new merge migration %s" % writer.path)
242+
243+
if not self.dry_run:
244+
# Write the merge migrations file to the disk
245+
with open(writer.path, "wb") as fh:
246+
fh.write(writer.as_string())
247+
if self.verbosity > 0:
248+
self.stdout.write("\nCreated new merge migration %s" % writer.path)
249+
elif self.verbosity == 3:
250+
# Alternatively, makemigrations --merge --dry-run --verbosity 3
251+
# will output the merge migrations to stdout rather than saving
252+
# the file to the disk.
253+
self.stdout.write(self.style.MIGRATE_HEADING(
254+
"Full merge migrations file '%s':" % writer.filename) + "\n"
255+
)
256+
self.stdout.write("%s\n" % writer.as_string())

docs/releases/1.7.7.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ Bugfixes
1212
* Fixed renaming of classes in migrations where renaming a subclass would
1313
cause incorrect state to be recorded for objects that referenced the
1414
superclass (:ticket:`24354`).
15+
16+
* Stopped writing migration files in dry run mode when merging migration
17+
conflicts. When ``makemigrations --merge`` is called with ``verbosity=3`` the
18+
migration file is written to ``stdout`` (:ticket: `24427`).

tests/migrations/test_commands.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,49 @@ def test_makemigrations_handle_merge(self):
620620
self.assertIn("Branch 0002_conflicting_second", output)
621621
self.assertIn("Created new merge migration", output)
622622

623+
def test_makemigration_merge_dry_run(self):
624+
"""
625+
Makes sure that makemigrations respects --dry-run option when fixing
626+
migration conflicts (#24427).
627+
"""
628+
out = six.StringIO()
629+
with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
630+
call_command("makemigrations", "migrations", dry_run=True, merge=True, interactive=False, stdout=out)
631+
merge_file = os.path.join(migration_dir, '0003_merge.py')
632+
self.assertFalse(os.path.exists(merge_file))
633+
output = force_text(out.getvalue())
634+
self.assertIn("Merging migrations", output)
635+
self.assertIn("Branch 0002_second", output)
636+
self.assertIn("Branch 0002_conflicting_second", output)
637+
self.assertNotIn("Created new merge migration", output)
638+
639+
def test_makemigration_merge_dry_run_verbosity_3(self):
640+
"""
641+
Makes sure that `makemigrations --merge --dry-run` writes the merge
642+
migration file to stdout with `verbosity == 3` (#24427).
643+
"""
644+
out = six.StringIO()
645+
with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
646+
call_command("makemigrations", "migrations", dry_run=True, merge=True, interactive=False,
647+
stdout=out, verbosity=3)
648+
merge_file = os.path.join(migration_dir, '0003_merge.py')
649+
self.assertFalse(os.path.exists(merge_file))
650+
output = force_text(out.getvalue())
651+
self.assertIn("Merging migrations", output)
652+
self.assertIn("Branch 0002_second", output)
653+
self.assertIn("Branch 0002_conflicting_second", output)
654+
self.assertNotIn("Created new merge migration", output)
655+
656+
# Additional output caused by verbosity 3
657+
# The complete merge migration file that would be written
658+
self.assertIn("# -*- coding: utf-8 -*-", output)
659+
self.assertIn("class Migration(migrations.Migration):", output)
660+
self.assertIn("dependencies = [", output)
661+
self.assertIn("('migrations', '0002_second')", output)
662+
self.assertIn("('migrations', '0002_conflicting_second')", output)
663+
self.assertIn("operations = [", output)
664+
self.assertIn("]", output)
665+
623666
def test_makemigrations_dry_run(self):
624667
"""
625668
Ticket #22676 -- `makemigrations --dry-run` should not ask for defaults.

0 commit comments

Comments
 (0)