| 1 |
import sys |
| 2 |
import time |
| 3 |
from importlib import import_module |
| 4 |
|
| 5 |
from django.apps import apps |
| 6 |
from django.core.management.base import BaseCommand, CommandError, no_translations |
| 7 |
from django.core.management.sql import emit_post_migrate_signal, emit_pre_migrate_signal |
| 8 |
from django.db import DEFAULT_DB_ALIAS, connections, router |
| 9 |
from django.db.migrations.autodetector import MigrationAutodetector |
| 10 |
from django.db.migrations.executor import MigrationExecutor |
| 11 |
from django.db.migrations.loader import AmbiguityError |
| 12 |
from django.db.migrations.state import ModelState, ProjectState |
| 13 |
from django.utils.module_loading import module_has_submodule |
| 14 |
from django.utils.text import Truncator |
| 15 |
|
| 16 |
|
| 17 |
class Command(BaseCommand): |
| 18 |
autodetector = MigrationAutodetector |
| 19 |
help = ( |
| 20 |
"Updates database schema. Manages both apps with migrations and those without." |
| 21 |
) |
| 22 |
|
| 23 |
def add_arguments(self, parser): |
| 24 |
parser.add_argument( |
| 25 |
"app_label", |
| 26 |
nargs="?", |
| 27 |
help="App label of an application to synchronize the state.", |
| 28 |
) |
| 29 |
parser.add_argument( |
| 30 |
"migration_name", |
| 31 |
nargs="?", |
| 32 |
help="Database state will be brought to the state after that " |
| 33 |
'migration. Use the name "zero" to unapply all migrations.', |
| 34 |
) |
| 35 |
parser.add_argument( |
| 36 |
"--noinput", |
| 37 |
"--no-input", |
| 38 |
action="store_false", |
| 39 |
dest="interactive", |
| 40 |
help="Tells Django to NOT prompt the user for input of any kind.", |
| 41 |
) |
| 42 |
parser.add_argument( |
| 43 |
"--database", |
| 44 |
default=DEFAULT_DB_ALIAS, |
| 45 |
choices=tuple(connections), |
| 46 |
help=( |
| 47 |
'Nominates a database to synchronize. Defaults to the "default" ' |
| 48 |
"database." |
| 49 |
), |
| 50 |
) |
| 51 |
parser.add_argument( |
| 52 |
"--fake", |
| 53 |
action="store_true", |
| 54 |
help="Mark migrations as run without actually running them.", |
| 55 |
) |
| 56 |
parser.add_argument( |
| 57 |
"--fake-initial", |
| 58 |
action="store_true", |
| 59 |
help=( |
| 60 |
"Detect if tables already exist and fake-apply initial migrations if " |
| 61 |
"so. Make sure that the current database schema matches your initial " |
| 62 |
"migration before using this flag. Django will only check for an " |
| 63 |
"existing table name." |
| 64 |
), |
| 65 |
) |
| 66 |
parser.add_argument( |
| 67 |
"--plan", |
| 68 |
action="store_true", |
| 69 |
help="Shows a list of the migration actions that will be performed.", |
| 70 |
) |
| 71 |
parser.add_argument( |
| 72 |
"--run-syncdb", |
| 73 |
action="store_true", |
| 74 |
help="Creates tables for apps without migrations.", |
| 75 |
) |
| 76 |
parser.add_argument( |
| 77 |
"--check", |
| 78 |
action="store_true", |
| 79 |
dest="check_unapplied", |
| 80 |
help=( |
| 81 |
"Exits with a non-zero status if unapplied migrations exist and does " |
| 82 |
"not actually apply migrations." |
| 83 |
), |
| 84 |
) |
| 85 |
parser.add_argument( |
| 86 |
"--prune", |
| 87 |
action="store_true", |
| 88 |
dest="prune", |
| 89 |
help="Delete nonexistent migrations from the django_migrations table.", |
| 90 |
) |
| 91 |
|
| 92 |
def get_check_kwargs(self, options): |
| 93 |
kwargs = super().get_check_kwargs(options) |
| 94 |
return {**kwargs, "databases": [options["database"]]} |
| 95 |
|
| 96 |
@no_translations |
| 97 |
def handle(self, *args, **options): |
| 98 |
database = options["database"] |
| 99 |
self.verbosity = options["verbosity"] |
| 100 |
self.interactive = options["interactive"] |
| 101 |
|
| 102 |
# Import the 'management' module within each installed app, to register |
| 103 |
# dispatcher events. |
| 104 |
for app_config in apps.get_app_configs(): |
| 105 |
if module_has_submodule(app_config.module, "management"): |
| 106 |
import_module(".management", app_config.name) |
| 107 |
|
| 108 |
# Get the database we're operating from |
| 109 |
connection = connections[database] |
| 110 |
|
| 111 |
# Hook for backends needing any database preparation |
| 112 |
connection.prepare_database() |
| 113 |
# Work out which apps have migrations and which do not |
| 114 |
executor = MigrationExecutor(connection, self.migration_progress_callback) |
| 115 |
|
| 116 |
# Raise an error if any migrations are applied before their dependencies. |
| 117 |
executor.loader.check_consistent_history(connection) |
| 118 |
|
| 119 |
# Before anything else, see if there's conflicting apps and drop out |
| 120 |
# hard if there are any |
| 121 |
conflicts = executor.loader.detect_conflicts() |
| 122 |
if conflicts: |
| 123 |
name_str = "; ".join( |
| 124 |
"%s in %s" % (", ".join(names), app) for app, names in conflicts.items() |
| 125 |
) |
| 126 |
raise CommandError( |
| 127 |
"Conflicting migrations detected; multiple leaf nodes in the " |
| 128 |
"migration graph: (%s).\nTo fix them run " |
| 129 |
"'python manage.py makemigrations --merge'" % name_str |
| 130 |
) |
| 131 |
|
| 132 |
# If they supplied command line arguments, work out what they mean. |
| 133 |
run_syncdb = options["run_syncdb"] |
| 134 |
target_app_labels_only = True |
| 135 |
if options["app_label"]: |
| 136 |
# Validate app_label. |
| 137 |
app_label = options["app_label"] |
| 138 |
try: |
| 139 |
apps.get_app_config(app_label) |
| 140 |
except LookupError as err: |
| 141 |
raise CommandError(str(err)) |
| 142 |
if run_syncdb: |
| 143 |
if app_label in executor.loader.migrated_apps: |
| 144 |
raise CommandError( |
| 145 |
"Can't use run_syncdb with app '%s' as it has migrations." |
| 146 |
% app_label |
| 147 |
) |
| 148 |
elif app_label not in executor.loader.migrated_apps: |
| 149 |
raise CommandError("App '%s' does not have migrations." % app_label) |
| 150 |
|
| 151 |
if options["app_label"] and options["migration_name"]: |
| 152 |
migration_name = options["migration_name"] |
| 153 |
if migration_name == "zero": |
| 154 |
targets = [(app_label, None)] |
| 155 |
else: |
| 156 |
try: |
| 157 |
migration = executor.loader.get_migration_by_prefix( |
| 158 |
app_label, migration_name |
| 159 |
) |
| 160 |
except AmbiguityError: |
| 161 |
raise CommandError( |
| 162 |
"More than one migration matches '%s' in app '%s'. " |
| 163 |
"Please be more specific." % (migration_name, app_label) |
| 164 |
) |
| 165 |
except KeyError: |
| 166 |
raise CommandError( |
| 167 |
"Cannot find a migration matching '%s' from app '%s'." |
| 168 |
% (migration_name, app_label) |
| 169 |
) |
| 170 |
target = (app_label, migration.name) |
| 171 |
# Partially applied squashed migrations are not included in the |
| 172 |
# graph, use the last replacement instead. |
| 173 |
if ( |
| 174 |
target not in executor.loader.graph.nodes |
| 175 |
and target in executor.loader.replacements |
| 176 |
): |
| 177 |
incomplete_migration = executor.loader.replacements[target] |
| 178 |
target = incomplete_migration.replaces[-1] |
| 179 |
targets = [target] |
| 180 |
target_app_labels_only = False |
| 181 |
elif options["app_label"]: |
| 182 |
targets = [ |
| 183 |
key for key in executor.loader.graph.leaf_nodes() if key[0] == app_label |
| 184 |
] |
| 185 |
else: |
| 186 |
targets = executor.loader.graph.leaf_nodes() |
| 187 |
|
| 188 |
if options["prune"]: |
| 189 |
if not options["app_label"]: |
| 190 |
raise CommandError( |
| 191 |
"Migrations can be pruned only when an app is specified." |
| 192 |
) |
| 193 |
if self.verbosity > 0: |
| 194 |
self.stdout.write("Pruning migrations:", self.style.MIGRATE_HEADING) |
| 195 |
to_prune = sorted( |
| 196 |
migration |
| 197 |
for migration in set(executor.loader.applied_migrations) |
| 198 |
- set(executor.loader.disk_migrations) |
| 199 |
if migration[0] == app_label |
| 200 |
) |
| 201 |
squashed_migrations_with_deleted_replaced_migrations = [ |
| 202 |
migration_key |
| 203 |
for migration_key, migration_obj in executor.loader.replacements.items() |
| 204 |
if any(replaced in to_prune for replaced in migration_obj.replaces) |
| 205 |
] |
| 206 |
if squashed_migrations_with_deleted_replaced_migrations: |
| 207 |
self.stdout.write( |
| 208 |
self.style.NOTICE( |
| 209 |
" Cannot use --prune because the following squashed " |
| 210 |
"migrations have their 'replaces' attributes and may not " |
| 211 |
"be recorded as applied:" |
| 212 |
) |
| 213 |
) |
| 214 |
for migration in squashed_migrations_with_deleted_replaced_migrations: |
| 215 |
app, name = migration |
| 216 |
self.stdout.write(f" {app}.{name}") |
| 217 |
self.stdout.write( |
| 218 |
self.style.NOTICE( |
| 219 |
" Re-run 'manage.py migrate' if they are not marked as " |
| 220 |
"applied, and remove 'replaces' attributes in their " |
| 221 |
"Migration classes." |
| 222 |
) |
| 223 |
) |
| 224 |
else: |
| 225 |
if to_prune: |
| 226 |
for migration in to_prune: |
| 227 |
app, name = migration |
| 228 |
if self.verbosity > 0: |
| 229 |
self.stdout.write( |
| 230 |
self.style.MIGRATE_LABEL(f" Pruning {app}.{name}"), |
| 231 |
ending="", |
| 232 |
) |
| 233 |
executor.recorder.record_unapplied(app, name) |
| 234 |
if self.verbosity > 0: |
| 235 |
self.stdout.write(self.style.SUCCESS(" OK")) |
| 236 |
elif self.verbosity > 0: |
| 237 |
self.stdout.write(" No migrations to prune.") |
| 238 |
|
| 239 |
plan = executor.migration_plan(targets) |
| 240 |
|
| 241 |
if options["plan"]: |
| 242 |
self.stdout.write("Planned operations:", self.style.MIGRATE_LABEL) |
| 243 |
if not plan: |
| 244 |
self.stdout.write(" No planned migration operations.") |
| 245 |
else: |
| 246 |
for migration, backwards in plan: |
| 247 |
self.stdout.write(str(migration), self.style.MIGRATE_HEADING) |
| 248 |
for operation in migration.operations: |
| 249 |
message, is_error = self.describe_operation( |
| 250 |
operation, backwards |
| 251 |
) |
| 252 |
style = self.style.WARNING if is_error else None |
| 253 |
self.stdout.write(" " + message, style) |
| 254 |
if options["check_unapplied"]: |
| 255 |
sys.exit(1) |
| 256 |
return |
| 257 |
if options["check_unapplied"]: |
| 258 |
if plan: |
| 259 |
sys.exit(1) |
| 260 |
return |
| 261 |
if options["prune"]: |
| 262 |
return |
| 263 |
|
| 264 |
# At this point, ignore run_syncdb if there aren't any apps to sync. |
| 265 |
run_syncdb = options["run_syncdb"] and executor.loader.unmigrated_apps |
| 266 |
# Print some useful info |
| 267 |
if self.verbosity >= 1: |
| 268 |
self.stdout.write(self.style.MIGRATE_HEADING("Operations to perform:")) |
| 269 |
if run_syncdb: |
| 270 |
if options["app_label"]: |
| 271 |
self.stdout.write( |
| 272 |
self.style.MIGRATE_LABEL( |
| 273 |
" Synchronize unmigrated app: %s" % app_label |
| 274 |
) |
| 275 |
) |
| 276 |
else: |
| 277 |
self.stdout.write( |
| 278 |
self.style.MIGRATE_LABEL(" Synchronize unmigrated apps: ") |
| 279 |
+ (", ".join(sorted(executor.loader.unmigrated_apps))) |
| 280 |
) |
| 281 |
if target_app_labels_only: |
| 282 |
self.stdout.write( |
| 283 |
self.style.MIGRATE_LABEL(" Apply all migrations: ") |
| 284 |
+ (", ".join(sorted({a for a, n in targets})) or "(none)") |
| 285 |
) |
| 286 |
else: |
| 287 |
if targets[0][1] is None: |
| 288 |
self.stdout.write( |
| 289 |
self.style.MIGRATE_LABEL(" Unapply all migrations: ") |
| 290 |
+ str(targets[0][0]) |
| 291 |
) |
| 292 |
else: |
| 293 |
self.stdout.write( |
| 294 |
self.style.MIGRATE_LABEL(" Target specific migration: ") |
| 295 |
+ "%s, from %s" % (targets[0][1], targets[0][0]) |
| 296 |
) |
| 297 |
|
| 298 |
pre_migrate_state = executor._create_project_state(with_applied_migrations=True) |
| 299 |
pre_migrate_apps = pre_migrate_state.apps |
| 300 |
emit_pre_migrate_signal( |
| 301 |
self.verbosity, |
| 302 |
self.interactive, |
| 303 |
connection.alias, |
| 304 |
stdout=self.stdout, |
| 305 |
apps=pre_migrate_apps, |
| 306 |
plan=plan, |
| 307 |
) |
| 308 |
|
| 309 |
# Run the syncdb phase. |
| 310 |
if run_syncdb: |
| 311 |
if self.verbosity >= 1: |
| 312 |
self.stdout.write( |
| 313 |
self.style.MIGRATE_HEADING("Synchronizing apps without migrations:") |
| 314 |
) |
| 315 |
if options["app_label"]: |
| 316 |
self.sync_apps(connection, [app_label]) |
| 317 |
else: |
| 318 |
self.sync_apps(connection, executor.loader.unmigrated_apps) |
| 319 |
|
| 320 |
# Migrate! |
| 321 |
if self.verbosity >= 1: |
| 322 |
self.stdout.write(self.style.MIGRATE_HEADING("Running migrations:")) |
| 323 |
if not plan: |
| 324 |
if self.verbosity >= 1: |
| 325 |
self.stdout.write(" No migrations to apply.") |
| 326 |
# If there's changes that aren't in migrations yet, tell them |
| 327 |
# how to fix it. |
| 328 |
autodetector = self.autodetector( |
| 329 |
executor.loader.project_state(), |
| 330 |
ProjectState.from_apps(apps), |
| 331 |
) |
| 332 |
changes = autodetector.changes(graph=executor.loader.graph) |
| 333 |
if changes: |
| 334 |
self.stdout.write( |
| 335 |
self.style.NOTICE( |
| 336 |
" Your models in app(s): %s have changes that are not " |
| 337 |
"yet reflected in a migration, and so won't be " |
| 338 |
"applied." % ", ".join(repr(app) for app in sorted(changes)) |
| 339 |
) |
| 340 |
) |
| 341 |
self.stdout.write( |
| 342 |
self.style.NOTICE( |
| 343 |
" Run 'manage.py makemigrations' to make new " |
| 344 |
"migrations, and then re-run 'manage.py migrate' to " |
| 345 |
"apply them." |
| 346 |
) |
| 347 |
) |
| 348 |
fake = False |
| 349 |
fake_initial = False |
| 350 |
else: |
| 351 |
fake = options["fake"] |
| 352 |
fake_initial = options["fake_initial"] |
| 353 |
post_migrate_state = executor.migrate( |
| 354 |
targets, |
| 355 |
plan=plan, |
| 356 |
state=pre_migrate_state.clone(), |
| 357 |
fake=fake, |
| 358 |
fake_initial=fake_initial, |
| 359 |
) |
| 360 |
# post_migrate signals have access to all models. Ensure that all models |
| 361 |
# are reloaded in case any are delayed. |
| 362 |
post_migrate_state.clear_delayed_apps_cache() |
| 363 |
post_migrate_apps = post_migrate_state.apps |
| 364 |
|
| 365 |
# Re-render models of real apps to include relationships now that |
| 366 |
# we've got a final state. This wouldn't be necessary if real apps |
| 367 |
# models were rendered with relationships in the first place. |
| 368 |
with post_migrate_apps.bulk_update(): |
| 369 |
model_keys = [] |
| 370 |
for model_state in post_migrate_apps.real_models: |
| 371 |
model_key = model_state.app_label, model_state.name_lower |
| 372 |
model_keys.append(model_key) |
| 373 |
post_migrate_apps.unregister_model(*model_key) |
| 374 |
post_migrate_apps.render_multiple( |
| 375 |
[ModelState.from_model(apps.get_model(*model)) for model in model_keys] |
| 376 |
) |
| 377 |
|
| 378 |
# Send the post_migrate signal, so individual apps can do whatever they need |
| 379 |
# to do at this point. |
| 380 |
emit_post_migrate_signal( |
| 381 |
self.verbosity, |
| 382 |
self.interactive, |
| 383 |
connection.alias, |
| 384 |
stdout=self.stdout, |
| 385 |
apps=post_migrate_apps, |
| 386 |
plan=plan, |
| 387 |
) |
| 388 |
|
| 389 |
def migration_progress_callback(self, action, migration=None, fake=False): |
| 390 |
if self.verbosity >= 1: |
| 391 |
compute_time = self.verbosity > 1 |
| 392 |
if action == "apply_start": |
| 393 |
if compute_time: |
| 394 |
self.start = time.monotonic() |
| 395 |
self.stdout.write(" Applying %s..." % migration, ending="") |
| 396 |
self.stdout.flush() |
| 397 |
elif action == "apply_success": |
| 398 |
elapsed = ( |
| 399 |
" (%.3fs)" % (time.monotonic() - self.start) if compute_time else "" |
| 400 |
) |
| 401 |
if fake: |
| 402 |
self.stdout.write(self.style.SUCCESS(" FAKED" + elapsed)) |
| 403 |
else: |
| 404 |
self.stdout.write(self.style.SUCCESS(" OK" + elapsed)) |
| 405 |
elif action == "unapply_start": |
| 406 |
if compute_time: |
| 407 |
self.start = time.monotonic() |
| 408 |
self.stdout.write(" Unapplying %s..." % migration, ending="") |
| 409 |
self.stdout.flush() |
| 410 |
elif action == "unapply_success": |
| 411 |
elapsed = ( |
| 412 |
" (%.3fs)" % (time.monotonic() - self.start) if compute_time else "" |
| 413 |
) |
| 414 |
if fake: |
| 415 |
self.stdout.write(self.style.SUCCESS(" FAKED" + elapsed)) |
| 416 |
else: |
| 417 |
self.stdout.write(self.style.SUCCESS(" OK" + elapsed)) |
| 418 |
elif action == "render_start": |
| 419 |
if compute_time: |
| 420 |
self.start = time.monotonic() |
| 421 |
self.stdout.write(" Rendering model states...", ending="") |
| 422 |
self.stdout.flush() |
| 423 |
elif action == "render_success": |
| 424 |
elapsed = ( |
| 425 |
" (%.3fs)" % (time.monotonic() - self.start) if compute_time else "" |
| 426 |
) |
| 427 |
self.stdout.write(self.style.SUCCESS(" DONE" + elapsed)) |
| 428 |
|
| 429 |
def sync_apps(self, connection, app_labels): |
| 430 |
"""Run the old syncdb-style operation on a list of app_labels.""" |
| 431 |
with connection.cursor() as cursor: |
| 432 |
tables = connection.introspection.table_names(cursor) |
| 433 |
|
| 434 |
# Build the manifest of apps and models that are to be synchronized. |
| 435 |
all_models = [ |
| 436 |
( |
| 437 |
app_config.label, |
| 438 |
router.get_migratable_models( |
| 439 |
app_config, connection.alias, include_auto_created=False |
| 440 |
), |
| 441 |
) |
| 442 |
for app_config in apps.get_app_configs() |
| 443 |
if app_config.models_module is not None and app_config.label in app_labels |
| 444 |
] |
| 445 |
|
| 446 |
def model_installed(model): |
| 447 |
opts = model._meta |
| 448 |
converter = connection.introspection.identifier_converter |
| 449 |
return not ( |
| 450 |
(converter(opts.db_table) in tables) |
| 451 |
or ( |
| 452 |
opts.auto_created |
| 453 |
and converter(opts.auto_created._meta.db_table) in tables |
| 454 |
) |
| 455 |
) |
| 456 |
|
| 457 |
manifest = { |
| 458 |
app_name: list(filter(model_installed, model_list)) |
| 459 |
for app_name, model_list in all_models |
| 460 |
} |
| 461 |
|
| 462 |
# Create the tables for each model |
| 463 |
if self.verbosity >= 1: |
| 464 |
self.stdout.write(" Creating tables...") |
| 465 |
with connection.schema_editor() as editor: |
| 466 |
for app_name, model_list in manifest.items(): |
| 467 |
for model in model_list: |
| 468 |
# Never install unmanaged models, etc. |
| 469 |
if not model._meta.can_migrate(connection): |
| 470 |
continue |
| 471 |
if self.verbosity >= 3: |
| 472 |
self.stdout.write( |
| 473 |
" Processing %s.%s model" |
| 474 |
% (app_name, model._meta.object_name) |
| 475 |
) |
| 476 |
if self.verbosity >= 1: |
| 477 |
self.stdout.write( |
| 478 |
" Creating table %s" % model._meta.db_table |
| 479 |
) |
| 480 |
editor.create_model(model) |
| 481 |
|
| 482 |
# Deferred SQL is executed when exiting the editor's context. |
| 483 |
if self.verbosity >= 1: |
| 484 |
self.stdout.write(" Running deferred SQL...") |
| 485 |
|
| 486 |
@staticmethod |
| 487 |
def describe_operation(operation, backwards): |
| 488 |
"""Return a string that describes a migration operation for --plan.""" |
| 489 |
prefix = "" |
| 490 |
is_error = False |
| 491 |
if hasattr(operation, "code"): |
| 492 |
code = operation.reverse_code if backwards else operation.code |
| 493 |
action = (code.__doc__ or "") if code else None |
| 494 |
elif hasattr(operation, "sql"): |
| 495 |
action = operation.reverse_sql if backwards else operation.sql |
| 496 |
else: |
| 497 |
action = "" |
| 498 |
if backwards: |
| 499 |
prefix = "Undo " |
| 500 |
if action is not None: |
| 501 |
action = str(action).replace("\n", "") |
| 502 |
elif backwards: |
| 503 |
action = "IRREVERSIBLE" |
| 504 |
is_error = True |
| 505 |
if action: |
| 506 |
action = " -> " + action |
| 507 |
truncated = Truncator(action) |
| 508 |
return prefix + operation.describe() + truncated.chars(40), is_error |