2014-09-03 16:51:07 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections
|
2014-09-03 16:51:07 +08:00
|
|
|
from django.db.migrations.loader import MigrationLoader
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Shows all available migrations for the current project"
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'app_label', nargs='*',
|
|
|
|
help='App labels of applications to limit the output to.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--database', default=DEFAULT_DB_ALIAS,
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Nominates a database to synchronize. Defaults to the "default" database.',
|
|
|
|
)
|
2014-09-03 16:51:07 +08:00
|
|
|
|
|
|
|
formats = parser.add_mutually_exclusive_group()
|
2016-03-29 06:33:29 +08:00
|
|
|
formats.add_argument(
|
|
|
|
'--list', '-l', action='store_const', dest='format', const='list',
|
|
|
|
help='Shows a list of all migrations and which are applied.',
|
|
|
|
)
|
|
|
|
formats.add_argument(
|
|
|
|
'--plan', '-p', action='store_const', dest='format', const='plan',
|
2016-05-08 08:58:15 +08:00
|
|
|
help=(
|
|
|
|
'Shows all migrations in the order they will be applied. '
|
|
|
|
'With a verbosity level of 2 or above all direct migration dependencies '
|
|
|
|
'and reverse dependencies (run_before) will be included.'
|
|
|
|
)
|
2016-03-29 06:33:29 +08:00
|
|
|
)
|
2014-09-03 16:51:07 +08:00
|
|
|
|
|
|
|
parser.set_defaults(format='list')
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-03-03 10:01:36 +08:00
|
|
|
self.verbosity = options['verbosity']
|
2014-09-03 16:51:07 +08:00
|
|
|
|
|
|
|
# Get the database we're operating from
|
2016-03-03 10:01:36 +08:00
|
|
|
db = options['database']
|
2014-09-03 16:51:07 +08:00
|
|
|
connection = connections[db]
|
|
|
|
|
|
|
|
if options['format'] == "plan":
|
2016-11-06 20:52:06 +08:00
|
|
|
return self.show_plan(connection, options['app_label'])
|
2014-09-03 16:51:07 +08:00
|
|
|
else:
|
2016-01-12 09:59:34 +08:00
|
|
|
return self.show_list(connection, options['app_label'])
|
2014-09-03 16:51:07 +08:00
|
|
|
|
2016-11-06 20:52:06 +08:00
|
|
|
def _validate_app_names(self, loader, app_names):
|
|
|
|
invalid_apps = []
|
|
|
|
for app_name in app_names:
|
|
|
|
if app_name not in loader.migrated_apps:
|
|
|
|
invalid_apps.append(app_name)
|
|
|
|
if invalid_apps:
|
|
|
|
raise CommandError('No migrations present for: %s' % (', '.join(sorted(invalid_apps))))
|
|
|
|
|
2014-09-03 16:51:07 +08:00
|
|
|
def show_list(self, connection, app_names=None):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Show a list of all migrations on the system, or only those of
|
2014-09-03 16:51:07 +08:00
|
|
|
some named apps.
|
|
|
|
"""
|
|
|
|
# Load migrations from disk/DB
|
2015-01-01 06:26:15 +08:00
|
|
|
loader = MigrationLoader(connection, ignore_no_migrations=True)
|
2014-09-03 16:51:07 +08:00
|
|
|
graph = loader.graph
|
|
|
|
# If we were passed a list of apps, validate it
|
|
|
|
if app_names:
|
2016-11-06 20:52:06 +08:00
|
|
|
self._validate_app_names(loader, app_names)
|
2014-09-03 16:51:07 +08:00
|
|
|
# Otherwise, show all apps in alphabetic order
|
|
|
|
else:
|
|
|
|
app_names = sorted(loader.migrated_apps)
|
|
|
|
# For each app, print its migrations in order from oldest (roots) to
|
|
|
|
# newest (leaves).
|
|
|
|
for app_name in app_names:
|
|
|
|
self.stdout.write(app_name, self.style.MIGRATE_LABEL)
|
|
|
|
shown = set()
|
|
|
|
for node in graph.leaf_nodes(app_name):
|
|
|
|
for plan_node in graph.forwards_plan(node):
|
|
|
|
if plan_node not in shown and plan_node[0] == app_name:
|
|
|
|
# Give it a nice title if it's a squashed one
|
|
|
|
title = plan_node[1]
|
|
|
|
if graph.nodes[plan_node].replaces:
|
|
|
|
title += " (%s squashed migrations)" % len(graph.nodes[plan_node].replaces)
|
|
|
|
# Mark it as applied/unapplied
|
|
|
|
if plan_node in loader.applied_migrations:
|
|
|
|
self.stdout.write(" [X] %s" % title)
|
|
|
|
else:
|
|
|
|
self.stdout.write(" [ ] %s" % title)
|
|
|
|
shown.add(plan_node)
|
|
|
|
# If we didn't print anything, then a small message
|
|
|
|
if not shown:
|
2015-11-18 07:33:03 +08:00
|
|
|
self.stdout.write(" (no migrations)", self.style.ERROR)
|
2014-09-03 16:51:07 +08:00
|
|
|
|
2016-11-06 20:52:06 +08:00
|
|
|
def show_plan(self, connection, app_names=None):
|
2014-09-03 16:51:07 +08:00
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Show all known migrations (or only those of the specified app_names)
|
2016-11-06 20:52:06 +08:00
|
|
|
in the order they will be applied.
|
2014-09-03 16:51:07 +08:00
|
|
|
"""
|
|
|
|
# Load migrations from disk/DB
|
|
|
|
loader = MigrationLoader(connection)
|
|
|
|
graph = loader.graph
|
2016-11-06 20:52:06 +08:00
|
|
|
if app_names:
|
|
|
|
self._validate_app_names(loader, app_names)
|
|
|
|
targets = [key for key in graph.leaf_nodes() if key[0] in app_names]
|
|
|
|
else:
|
|
|
|
targets = graph.leaf_nodes()
|
2014-09-03 16:51:07 +08:00
|
|
|
plan = []
|
|
|
|
seen = set()
|
|
|
|
|
|
|
|
# Generate the plan
|
|
|
|
for target in targets:
|
|
|
|
for migration in graph.forwards_plan(target):
|
|
|
|
if migration not in seen:
|
2016-05-08 08:58:15 +08:00
|
|
|
node = graph.node_map[migration]
|
|
|
|
plan.append(node)
|
2014-09-03 16:51:07 +08:00
|
|
|
seen.add(migration)
|
|
|
|
|
|
|
|
# Output
|
2016-05-08 08:58:15 +08:00
|
|
|
def print_deps(node):
|
2014-09-03 16:51:07 +08:00
|
|
|
out = []
|
2016-05-08 08:58:15 +08:00
|
|
|
for parent in sorted(node.parents):
|
|
|
|
out.append("%s.%s" % parent.key)
|
2014-09-03 16:51:07 +08:00
|
|
|
if out:
|
2018-08-22 21:13:58 +08:00
|
|
|
return " … (%s)" % ", ".join(out)
|
2014-09-03 16:51:07 +08:00
|
|
|
return ""
|
|
|
|
|
2016-05-08 08:58:15 +08:00
|
|
|
for node in plan:
|
2014-09-03 16:51:07 +08:00
|
|
|
deps = ""
|
|
|
|
if self.verbosity >= 2:
|
2016-05-08 08:58:15 +08:00
|
|
|
deps = print_deps(node)
|
|
|
|
if node.key in loader.applied_migrations:
|
|
|
|
self.stdout.write("[X] %s.%s%s" % (node.key[0], node.key[1], deps))
|
2014-09-03 16:51:07 +08:00
|
|
|
else:
|
2016-05-08 08:58:15 +08:00
|
|
|
self.stdout.write("[ ] %s.%s%s" % (node.key[0], node.key[1], deps))
|