Fixed #23728 -- Added the --exit option to makemigrations.

If no changes that need migrations are found, `makemigrations --exit`
exits with error code 1.
This commit is contained in:
Tim Heap 2014-11-11 15:41:55 +11:00 committed by Tim Graham
parent ab89414f40
commit deb607648e
5 changed files with 40 additions and 8 deletions

View File

@ -649,6 +649,7 @@ answer newbie questions, and generally made Django that much better:
Thomas Stromberg <tstromberg@google.com> Thomas Stromberg <tstromberg@google.com>
tibimicu@gmx.net tibimicu@gmx.net
Tim Graham <timograham@gmail.com> Tim Graham <timograham@gmail.com>
Tim Heap <tim@timheap.me>
Tim Saylor <tim.saylor@gmail.com> Tim Saylor <tim.saylor@gmail.com>
Tobias McNulty <http://www.caktusgroup.com/blog> Tobias McNulty <http://www.caktusgroup.com/blog>
tobias@neuyork.de tobias@neuyork.de

View File

@ -30,6 +30,8 @@ class Command(BaseCommand):
help='Tells Django to NOT prompt the user for input of any kind.') help='Tells Django to NOT prompt the user for input of any kind.')
parser.add_argument('-n', '--name', action='store', dest='name', default=None, parser.add_argument('-n', '--name', action='store', dest='name', default=None,
help="Use this name for migration file(s).") help="Use this name for migration file(s).")
parser.add_argument('-e', '--exit', action='store_true', dest='exit_code', default=False,
help='Exit with error code 1 if no changes needing migrations are found.')
def handle(self, *app_labels, **options): def handle(self, *app_labels, **options):
@ -39,6 +41,7 @@ class Command(BaseCommand):
self.merge = options.get('merge', False) self.merge = options.get('merge', False)
self.empty = options.get('empty', False) self.empty = options.get('empty', False)
self.migration_name = options.get('name', None) self.migration_name = options.get('name', None)
self.exit_code = options.get('exit_code', False)
# Make sure the app they asked for exists # Make sure the app they asked for exists
app_labels = set(app_labels) app_labels = set(app_labels)
@ -120,14 +123,19 @@ class Command(BaseCommand):
migration_name=self.migration_name, migration_name=self.migration_name,
) )
if not changes:
# No changes? Tell them. # No changes? Tell them.
if not changes and self.verbosity >= 1: if self.verbosity >= 1:
if len(app_labels) == 1: if len(app_labels) == 1:
self.stdout.write("No changes detected in app '%s'" % app_labels.pop()) self.stdout.write("No changes detected in app '%s'" % app_labels.pop())
elif len(app_labels) > 1: elif len(app_labels) > 1:
self.stdout.write("No changes detected in apps '%s'" % ("', '".join(app_labels))) self.stdout.write("No changes detected in apps '%s'" % ("', '".join(app_labels)))
else: else:
self.stdout.write("No changes detected") self.stdout.write("No changes detected")
if self.exit_code:
sys.exit(1)
else:
return return
self.write_migration_files(changes) self.write_migration_files(changes)

View File

@ -719,6 +719,14 @@ a merge.
The ``--name`` option allows you to give the migration(s) a custom name instead The ``--name`` option allows you to give the migration(s) a custom name instead
of a generated one. of a generated one.
.. django-admin-option:: --exit, -e
.. versionadded:: 1.8
The ``--exit`` option will cause ``makemigrations`` to exit with error code 1
when no migration are created (or would have been created, if combined with
``--dry-run``).
migrate [<app_label> [<migrationname>]] migrate [<app_label> [<migrationname>]]
--------------------------------------- ---------------------------------------

View File

@ -333,6 +333,9 @@ Management Commands
:setting:`FIXTURE_DIRS` contains duplicates or a default fixture directory :setting:`FIXTURE_DIRS` contains duplicates or a default fixture directory
path (``app_name/fixtures``), an exception is raised. path (``app_name/fixtures``), an exception is raised.
* :djadmin:`makemigrations` now supports an :djadminopt:`--exit` option to
exit with an error code if no migrations are created.
Middleware Middleware
^^^^^^^^^^ ^^^^^^^^^^

View File

@ -543,6 +543,18 @@ class MakeMigrationsTests(MigrationTestBase):
self.assertIn("dependencies=[\n('migrations','0001_%s'),\n]" % migration_name_0001, content) self.assertIn("dependencies=[\n('migrations','0001_%s'),\n]" % migration_name_0001, content)
self.assertIn("operations=[\n]", content) self.assertIn("operations=[\n]", content)
def test_makemigrations_exit(self):
"""
makemigrations --exit should exit with sys.exit(1) when there are no
changes to an app.
"""
with self.settings(MIGRATION_MODULES={"migrations": self.migration_pkg}):
call_command("makemigrations", "--exit", "migrations", verbosity=0)
with self.settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_changes"}):
with self.assertRaises(SystemExit):
call_command("makemigrations", "--exit", "migrations", verbosity=0)
class SquashMigrationsTest(MigrationTestBase): class SquashMigrationsTest(MigrationTestBase):
""" """