Fixed #31703 -- Made makemigrations name all initial migrations "initial".

When the MigrationAutodetector creates more than one initial migration
in a app, name all initial migrations "initial" rather than the opaque
"auto_<DATE>_<TIME>" name.

Initial migrations that have a descriptive name continue to use the
descriptive name.
This commit is contained in:
Jon Dufresne 2020-06-13 10:06:05 -07:00 committed by Mariusz Felisiak
parent 01195c4a83
commit 6f3e3e87ab
2 changed files with 29 additions and 1 deletions

View File

@ -192,7 +192,7 @@ class Migration:
):
name = '_'.join(sorted(o.migration_name_fragment for o in self.operations))
if name is None:
name = 'auto_%s' % get_migration_name_timestamp()
name = 'initial' if self.initial else 'auto_%s' % get_migration_name_timestamp()
return name

View File

@ -612,6 +612,26 @@ class AutodetectorTests(TestCase):
self.assertEqual(changes["otherapp"][0].name, "0002_pony_stable")
self.assertEqual(changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")])
def test_arrange_for_graph_with_multiple_initial(self):
# Make a fake graph.
graph = MigrationGraph()
# Use project state to make a new migration change set.
before = self.make_project_state([])
after = self.make_project_state([self.author_with_book, self.book, self.attribution])
autodetector = MigrationAutodetector(before, after, MigrationQuestioner({'ask_initial': True}))
changes = autodetector._detect_changes()
changes = autodetector.arrange_for_graph(changes, graph)
self.assertEqual(changes['otherapp'][0].name, '0001_initial')
self.assertEqual(changes['otherapp'][0].dependencies, [])
self.assertEqual(changes['otherapp'][1].name, '0002_initial')
self.assertCountEqual(
changes['otherapp'][1].dependencies,
[('testapp', '0001_initial'), ('otherapp', '0001_initial')],
)
self.assertEqual(changes['testapp'][0].name, '0001_initial')
self.assertEqual(changes['testapp'][0].dependencies, [('otherapp', '0001_initial')])
def test_trim_apps(self):
"""
Trim does not remove dependencies but does remove unwanted apps.
@ -2524,6 +2544,14 @@ class MigrationSuggestNameTests(SimpleTestCase):
suggest_name = migration.suggest_name()
self.assertIs(suggest_name.startswith('auto_'), True)
def test_none_name_with_initial_true(self):
class Migration(migrations.Migration):
initial = True
operations = [migrations.RunSQL('SELECT 1 FROM person;')]
migration = Migration('0001_initial', 'test_app')
self.assertEqual(migration.suggest_name(), 'initial')
def test_auto(self):
migration = migrations.Migration('0001_initial', 'test_app')
suggest_name = migration.suggest_name()