| 1 |
import sys |
| 2 |
|
| 3 |
from django.apps import apps |
| 4 |
from django.core.management.base import BaseCommand |
| 5 |
from django.db import DEFAULT_DB_ALIAS, connections |
| 6 |
from django.db.migrations.loader import MigrationLoader |
| 7 |
from django.db.migrations.recorder import MigrationRecorder |
| 8 |
|
| 9 |
|
| 10 |
class Command(BaseCommand): |
| 11 |
help = "Shows all available migrations for the current project" |
| 12 |
|
| 13 |
def add_arguments(self, parser): |
| 14 |
parser.add_argument( |
| 15 |
"app_label", |
| 16 |
nargs="*", |
| 17 |
help="App labels of applications to limit the output to.", |
| 18 |
) |
| 19 |
parser.add_argument( |
| 20 |
"--database", |
| 21 |
default=DEFAULT_DB_ALIAS, |
| 22 |
choices=tuple(connections), |
| 23 |
help=( |
| 24 |
"Nominates a database to show migrations for. Defaults to the " |
| 25 |
'"default" database.' |
| 26 |
), |
| 27 |
) |
| 28 |
|
| 29 |
formats = parser.add_mutually_exclusive_group() |
| 30 |
formats.add_argument( |
| 31 |
"--list", |
| 32 |
"-l", |
| 33 |
action="store_const", |
| 34 |
dest="format", |
| 35 |
const="list", |
| 36 |
help=( |
| 37 |
"Shows a list of all migrations and which are applied. " |
| 38 |
"With a verbosity level of 2 or above, the applied datetimes " |
| 39 |
"will be included." |
| 40 |
), |
| 41 |
) |
| 42 |
formats.add_argument( |
| 43 |
"--plan", |
| 44 |
"-p", |
| 45 |
action="store_const", |
| 46 |
dest="format", |
| 47 |
const="plan", |
| 48 |
help=( |
| 49 |
"Shows all migrations in the order they will be applied. With a " |
| 50 |
"verbosity level of 2 or above all direct migration dependencies and " |
| 51 |
"reverse dependencies (run_before) will be included." |
| 52 |
), |
| 53 |
) |
| 54 |
|
| 55 |
parser.set_defaults(format="list") |
| 56 |
|
| 57 |
def handle(self, *args, **options): |
| 58 |
self.verbosity = options["verbosity"] |
| 59 |
|
| 60 |
# Get the database we're operating from |
| 61 |
db = options["database"] |
| 62 |
connection = connections[db] |
| 63 |
|
| 64 |
if options["format"] == "plan": |
| 65 |
return self.show_plan(connection, options["app_label"]) |
| 66 |
else: |
| 67 |
return self.show_list(connection, options["app_label"]) |
| 68 |
|
| 69 |
def _validate_app_names(self, loader, app_names): |
| 70 |
has_bad_names = False |
| 71 |
for app_name in app_names: |
| 72 |
try: |
| 73 |
apps.get_app_config(app_name) |
| 74 |
except LookupError as err: |
| 75 |
self.stderr.write(str(err)) |
| 76 |
has_bad_names = True |
| 77 |
if has_bad_names: |
| 78 |
sys.exit(2) |
| 79 |
|
| 80 |
def show_list(self, connection, app_names=None): |
| 81 |
""" |
| 82 |
Show a list of all migrations on the system, or only those of |
| 83 |
some named apps. |
| 84 |
""" |
| 85 |
# Load migrations from disk/DB |
| 86 |
loader = MigrationLoader(connection, ignore_no_migrations=True) |
| 87 |
recorder = MigrationRecorder(connection) |
| 88 |
recorded_migrations = recorder.applied_migrations() |
| 89 |
graph = loader.graph |
| 90 |
# If we were passed a list of apps, validate it |
| 91 |
if app_names: |
| 92 |
self._validate_app_names(loader, app_names) |
| 93 |
# Otherwise, show all apps in alphabetic order |
| 94 |
else: |
| 95 |
app_names = sorted(loader.migrated_apps) |
| 96 |
# For each app, print its migrations in order from oldest (roots) to |
| 97 |
# newest (leaves). |
| 98 |
for app_name in app_names: |
| 99 |
self.stdout.write(app_name, self.style.MIGRATE_LABEL) |
| 100 |
shown = set() |
| 101 |
for node in graph.leaf_nodes(app_name): |
| 102 |
for plan_node in graph.forwards_plan(node): |
| 103 |
if plan_node not in shown and plan_node[0] == app_name: |
| 104 |
# Give it a nice title if it's a squashed one |
| 105 |
title = plan_node[1] |
| 106 |
if graph.nodes[plan_node].replaces: |
| 107 |
title += " (%s squashed migrations)" % len( |
| 108 |
graph.nodes[plan_node].replaces |
| 109 |
) |
| 110 |
applied_migration = loader.applied_migrations.get(plan_node) |
| 111 |
# Mark it as applied/unapplied |
| 112 |
if applied_migration: |
| 113 |
if plan_node in recorded_migrations: |
| 114 |
output = " [X] %s" % title |
| 115 |
else: |
| 116 |
title += " Run 'manage.py migrate' to finish recording." |
| 117 |
output = " [-] %s" % title |
| 118 |
if self.verbosity >= 2 and hasattr( |
| 119 |
applied_migration, "applied" |
| 120 |
): |
| 121 |
output += ( |
| 122 |
" (applied at %s)" |
| 123 |
% applied_migration.applied.strftime( |
| 124 |
"%Y-%m-%d %H:%M:%S" |
| 125 |
) |
| 126 |
) |
| 127 |
self.stdout.write(output) |
| 128 |
else: |
| 129 |
self.stdout.write(" [ ] %s" % title) |
| 130 |
shown.add(plan_node) |
| 131 |
# If we didn't print anything, then a small message |
| 132 |
if not shown: |
| 133 |
self.stdout.write(" (no migrations)", self.style.ERROR) |
| 134 |
|
| 135 |
def show_plan(self, connection, app_names=None): |
| 136 |
""" |
| 137 |
Show all known migrations (or only those of the specified app_names) |
| 138 |
in the order they will be applied. |
| 139 |
""" |
| 140 |
# Load migrations from disk/DB |
| 141 |
loader = MigrationLoader(connection) |
| 142 |
graph = loader.graph |
| 143 |
if app_names: |
| 144 |
self._validate_app_names(loader, app_names) |
| 145 |
targets = [key for key in graph.leaf_nodes() if key[0] in app_names] |
| 146 |
else: |
| 147 |
targets = graph.leaf_nodes() |
| 148 |
plan = [] |
| 149 |
seen = set() |
| 150 |
|
| 151 |
# Generate the plan |
| 152 |
for target in targets: |
| 153 |
for migration in graph.forwards_plan(target): |
| 154 |
if migration not in seen: |
| 155 |
node = graph.node_map[migration] |
| 156 |
plan.append(node) |
| 157 |
seen.add(migration) |
| 158 |
|
| 159 |
# Output |
| 160 |
def print_deps(node): |
| 161 |
out = [] |
| 162 |
for parent in sorted(node.parents): |
| 163 |
out.append("%s.%s" % parent.key) |
| 164 |
if out: |
| 165 |
return " ... (%s)" % ", ".join(out) |
| 166 |
return "" |
| 167 |
|
| 168 |
for node in plan: |
| 169 |
deps = "" |
| 170 |
if self.verbosity >= 2: |
| 171 |
deps = print_deps(node) |
| 172 |
if node.key in loader.applied_migrations: |
| 173 |
self.stdout.write("[X] %s.%s%s" % (node.key[0], node.key[1], deps)) |
| 174 |
else: |
| 175 |
self.stdout.write("[ ] %s.%s%s" % (node.key[0], node.key[1], deps)) |
| 176 |
if not plan: |
| 177 |
self.stdout.write("(no migrations)", self.style.ERROR) |