From 4a756cbc383bdefb683f03a0d40c0d0630ba3f60 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 14 Oct 2019 09:37:45 +0200 Subject: [PATCH] [3.0.x] Fixed #30870 -- Fixed showing that RunPython operations are irreversible by migrate --plan. Thanks Hasan Ramezani for the initial patch and Kyle Dickerson for the report. Backport of 06d34aab7cfb1632a1538a243db81f24498525ff from master --- django/core/management/commands/migrate.py | 10 ++++----- docs/releases/2.2.7.txt | 4 ++++ tests/migrations/test_commands.py | 22 +++++++++++++++++++ .../test_migrations_plan/0005_fifth.py | 22 +++++++++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/migrations/test_migrations_plan/0005_fifth.py diff --git a/django/core/management/commands/migrate.py b/django/core/management/commands/migrate.py index 37914e2622..5b964c41ae 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -343,21 +343,21 @@ class Command(BaseCommand): def describe_operation(operation, backwards): """Return a string that describes a migration operation for --plan.""" prefix = '' + is_error = False if hasattr(operation, 'code'): code = operation.reverse_code if backwards else operation.code - action = code.__doc__ if code else '' + action = (code.__doc__ or '') if code else None elif hasattr(operation, 'sql'): action = operation.reverse_sql if backwards else operation.sql else: action = '' if backwards: prefix = 'Undo ' - if action is None: + if action is not None: + action = str(action).replace('\n', '') + elif backwards: action = 'IRREVERSIBLE' is_error = True - else: - action = str(action).replace('\n', '') - is_error = False if action: action = ' -> ' + action truncated = Truncator(action) diff --git a/docs/releases/2.2.7.txt b/docs/releases/2.2.7.txt index f39587e43e..3232b5c5e8 100644 --- a/docs/releases/2.2.7.txt +++ b/docs/releases/2.2.7.txt @@ -13,3 +13,7 @@ Bugfixes ``has_keys``, or ``has_any_keys`` lookup on :class:`~django.contrib.postgres.fields.JSONField`, if the right or left hand side of an expression is a key transform (:ticket:`30826`). + +* Prevented :option:`migrate --plan` from showing that ``RunPython`` operations + are irreversible when ``reverse_code`` callables don't have docstrings or + when showing a forward migration plan (:ticket:`30870`). diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index f14f74b9a0..125be5de0e 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -371,6 +371,28 @@ class MigrateTests(MigrationTestBase): ' Raw SQL operation -> IRREVERSIBLE\n', out.getvalue() ) + out = io.StringIO() + call_command('migrate', 'migrations', '0005', plan=True, stdout=out, no_color=True) + # Operation is marked as irreversible only in the revert plan. + self.assertEqual( + 'Planned operations:\n' + 'migrations.0005_fifth\n' + ' Raw Python operation\n' + ' Raw Python operation\n' + ' Raw Python operation -> Feed salamander.\n', + out.getvalue() + ) + call_command('migrate', 'migrations', '0005', verbosity=0) + out = io.StringIO() + call_command('migrate', 'migrations', '0004', plan=True, stdout=out, no_color=True) + self.assertEqual( + 'Planned operations:\n' + 'migrations.0005_fifth\n' + ' Raw Python operation -> IRREVERSIBLE\n' + ' Raw Python operation -> IRREVERSIBLE\n' + ' Raw Python operation\n', + out.getvalue() + ) finally: # Cleanup by unmigrating everything: fake the irreversible, then # migrate all to zero. diff --git a/tests/migrations/test_migrations_plan/0005_fifth.py b/tests/migrations/test_migrations_plan/0005_fifth.py new file mode 100644 index 0000000000..3c569ffded --- /dev/null +++ b/tests/migrations/test_migrations_plan/0005_fifth.py @@ -0,0 +1,22 @@ +from django.db import migrations + + +def grow_tail(x, y): + pass + + +def feed(x, y): + """Feed salamander.""" + pass + + +class Migration(migrations.Migration): + dependencies = [ + ('migrations', '0004_fourth'), + ] + + operations = [ + migrations.RunPython(migrations.RunPython.noop), + migrations.RunPython(grow_tail), + migrations.RunPython(feed, migrations.RunPython.noop), + ]