Skip to content

Commit 2928019

Browse files
chinmoy12cfelixxm
authored andcommitted
Fixed #31645 -- Enhanced the migration warning for migrate commmand.
Added the list of apps with changes not reflected in migrations.
1 parent aeb8996 commit 2928019

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

django/core/management/commands/migrate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ def handle(self, *args, **options):
227227
changes = autodetector.changes(graph=executor.loader.graph)
228228
if changes:
229229
self.stdout.write(self.style.NOTICE(
230-
" Your models have changes that are not yet reflected "
231-
"in a migration, and so won't be applied."
230+
" Your models in app(s): %s have changes that are not "
231+
"yet reflected in a migration, and so won't be "
232+
"applied." % ", ".join(repr(app) for app in sorted(changes))
232233
))
233234
self.stdout.write(self.style.NOTICE(
234235
" Run 'manage.py makemigrations' to make new "

tests/migrations/test_commands.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,40 @@ def test_migrate_inconsistent_history(self):
890890
applied_migrations = recorder.applied_migrations()
891891
self.assertNotIn(("migrations", "0001_initial"), applied_migrations)
892892

893+
@override_settings(INSTALLED_APPS=[
894+
'migrations.migrations_test_apps.migrated_unapplied_app',
895+
'migrations.migrations_test_apps.migrated_app',
896+
])
897+
def test_migrate_not_reflected_changes(self):
898+
class NewModel1(models.Model):
899+
class Meta():
900+
app_label = 'migrated_app'
901+
902+
class NewModel2(models.Model):
903+
class Meta():
904+
app_label = 'migrated_unapplied_app'
905+
906+
out = io.StringIO()
907+
try:
908+
call_command('migrate', verbosity=0)
909+
call_command('migrate', stdout=out, no_color=True)
910+
self.assertEqual(
911+
"operations to perform:\n"
912+
" apply all migrations: migrated_app, migrated_unapplied_app\n"
913+
"running migrations:\n"
914+
" no migrations to apply.\n"
915+
" your models in app(s): 'migrated_app', "
916+
"'migrated_unapplied_app' have changes that are not yet "
917+
"reflected in a migration, and so won't be applied.\n"
918+
" run 'manage.py makemigrations' to make new migrations, and "
919+
"then re-run 'manage.py migrate' to apply them.\n",
920+
out.getvalue().lower(),
921+
)
922+
finally:
923+
# Unmigrate everything.
924+
call_command('migrate', 'migrated_app', 'zero', verbosity=0)
925+
call_command('migrate', 'migrated_unapplied_app', 'zero', verbosity=0)
926+
893927

894928
class MakeMigrationsTests(MigrationTestBase):
895929
"""

0 commit comments

Comments
 (0)