| 1 |
import os |
| 2 |
import sys |
| 3 |
import warnings |
| 4 |
from itertools import takewhile |
| 5 |
|
| 6 |
from django.apps import apps |
| 7 |
from django.conf import settings |
| 8 |
from django.core.management.base import BaseCommand, CommandError, no_translations |
| 9 |
from django.core.management.utils import run_formatters |
| 10 |
from django.db import DEFAULT_DB_ALIAS, OperationalError, connections, router |
| 11 |
from django.db.migrations import Migration |
| 12 |
from django.db.migrations.autodetector import MigrationAutodetector |
| 13 |
from django.db.migrations.loader import MigrationLoader |
| 14 |
from django.db.migrations.migration import SwappableTuple |
| 15 |
from django.db.migrations.optimizer import MigrationOptimizer |
| 16 |
from django.db.migrations.questioner import ( |
| 17 |
InteractiveMigrationQuestioner, |
| 18 |
MigrationQuestioner, |
| 19 |
NonInteractiveMigrationQuestioner, |
| 20 |
) |
| 21 |
from django.db.migrations.state import ProjectState |
| 22 |
from django.db.migrations.utils import get_migration_name_timestamp |
| 23 |
from django.db.migrations.writer import MigrationWriter |
| 24 |
|
| 25 |
|
| 26 |
class Command(BaseCommand): |
| 27 |
autodetector = MigrationAutodetector |
| 28 |
help = "Creates new migration(s) for apps." |
| 29 |
|
| 30 |
def add_arguments(self, parser): |
| 31 |
parser.add_argument( |
| 32 |
"args", |
| 33 |
metavar="app_label", |
| 34 |
nargs="*", |
| 35 |
help="Specify the app label(s) to create migrations for.", |
| 36 |
) |
| 37 |
parser.add_argument( |
| 38 |
"--dry-run", |
| 39 |
action="store_true", |
| 40 |
help="Just show what migrations would be made; don't actually write them.", |
| 41 |
) |
| 42 |
parser.add_argument( |
| 43 |
"--merge", |
| 44 |
action="store_true", |
| 45 |
help="Enable fixing of migration conflicts.", |
| 46 |
) |
| 47 |
parser.add_argument( |
| 48 |
"--empty", |
| 49 |
action="store_true", |
| 50 |
help="Create an empty migration.", |
| 51 |
) |
| 52 |
parser.add_argument( |
| 53 |
"--noinput", |
| 54 |
"--no-input", |
| 55 |
action="store_false", |
| 56 |
dest="interactive", |
| 57 |
help="Tells Django to NOT prompt the user for input of any kind.", |
| 58 |
) |
| 59 |
parser.add_argument( |
| 60 |
"-n", |
| 61 |
"--name", |
| 62 |
help="Use this name for migration file(s).", |
| 63 |
) |
| 64 |
parser.add_argument( |
| 65 |
"--no-header", |
| 66 |
action="store_false", |
| 67 |
dest="include_header", |
| 68 |
help="Do not add header comments to new migration file(s).", |
| 69 |
) |
| 70 |
parser.add_argument( |
| 71 |
"--check", |
| 72 |
action="store_true", |
| 73 |
dest="check_changes", |
| 74 |
help=( |
| 75 |
"Exit with a non-zero status if model changes are missing migrations " |
| 76 |
"and don't actually write them. Implies --dry-run." |
| 77 |
), |
| 78 |
) |
| 79 |
parser.add_argument( |
| 80 |
"--scriptable", |
| 81 |
action="store_true", |
| 82 |
dest="scriptable", |
| 83 |
help=( |
| 84 |
"Divert log output and input prompts to stderr, writing only " |
| 85 |
"paths of generated migration files to stdout." |
| 86 |
), |
| 87 |
) |
| 88 |
parser.add_argument( |
| 89 |
"--update", |
| 90 |
action="store_true", |
| 91 |
dest="update", |
| 92 |
help=( |
| 93 |
"Merge model changes into the latest migration and optimize the " |
| 94 |
"resulting operations." |
| 95 |
), |
| 96 |
) |
| 97 |
|
| 98 |
@property |
| 99 |
def log_output(self): |
| 100 |
return self.stderr if self.scriptable else self.stdout |
| 101 |
|
| 102 |
def log(self, msg): |
| 103 |
self.log_output.write(msg) |
| 104 |
|
| 105 |
@no_translations |
| 106 |
def handle(self, *app_labels, **options): |
| 107 |
self.written_files = [] |
| 108 |
self.verbosity = options["verbosity"] |
| 109 |
self.interactive = options["interactive"] |
| 110 |
self.dry_run = options["dry_run"] |
| 111 |
self.merge = options["merge"] |
| 112 |
self.empty = options["empty"] |
| 113 |
self.migration_name = options["name"] |
| 114 |
if self.migration_name and not self.migration_name.isidentifier(): |
| 115 |
raise CommandError("The migration name must be a valid Python identifier.") |
| 116 |
self.include_header = options["include_header"] |
| 117 |
check_changes = options["check_changes"] |
| 118 |
if check_changes: |
| 119 |
self.dry_run = True |
| 120 |
self.scriptable = options["scriptable"] |
| 121 |
self.update = options["update"] |
| 122 |
# If logs and prompts are diverted to stderr, remove the ERROR style. |
| 123 |
if self.scriptable: |
| 124 |
self.stderr.style_func = None |
| 125 |
|
| 126 |
# Make sure the app they asked for exists |
| 127 |
app_labels = set(app_labels) |
| 128 |
has_bad_labels = False |
| 129 |
for app_label in app_labels: |
| 130 |
try: |
| 131 |
apps.get_app_config(app_label) |
| 132 |
except LookupError as err: |
| 133 |
self.stderr.write(str(err)) |
| 134 |
has_bad_labels = True |
| 135 |
if has_bad_labels: |
| 136 |
sys.exit(2) |
| 137 |
|
| 138 |
# Load the current graph state. Pass in None for the connection so |
| 139 |
# the loader doesn't try to resolve replaced migrations from DB. |
| 140 |
loader = MigrationLoader(None, ignore_no_migrations=True) |
| 141 |
|
| 142 |
# Raise an error if any migrations are applied before their dependencies. |
| 143 |
consistency_check_labels = {config.label for config in apps.get_app_configs()} |
| 144 |
# Non-default databases are only checked if database routers used. |
| 145 |
aliases_to_check = ( |
| 146 |
connections if settings.DATABASE_ROUTERS else [DEFAULT_DB_ALIAS] |
| 147 |
) |
| 148 |
for alias in sorted(aliases_to_check): |
| 149 |
connection = connections[alias] |
| 150 |
if connection.settings_dict["ENGINE"] != "django.db.backends.dummy" and any( |
| 151 |
# At least one model must be migrated to the database. |
| 152 |
router.allow_migrate( |
| 153 |
connection.alias, app_label, model_name=model._meta.object_name |
| 154 |
) |
| 155 |
for app_label in consistency_check_labels |
| 156 |
for model in apps.get_app_config(app_label).get_models() |
| 157 |
): |
| 158 |
try: |
| 159 |
loader.check_consistent_history(connection) |
| 160 |
except OperationalError as error: |
| 161 |
warnings.warn( |
| 162 |
"Got an error checking a consistent migration history " |
| 163 |
"performed for database connection '%s': %s" % (alias, error), |
| 164 |
RuntimeWarning, |
| 165 |
) |
| 166 |
# Before anything else, see if there's conflicting apps and drop out |
| 167 |
# hard if there are any and they don't want to merge |
| 168 |
conflicts = loader.detect_conflicts() |
| 169 |
|
| 170 |
# If app_labels is specified, filter out conflicting migrations for |
| 171 |
# unspecified apps. |
| 172 |
if app_labels: |
| 173 |
conflicts = { |
| 174 |
app_label: conflict |
| 175 |
for app_label, conflict in conflicts.items() |
| 176 |
if app_label in app_labels |
| 177 |
} |
| 178 |
|
| 179 |
if conflicts and not self.merge: |
| 180 |
name_str = "; ".join( |
| 181 |
"%s in %s" % (", ".join(names), app) for app, names in conflicts.items() |
| 182 |
) |
| 183 |
raise CommandError( |
| 184 |
"Conflicting migrations detected; multiple leaf nodes in the " |
| 185 |
"migration graph: (%s).\nTo fix them run " |
| 186 |
"'python manage.py makemigrations --merge'" % name_str |
| 187 |
) |
| 188 |
|
| 189 |
# If they want to merge and there's nothing to merge, then politely exit |
| 190 |
if self.merge and not conflicts: |
| 191 |
self.log("No conflicts detected to merge.") |
| 192 |
return |
| 193 |
|
| 194 |
# If they want to merge and there is something to merge, then |
| 195 |
# divert into the merge code |
| 196 |
if self.merge and conflicts: |
| 197 |
return self.handle_merge(loader, conflicts) |
| 198 |
|
| 199 |
if self.interactive: |
| 200 |
questioner = InteractiveMigrationQuestioner( |
| 201 |
specified_apps=app_labels, |
| 202 |
dry_run=self.dry_run, |
| 203 |
prompt_output=self.log_output, |
| 204 |
) |
| 205 |
else: |
| 206 |
questioner = NonInteractiveMigrationQuestioner( |
| 207 |
specified_apps=app_labels, |
| 208 |
dry_run=self.dry_run, |
| 209 |
verbosity=self.verbosity, |
| 210 |
log=self.log, |
| 211 |
) |
| 212 |
# Set up autodetector |
| 213 |
autodetector = self.autodetector( |
| 214 |
loader.project_state(), |
| 215 |
ProjectState.from_apps(apps), |
| 216 |
questioner, |
| 217 |
) |
| 218 |
|
| 219 |
# If they want to make an empty migration, make one for each app |
| 220 |
if self.empty: |
| 221 |
if not app_labels: |
| 222 |
raise CommandError( |
| 223 |
"You must supply at least one app label when using --empty." |
| 224 |
) |
| 225 |
# Make a fake changes() result we can pass to arrange_for_graph |
| 226 |
changes = {app: [Migration("custom", app)] for app in app_labels} |
| 227 |
changes = autodetector.arrange_for_graph( |
| 228 |
changes=changes, |
| 229 |
graph=loader.graph, |
| 230 |
migration_name=self.migration_name, |
| 231 |
) |
| 232 |
self.write_migration_files(changes) |
| 233 |
return |
| 234 |
|
| 235 |
# Detect changes |
| 236 |
changes = autodetector.changes( |
| 237 |
graph=loader.graph, |
| 238 |
trim_to_apps=app_labels or None, |
| 239 |
convert_apps=app_labels or None, |
| 240 |
migration_name=self.migration_name, |
| 241 |
) |
| 242 |
|
| 243 |
if not changes: |
| 244 |
# No changes? Tell them. |
| 245 |
if self.verbosity >= 1: |
| 246 |
if app_labels: |
| 247 |
if len(app_labels) == 1: |
| 248 |
self.log("No changes detected in app '%s'" % app_labels.pop()) |
| 249 |
else: |
| 250 |
self.log( |
| 251 |
"No changes detected in apps '%s'" |
| 252 |
% ("', '".join(app_labels)) |
| 253 |
) |
| 254 |
else: |
| 255 |
self.log("No changes detected") |
| 256 |
else: |
| 257 |
if self.update: |
| 258 |
self.write_to_last_migration_files(changes) |
| 259 |
else: |
| 260 |
self.write_migration_files(changes) |
| 261 |
if check_changes: |
| 262 |
sys.exit(1) |
| 263 |
|
| 264 |
def write_to_last_migration_files(self, changes): |
| 265 |
loader = MigrationLoader(connections[DEFAULT_DB_ALIAS]) |
| 266 |
new_changes = {} |
| 267 |
update_previous_migration_paths = {} |
| 268 |
for app_label, app_migrations in changes.items(): |
| 269 |
# Find last migration. |
| 270 |
leaf_migration_nodes = loader.graph.leaf_nodes(app=app_label) |
| 271 |
if len(leaf_migration_nodes) == 0: |
| 272 |
raise CommandError( |
| 273 |
f"App {app_label} has no migration, cannot update last migration." |
| 274 |
) |
| 275 |
leaf_migration_node = leaf_migration_nodes[0] |
| 276 |
# Multiple leaf nodes have already been checked earlier in command. |
| 277 |
leaf_migration = loader.graph.nodes[leaf_migration_node] |
| 278 |
# Updated migration cannot be a squash migration, a dependency of |
| 279 |
# another migration, and cannot be already applied. |
| 280 |
if leaf_migration.replaces: |
| 281 |
raise CommandError( |
| 282 |
f"Cannot update squash migration '{leaf_migration}'." |
| 283 |
) |
| 284 |
if leaf_migration_node in loader.applied_migrations: |
| 285 |
raise CommandError( |
| 286 |
f"Cannot update applied migration '{leaf_migration}'." |
| 287 |
) |
| 288 |
depending_migrations = [ |
| 289 |
migration |
| 290 |
for migration in loader.disk_migrations.values() |
| 291 |
if leaf_migration_node in migration.dependencies |
| 292 |
] |
| 293 |
if depending_migrations: |
| 294 |
formatted_migrations = ", ".join( |
| 295 |
[f"'{migration}'" for migration in depending_migrations] |
| 296 |
) |
| 297 |
raise CommandError( |
| 298 |
f"Cannot update migration '{leaf_migration}' that migrations " |
| 299 |
f"{formatted_migrations} depend on." |
| 300 |
) |
| 301 |
# Build new migration. |
| 302 |
for migration in app_migrations: |
| 303 |
leaf_migration.operations.extend(migration.operations) |
| 304 |
|
| 305 |
for dependency in migration.dependencies: |
| 306 |
if isinstance(dependency, SwappableTuple): |
| 307 |
if settings.AUTH_USER_MODEL == dependency.setting: |
| 308 |
leaf_migration.dependencies.append( |
| 309 |
("__setting__", "AUTH_USER_MODEL") |
| 310 |
) |
| 311 |
else: |
| 312 |
leaf_migration.dependencies.append(dependency) |
| 313 |
elif dependency[0] != migration.app_label: |
| 314 |
leaf_migration.dependencies.append(dependency) |
| 315 |
# Optimize migration. |
| 316 |
optimizer = MigrationOptimizer() |
| 317 |
leaf_migration.operations = optimizer.optimize( |
| 318 |
leaf_migration.operations, app_label |
| 319 |
) |
| 320 |
# Update name. |
| 321 |
previous_migration_path = MigrationWriter(leaf_migration).path |
| 322 |
name_fragment = self.migration_name or leaf_migration.suggest_name() |
| 323 |
suggested_name = leaf_migration.name[:4] + f"_{name_fragment}" |
| 324 |
if leaf_migration.name == suggested_name: |
| 325 |
new_name = leaf_migration.name + "_updated" |
| 326 |
else: |
| 327 |
new_name = suggested_name |
| 328 |
leaf_migration.name = new_name |
| 329 |
# Register overridden migration. |
| 330 |
new_changes[app_label] = [leaf_migration] |
| 331 |
update_previous_migration_paths[app_label] = previous_migration_path |
| 332 |
|
| 333 |
self.write_migration_files(new_changes, update_previous_migration_paths) |
| 334 |
|
| 335 |
def write_migration_files(self, changes, update_previous_migration_paths=None): |
| 336 |
""" |
| 337 |
Take a changes dict and write them out as migration files. |
| 338 |
""" |
| 339 |
directory_created = {} |
| 340 |
for app_label, app_migrations in changes.items(): |
| 341 |
if self.verbosity >= 1: |
| 342 |
self.log(self.style.MIGRATE_HEADING("Migrations for '%s':" % app_label)) |
| 343 |
for migration in app_migrations: |
| 344 |
# Describe the migration |
| 345 |
writer = MigrationWriter(migration, self.include_header) |
| 346 |
if self.verbosity >= 1: |
| 347 |
# Display a relative path if it's below the current working |
| 348 |
# directory, or an absolute path otherwise. |
| 349 |
migration_string = self.get_relative_path(writer.path) |
| 350 |
self.log(" %s\n" % self.style.MIGRATE_LABEL(migration_string)) |
| 351 |
for operation in migration.operations: |
| 352 |
self.log(" %s" % operation.formatted_description()) |
| 353 |
if self.scriptable: |
| 354 |
self.stdout.write(migration_string) |
| 355 |
if not self.dry_run: |
| 356 |
# Write the migrations file to the disk. |
| 357 |
migrations_directory = os.path.dirname(writer.path) |
| 358 |
if not directory_created.get(app_label): |
| 359 |
os.makedirs(migrations_directory, exist_ok=True) |
| 360 |
init_path = os.path.join(migrations_directory, "__init__.py") |
| 361 |
if not os.path.isfile(init_path): |
| 362 |
open(init_path, "w").close() |
| 363 |
# We just do this once per app |
| 364 |
directory_created[app_label] = True |
| 365 |
migration_string = writer.as_string() |
| 366 |
with open(writer.path, "w", encoding="utf-8") as fh: |
| 367 |
fh.write(migration_string) |
| 368 |
self.written_files.append(writer.path) |
| 369 |
if update_previous_migration_paths: |
| 370 |
prev_path = update_previous_migration_paths[app_label] |
| 371 |
rel_prev_path = self.get_relative_path(prev_path) |
| 372 |
if writer.needs_manual_porting: |
| 373 |
migration_path = self.get_relative_path(writer.path) |
| 374 |
self.log( |
| 375 |
self.style.WARNING( |
| 376 |
f"Updated migration {migration_path} requires " |
| 377 |
f"manual porting.\n" |
| 378 |
f"Previous migration {rel_prev_path} was kept and " |
| 379 |
f"must be deleted after porting functions manually." |
| 380 |
) |
| 381 |
) |
| 382 |
else: |
| 383 |
os.remove(prev_path) |
| 384 |
self.log(f"Deleted {rel_prev_path}") |
| 385 |
elif self.verbosity == 3: |
| 386 |
# Alternatively, makemigrations --dry-run --verbosity 3 |
| 387 |
# will log the migrations rather than saving the file to |
| 388 |
# the disk. |
| 389 |
self.log( |
| 390 |
self.style.MIGRATE_HEADING( |
| 391 |
"Full migrations file '%s':" % writer.filename |
| 392 |
) |
| 393 |
) |
| 394 |
self.log(writer.as_string()) |
| 395 |
run_formatters(self.written_files, stderr=self.stderr) |
| 396 |
|
| 397 |
@staticmethod |
| 398 |
def get_relative_path(path): |
| 399 |
try: |
| 400 |
migration_string = os.path.relpath(path) |
| 401 |
except ValueError: |
| 402 |
migration_string = path |
| 403 |
if migration_string.startswith(".."): |
| 404 |
migration_string = path |
| 405 |
return migration_string |
| 406 |
|
| 407 |
def handle_merge(self, loader, conflicts): |
| 408 |
""" |
| 409 |
Handles merging together conflicted migrations interactively, |
| 410 |
if it's safe; otherwise, advises on how to fix it. |
| 411 |
""" |
| 412 |
if self.interactive: |
| 413 |
questioner = InteractiveMigrationQuestioner(prompt_output=self.log_output) |
| 414 |
else: |
| 415 |
questioner = MigrationQuestioner(defaults={"ask_merge": True}) |
| 416 |
|
| 417 |
for app_label, migration_names in conflicts.items(): |
| 418 |
# Grab out the migrations in question, and work out their |
| 419 |
# common ancestor. |
| 420 |
merge_migrations = [] |
| 421 |
for migration_name in migration_names: |
| 422 |
migration = loader.get_migration(app_label, migration_name) |
| 423 |
migration.ancestry = [ |
| 424 |
mig |
| 425 |
for mig in loader.graph.forwards_plan((app_label, migration_name)) |
| 426 |
if mig[0] == migration.app_label |
| 427 |
] |
| 428 |
merge_migrations.append(migration) |
| 429 |
|
| 430 |
def all_items_equal(seq): |
| 431 |
return all(item == seq[0] for item in seq[1:]) |
| 432 |
|
| 433 |
merge_migrations_generations = zip(*(m.ancestry for m in merge_migrations)) |
| 434 |
common_ancestor_count = sum( |
| 435 |
1 |
| 436 |
for common_ancestor_generation in takewhile( |
| 437 |
all_items_equal, merge_migrations_generations |
| 438 |
) |
| 439 |
) |
| 440 |
if not common_ancestor_count: |
| 441 |
raise ValueError( |
| 442 |
"Could not find common ancestor of %s" % migration_names |
| 443 |
) |
| 444 |
# Now work out the operations along each divergent branch |
| 445 |
for migration in merge_migrations: |
| 446 |
migration.branch = migration.ancestry[common_ancestor_count:] |
| 447 |
migrations_ops = ( |
| 448 |
loader.get_migration(node_app, node_name).operations |
| 449 |
for node_app, node_name in migration.branch |
| 450 |
) |
| 451 |
migration.merged_operations = sum(migrations_ops, []) |
| 452 |
# In future, this could use some of the Optimizer code |
| 453 |
# (can_optimize_through) to automatically see if they're |
| 454 |
# mergeable. For now, we always just prompt the user. |
| 455 |
if self.verbosity > 0: |
| 456 |
self.log(self.style.MIGRATE_HEADING("Merging %s" % app_label)) |
| 457 |
for migration in merge_migrations: |
| 458 |
self.log(self.style.MIGRATE_LABEL(" Branch %s" % migration.name)) |
| 459 |
for operation in migration.merged_operations: |
| 460 |
self.log(" %s" % operation.formatted_description()) |
| 461 |
if questioner.ask_merge(app_label): |
| 462 |
# If they still want to merge it, then write out an empty |
| 463 |
# file depending on the migrations needing merging. |
| 464 |
numbers = [ |
| 465 |
self.autodetector.parse_number(migration.name) |
| 466 |
for migration in merge_migrations |
| 467 |
] |
| 468 |
try: |
| 469 |
biggest_number = max(x for x in numbers if x is not None) |
| 470 |
except ValueError: |
| 471 |
biggest_number = 1 |
| 472 |
subclass = type( |
| 473 |
"Migration", |
| 474 |
(Migration,), |
| 475 |
{ |
| 476 |
"dependencies": [ |
| 477 |
(app_label, migration.name) |
| 478 |
for migration in merge_migrations |
| 479 |
], |
| 480 |
}, |
| 481 |
) |
| 482 |
parts = ["%04i" % (biggest_number + 1)] |
| 483 |
if self.migration_name: |
| 484 |
parts.append(self.migration_name) |
| 485 |
else: |
| 486 |
parts.append("merge") |
| 487 |
leaf_names = "_".join( |
| 488 |
sorted(migration.name for migration in merge_migrations) |
| 489 |
) |
| 490 |
if len(leaf_names) > 47: |
| 491 |
parts.append(get_migration_name_timestamp()) |
| 492 |
else: |
| 493 |
parts.append(leaf_names) |
| 494 |
migration_name = "_".join(parts) |
| 495 |
new_migration = subclass(migration_name, app_label) |
| 496 |
writer = MigrationWriter(new_migration, self.include_header) |
| 497 |
|
| 498 |
if not self.dry_run: |
| 499 |
# Write the merge migrations file to the disk |
| 500 |
with open(writer.path, "w", encoding="utf-8") as fh: |
| 501 |
fh.write(writer.as_string()) |
| 502 |
run_formatters([writer.path], stderr=self.stderr) |
| 503 |
if self.verbosity > 0: |
| 504 |
self.log("\nCreated new merge migration %s" % writer.path) |
| 505 |
if self.scriptable: |
| 506 |
self.stdout.write(writer.path) |
| 507 |
elif self.verbosity == 3: |
| 508 |
# Alternatively, makemigrations --merge --dry-run --verbosity 3 |
| 509 |
# will log the merge migrations rather than saving the file |
| 510 |
# to the disk. |
| 511 |
self.log( |
| 512 |
self.style.MIGRATE_HEADING( |
| 513 |
"Full merge migrations file '%s':" % writer.filename |
| 514 |
) |
| 515 |
) |
| 516 |
self.log(writer.as_string()) |