From cdd970ae22303e6c58b5c1f3ba695c470a811b56 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 17 May 2023 13:14:43 +0200 Subject: [PATCH] [4.2.x] Fixed #34568 -- Made makemigrations --update respect --name option. Thanks David Sanders for the report. Backport of c52f4295f254e1c14af769d22b1a5f516a941f58 from main --- .../management/commands/makemigrations.py | 5 ++-- docs/ref/django-admin.txt | 3 +++ docs/releases/4.2.2.txt | 3 +++ tests/migrations/test_commands.py | 26 +++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index 284a95409f6..35661d49972 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -316,9 +316,8 @@ class Command(BaseCommand): ) # Update name. previous_migration_path = MigrationWriter(leaf_migration).path - suggested_name = ( - leaf_migration.name[:4] + "_" + leaf_migration.suggest_name() - ) + name_fragment = self.migration_name or leaf_migration.suggest_name() + suggested_name = leaf_migration.name[:4] + f"_{name_fragment}" if leaf_migration.name == suggested_name: new_name = leaf_migration.name + "_updated" else: diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 5e1be0d206d..1c43523d67a 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -759,6 +759,9 @@ generated migration files to ``stdout``. Merges model changes into the latest migration and optimize the resulting operations. +The updated migration will have a generated name. In order to preserve the +previous name, set it using ``--name``. + ``migrate`` ----------- diff --git a/docs/releases/4.2.2.txt b/docs/releases/4.2.2.txt index ca21e5f1ec9..7506b8a2371 100644 --- a/docs/releases/4.2.2.txt +++ b/docs/releases/4.2.2.txt @@ -19,3 +19,6 @@ Bugfixes * Fixed a regression in Django 4.2 that caused a crash of ``QuerySet.defer()`` when passing a ``ManyToManyField`` or ``GenericForeignKey`` reference. While doing so is a no-op, it was allowed in older version (:ticket:`34570`). + +* Fixed a bug in Django 4.2 where :option:`makemigrations --update` didn't + respect the ``--name`` option (:ticket:`34568`). diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 0117d1e4aa3..cabf0ec7120 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -2649,6 +2649,32 @@ class MakeMigrationsTests(MigrationTestBase): self.assertNotEqual(initial_content, fp.read()) self.assertIn(f"Deleted {migration_file}", out.getvalue()) + def test_makemigrations_update_custom_name(self): + custom_name = "delete_something" + with self.temporary_migration_module( + module="migrations.test_migrations" + ) as migration_dir: + old_migration_file = os.path.join(migration_dir, "0002_second.py") + with open(old_migration_file) as fp: + initial_content = fp.read() + + with captured_stdout() as out: + call_command( + "makemigrations", "migrations", update=True, name=custom_name + ) + self.assertFalse( + any( + filename.startswith("0003") + for filename in os.listdir(migration_dir) + ) + ) + self.assertIs(os.path.exists(old_migration_file), False) + new_migration_file = os.path.join(migration_dir, f"0002_{custom_name}.py") + self.assertIs(os.path.exists(new_migration_file), True) + with open(new_migration_file) as fp: + self.assertNotEqual(initial_content, fp.read()) + self.assertIn(f"Deleted {old_migration_file}", out.getvalue()) + def test_makemigrations_update_applied_migration(self): recorder = MigrationRecorder(connection) recorder.record_applied("migrations", "0001_initial")