From 796be5901a67da44e251e092641682cb2d778176 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 6 Jul 2020 17:08:25 -0700 Subject: [PATCH] Fixed #31769 -- Improved default naming of merged migrations. 47 gives 60 in total (47 + 5 + 5 + 3). --- .../management/commands/makemigrations.py | 15 +++++++++++---- tests/migrations/test_commands.py | 19 +++++++++++++++++-- .../0001_initial.py | 14 ++++++++++++++ ...licting_second_migration_with_long_name.py | 14 ++++++++++++++ .../0002_second.py | 14 ++++++++++++++ .../__init__.py | 0 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 tests/migrations/test_migrations_conflict_long_name/0001_initial.py create mode 100644 tests/migrations/test_migrations_conflict_long_name/0002_conflicting_second_migration_with_long_name.py create mode 100644 tests/migrations/test_migrations_conflict_long_name/0002_second.py create mode 100644 tests/migrations/test_migrations_conflict_long_name/__init__.py diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index c8c688ec94..5c0a7829b4 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -295,10 +295,17 @@ class Command(BaseCommand): subclass = type("Migration", (Migration,), { "dependencies": [(app_label, migration.name) for migration in merge_migrations], }) - migration_name = "%04i_%s" % ( - biggest_number + 1, - self.migration_name or ("merge_%s" % get_migration_name_timestamp()) - ) + parts = ['%04i' % (biggest_number + 1)] + if self.migration_name: + parts.append(self.migration_name) + else: + parts.append('merge') + leaf_names = '_'.join(sorted(migration.name for migration in merge_migrations)) + if len(leaf_names) > 47: + parts.append(get_migration_name_timestamp()) + else: + parts.append(leaf_names) + migration_name = '_'.join(parts) new_migration = subclass(migration_name, app_label) writer = MigrationWriter(new_migration, self.include_header) diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index fb620aab9a..79dd95133c 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -1208,12 +1208,27 @@ class MakeMigrationsTests(MigrationTestBase): self.assertTrue(os.path.exists(merge_file)) self.assertIn("Created new merge migration", out.getvalue()) + def test_makemigrations_default_merge_name(self): + out = io.StringIO() + with self.temporary_migration_module( + module='migrations.test_migrations_conflict' + ) as migration_dir: + call_command('makemigrations', 'migrations', merge=True, interactive=False, stdout=out) + merge_file = os.path.join( + migration_dir, + '0003_merge_0002_conflicting_second_0002_second.py', + ) + self.assertIs(os.path.exists(merge_file), True) + self.assertIn('Created new merge migration %s' % merge_file, out.getvalue()) + @mock.patch('django.db.migrations.utils.datetime') - def test_makemigrations_default_merge_name(self, mock_datetime): + def test_makemigrations_auto_merge_name(self, mock_datetime): mock_datetime.datetime.now.return_value = datetime.datetime(2016, 1, 2, 3, 4) with mock.patch('builtins.input', mock.Mock(return_value='y')): out = io.StringIO() - with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir: + with self.temporary_migration_module( + module='migrations.test_migrations_conflict_long_name' + ) as migration_dir: call_command("makemigrations", "migrations", merge=True, interactive=True, stdout=out) merge_file = os.path.join(migration_dir, '0003_merge_20160102_0304.py') self.assertTrue(os.path.exists(merge_file)) diff --git a/tests/migrations/test_migrations_conflict_long_name/0001_initial.py b/tests/migrations/test_migrations_conflict_long_name/0001_initial.py new file mode 100644 index 0000000000..e3faa21b3d --- /dev/null +++ b/tests/migrations/test_migrations_conflict_long_name/0001_initial.py @@ -0,0 +1,14 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + initial = True + + operations = [ + migrations.CreateModel( + 'Author', + [ + ('id', models.AutoField(primary_key=True)), + ], + ), + ] diff --git a/tests/migrations/test_migrations_conflict_long_name/0002_conflicting_second_migration_with_long_name.py b/tests/migrations/test_migrations_conflict_long_name/0002_conflicting_second_migration_with_long_name.py new file mode 100644 index 0000000000..747bd7a675 --- /dev/null +++ b/tests/migrations/test_migrations_conflict_long_name/0002_conflicting_second_migration_with_long_name.py @@ -0,0 +1,14 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [('migrations', '0001_initial')] + + operations = [ + migrations.CreateModel( + 'SomethingElse', + [ + ('id', models.AutoField(primary_key=True)), + ], + ), + ] diff --git a/tests/migrations/test_migrations_conflict_long_name/0002_second.py b/tests/migrations/test_migrations_conflict_long_name/0002_second.py new file mode 100644 index 0000000000..468dd06938 --- /dev/null +++ b/tests/migrations/test_migrations_conflict_long_name/0002_second.py @@ -0,0 +1,14 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [('migrations', '0001_initial')] + + operations = [ + migrations.CreateModel( + 'Something', + [ + ('id', models.AutoField(primary_key=True)), + ], + ), + ] diff --git a/tests/migrations/test_migrations_conflict_long_name/__init__.py b/tests/migrations/test_migrations_conflict_long_name/__init__.py new file mode 100644 index 0000000000..e69de29bb2