Debian Codegraph @

django/db/migrations/autodetector.py

raw
1 import functools
2 import re
3 from collections import defaultdict, namedtuple
4 from enum import Enum
5 from graphlib import TopologicalSorter
6 from itertools import chain
7
8 from django.conf import settings
9 from django.db import models
10 from django.db.migrations import operations
11 from django.db.migrations.migration import Migration
12 from django.db.migrations.operations.models import AlterModelOptions
13 from django.db.migrations.optimizer import MigrationOptimizer
14 from django.db.migrations.questioner import MigrationQuestioner
15 from django.db.migrations.utils import (
16 COMPILED_REGEX_TYPE,
17 RegexObject,
18 resolve_relation,
19 )
20 from django.utils.functional import cached_property
21
22
23 class OperationDependency(
24 namedtuple("OperationDependency", "app_label model_name field_name type")
25 ):
26 class Type(Enum):
27 CREATE = 0
28 REMOVE = 1
29 ALTER = 2
30 REMOVE_ORDER_WRT = 3
31 ALTER_FOO_TOGETHER = 4
32
33 @cached_property
34 def model_name_lower(self):
35 return self.model_name.lower()
36
37 @cached_property
38 def field_name_lower(self):
39 return self.field_name.lower()
40
41
42 class MigrationAutodetector:
43 """
44 Take a pair of ProjectStates and compare them to see what the first would
45 need doing to make it match the second (the second usually being the
46 project's current state).
47
48 Note that this naturally operates on entire projects at a time,
49 as it's likely that changes interact (for example, you can't
50 add a ForeignKey without having a migration to add the table it
51 depends on first). A user interface may offer single-app usage
52 if it wishes, with the caveat that it may not always be possible.
53 """
54
55 def __init__(self, from_state, to_state, questioner=None):
56 self.from_state = from_state
57 self.to_state = to_state
58 self.questioner = questioner or MigrationQuestioner()
59 self.existing_apps = {app for app, model in from_state.models}
60
61 def changes(self, graph, trim_to_apps=None, convert_apps=None, migration_name=None):
62 """
63 Main entry point to produce a list of applicable changes.
64 Take a graph to base names on and an optional set of apps
65 to try and restrict to (restriction is not guaranteed)
66 """
67 changes = self._detect_changes(convert_apps, graph)
68 changes = self.arrange_for_graph(changes, graph, migration_name)
69 if trim_to_apps:
70 changes = self._trim_to_apps(changes, trim_to_apps)
71 return changes
72
73 def deep_deconstruct(self, obj):
74 """
75 Recursive deconstruction for a field and its arguments.
76 Used for full comparison for rename/alter; sometimes a single-level
77 deconstruction will not compare correctly.
78 """
79 if isinstance(obj, list):
80 return [self.deep_deconstruct(value) for value in obj]
81 elif isinstance(obj, tuple):
82 return tuple(self.deep_deconstruct(value) for value in obj)
83 elif isinstance(obj, dict):
84 return {key: self.deep_deconstruct(value) for key, value in obj.items()}
85 elif isinstance(obj, functools.partial):
86 return (
87 obj.func,
88 self.deep_deconstruct(obj.args),
89 self.deep_deconstruct(obj.keywords),
90 )
91 elif isinstance(obj, COMPILED_REGEX_TYPE):
92 return RegexObject(obj)
93 elif isinstance(obj, type):
94 # If this is a type that implements 'deconstruct' as an instance method,
95 # avoid treating this as being deconstructible itself - see #22951
96 return obj
97 elif hasattr(obj, "deconstruct"):
98 deconstructed = obj.deconstruct()
99 if isinstance(obj, models.Field):
100 # we have a field which also returns a name
101 deconstructed = deconstructed[1:]
102 path, args, kwargs = deconstructed
103 return (
104 path,
105 [self.deep_deconstruct(value) for value in args],
106 {key: self.deep_deconstruct(value) for key, value in kwargs.items()},
107 )
108 else:
109 return obj
110
111 def only_relation_agnostic_fields(self, fields):
112 """
113 Return a definition of the fields that ignores field names and
114 what related fields actually relate to. Used for detecting renames (as
115 the related fields change during renames).
116 """
117 fields_def = []
118 for name, field in sorted(fields.items()):
119 deconstruction = self.deep_deconstruct(field)
120 if field.remote_field and field.remote_field.model:
121 deconstruction[2].pop("to", None)
122 fields_def.append(deconstruction)
123 return fields_def
124
125 def _detect_changes(self, convert_apps=None, graph=None):
126 """
127 Return a dict of migration plans which will achieve the
128 change from from_state to to_state. The dict has app labels
129 as keys and a list of migrations as values.
130
131 The resulting migrations aren't specially named, but the names
132 do matter for dependencies inside the set.
133
134 convert_apps is the list of apps to convert to use migrations
135 (i.e. to make initial migrations for, in the usual case)
136
137 graph is an optional argument that, if provided, can help improve
138 dependency generation and avoid potential circular dependencies.
139 """
140 # The first phase is generating all the operations for each app
141 # and gathering them into a big per-app list.
142 # Then go through that list, order it, and split into migrations to
143 # resolve dependencies caused by M2Ms and FKs.
144 self.generated_operations = {}
145 self.altered_indexes = {}
146 self.altered_constraints = {}
147 self.renamed_fields = {}
148
149 # Prepare some old/new state and model lists, separating
150 # proxy models and ignoring unmigrated apps.
151 self.old_model_keys = set()
152 self.old_proxy_keys = set()
153 self.old_unmanaged_keys = set()
154 self.new_model_keys = set()
155 self.new_proxy_keys = set()
156 self.new_unmanaged_keys = set()
157 for (app_label, model_name), model_state in self.from_state.models.items():
158 if not model_state.options.get("managed", True):
159 self.old_unmanaged_keys.add((app_label, model_name))
160 elif app_label not in self.from_state.real_apps:
161 if model_state.options.get("proxy"):
162 self.old_proxy_keys.add((app_label, model_name))
163 else:
164 self.old_model_keys.add((app_label, model_name))
165
166 for (app_label, model_name), model_state in self.to_state.models.items():
167 if not model_state.options.get("managed", True):
168 self.new_unmanaged_keys.add((app_label, model_name))
169 elif app_label not in self.from_state.real_apps or (
170 convert_apps and app_label in convert_apps
171 ):
172 if model_state.options.get("proxy"):
173 self.new_proxy_keys.add((app_label, model_name))
174 else:
175 self.new_model_keys.add((app_label, model_name))
176
177 self.from_state.resolve_fields_and_relations()
178 self.to_state.resolve_fields_and_relations()
179
180 # Renames have to come first
181 self.generate_renamed_models()
182
183 # Prepare lists of fields and generate through model map
184 self._prepare_field_lists()
185 self._generate_through_model_map()
186
187 # Generate non-rename model operations
188 self.generate_deleted_models()
189 self.generate_created_models()
190 self.generate_deleted_proxies()
191 self.generate_created_proxies()
192 self.generate_altered_options()
193 self.generate_altered_managers()
194 self.generate_altered_db_table_comment()
195
196 # Create the renamed fields and store them in self.renamed_fields.
197 # They are used by create_altered_indexes(), generate_altered_fields(),
198 # generate_removed_altered_index/unique_together(), and
199 # generate_altered_index/unique_together().
200 self.create_renamed_fields()
201 # Create the altered indexes and store them in self.altered_indexes.
202 # This avoids the same computation in generate_removed_indexes()
203 # and generate_added_indexes().
204 self.create_altered_indexes()
205 self.create_altered_constraints()
206 # Generate index removal operations before field is removed
207 self.generate_removed_constraints()
208 self.generate_removed_indexes()
209 # Generate field renaming operations.
210 self.generate_renamed_fields()
211 self.generate_renamed_indexes()
212 # Generate removal of foo together.
213 self.generate_removed_altered_unique_together()
214 # Generate field operations.
215 self.generate_removed_fields()
216 self.generate_added_fields()
217 self.generate_altered_fields()
218 self.generate_altered_order_with_respect_to()
219 self.generate_altered_unique_together()
220 self.generate_added_indexes()
221 self.generate_added_constraints()
222 self.generate_altered_constraints()
223 self.generate_altered_db_table()
224
225 self._sort_migrations()
226 self._build_migration_list(graph)
227 self._optimize_migrations()
228
229 return self.migrations
230
231 def _prepare_field_lists(self):
232 """
233 Prepare field lists and a list of the fields that used through models
234 in the old state so dependencies can be made from the through model
235 deletion to the field that uses it.
236 """
237 self.kept_model_keys = self.old_model_keys & self.new_model_keys
238 self.kept_proxy_keys = self.old_proxy_keys & self.new_proxy_keys
239 self.kept_unmanaged_keys = self.old_unmanaged_keys & self.new_unmanaged_keys
240 self.through_users = {}
241 self.old_field_keys = {
242 (app_label, model_name, field_name)
243 for app_label, model_name in self.kept_model_keys
244 for field_name in self.from_state.models[
245 app_label, self.renamed_models.get((app_label, model_name), model_name)
246 ].fields
247 }
248 self.new_field_keys = {
249 (app_label, model_name, field_name)
250 for app_label, model_name in self.kept_model_keys
251 for field_name in self.to_state.models[app_label, model_name].fields
252 }
253
254 def _generate_through_model_map(self):
255 """Through model map generation."""
256 for app_label, model_name in sorted(self.old_model_keys):
257 old_model_name = self.renamed_models.get(
258 (app_label, model_name), model_name
259 )
260 old_model_state = self.from_state.models[app_label, old_model_name]
261 for field_name, field in old_model_state.fields.items():
262 if hasattr(field, "remote_field") and getattr(
263 field.remote_field, "through", None
264 ):
265 through_key = resolve_relation(
266 field.remote_field.through, app_label, model_name
267 )
268 self.through_users[through_key] = (
269 app_label,
270 old_model_name,
271 field_name,
272 )
273
274 @staticmethod
275 def _resolve_dependency(dependency):
276 """
277 Return the resolved dependency and a boolean denoting whether or not
278 it was swappable.
279 """
280 if dependency.app_label != "__setting__":
281 return dependency, False
282 resolved_app_label, resolved_object_name = getattr(
283 settings, dependency.model_name
284 ).split(".")
285 return (
286 OperationDependency(
287 resolved_app_label,
288 resolved_object_name.lower(),
289 dependency.field_name,
290 dependency.type,
291 ),
292 True,
293 )
294
295 def _build_migration_list(self, graph=None):
296 """
297 Chop the lists of operations up into migrations with dependencies on
298 each other. Do this by going through an app's list of operations until
299 one is found that has an outgoing dependency that isn't in another
300 app's migration yet (hasn't been chopped off its list). Then chop off
301 the operations before it into a migration and move onto the next app.
302 If the loops completes without doing anything, there's a circular
303 dependency (which _should_ be impossible as the operations are
304 all split at this point so they can't depend and be depended on).
305 """
306 self.migrations = {}
307 num_ops = sum(len(x) for x in self.generated_operations.values())
308 chop_mode = False
309 while num_ops:
310 # On every iteration, we step through all the apps and see if there
311 # is a completed set of operations.
312 # If we find that a subset of the operations are complete we can
313 # try to chop it off from the rest and continue, but we only
314 # do this if we've already been through the list once before
315 # without any chopping and nothing has changed.
316 for app_label in sorted(self.generated_operations):
317 chopped = []
318 dependencies = set()
319 for operation in list(self.generated_operations[app_label]):
320 deps_satisfied = True
321 operation_dependencies = set()
322 for dep in operation._auto_deps:
323 # Temporarily resolve the swappable dependency to
324 # prevent circular references. While keeping the
325 # dependency checks on the resolved model, add the
326 # swappable dependencies.
327 original_dep = dep
328 dep, is_swappable_dep = self._resolve_dependency(dep)
329 if dep.app_label != app_label:
330 # External app dependency. See if it's not yet
331 # satisfied.
332 for other_operation in self.generated_operations.get(
333 dep.app_label, []
334 ):
335 if self.check_dependency(other_operation, dep):
336 deps_satisfied = False
337 break
338 if not deps_satisfied:
339 break
340 else:
341 if is_swappable_dep:
342 operation_dependencies.add(
343 (
344 original_dep.app_label,
345 original_dep.model_name,
346 )
347 )
348 elif dep.app_label in self.migrations:
349 operation_dependencies.add(
350 (
351 dep.app_label,
352 self.migrations[dep.app_label][-1].name,
353 )
354 )
355 else:
356 # If we can't find the other app, we add a
357 # first/last dependency, but only if we've
358 # already been through once and checked
359 # everything.
360 if chop_mode:
361 # If the app already exists, we add a
362 # dependency on the last migration, as
363 # we don't know which migration
364 # contains the target field. If it's
365 # not yet migrated or has no
366 # migrations, we use __first__.
367 if graph and graph.leaf_nodes(dep.app_label):
368 operation_dependencies.add(
369 graph.leaf_nodes(dep.app_label)[0]
370 )
371 else:
372 operation_dependencies.add(
373 (dep.app_label, "__first__")
374 )
375 else:
376 deps_satisfied = False
377 if deps_satisfied:
378 chopped.append(operation)
379 dependencies.update(operation_dependencies)
380 del self.generated_operations[app_label][0]
381 else:
382 break
383 # Make a migration! Well, only if there's stuff to put in it
384 if dependencies or chopped:
385 if not self.generated_operations[app_label] or chop_mode:
386 subclass = type(
387 "Migration",
388 (Migration,),
389 {"operations": [], "dependencies": []},
390 )
391 instance = subclass(
392 "auto_%i" % (len(self.migrations.get(app_label, [])) + 1),
393 app_label,
394 )
395 instance.dependencies = list(dependencies)
396 instance.operations = chopped
397 instance.initial = app_label not in self.existing_apps
398 self.migrations.setdefault(app_label, []).append(instance)
399 chop_mode = False
400 else:
401 self.generated_operations[app_label] = (
402 chopped + self.generated_operations[app_label]
403 )
404 new_num_ops = sum(len(x) for x in self.generated_operations.values())
405 if new_num_ops == num_ops:
406 if not chop_mode:
407 chop_mode = True
408 else:
409 raise ValueError(
410 "Cannot resolve operation dependencies: %r"
411 % self.generated_operations
412 )
413 num_ops = new_num_ops
414
415 def _sort_migrations(self):
416 """
417 Reorder to make things possible. Reordering may be needed so FKs work
418 nicely inside the same app.
419 """
420 for app_label, ops in sorted(self.generated_operations.items()):
421 ts = TopologicalSorter()
422 for op in ops:
423 ts.add(op)
424 for dep in op._auto_deps:
425 # Resolve intra-app dependencies to handle circular
426 # references involving a swappable model.
427 dep = self._resolve_dependency(dep)[0]
428 if dep.app_label != app_label:
429 continue
430 ts.add(op, *(x for x in ops if self.check_dependency(x, dep)))
431 self.generated_operations[app_label] = list(ts.static_order())
432
433 def _optimize_migrations(self):
434 # Add in internal dependencies among the migrations
435 for app_label, migrations in self.migrations.items():
436 for m1, m2 in zip(migrations, migrations[1:]):
437 m2.dependencies.append((app_label, m1.name))
438
439 # De-dupe dependencies
440 for migrations in self.migrations.values():
441 for migration in migrations:
442 migration.dependencies = list(set(migration.dependencies))
443
444 # Optimize migrations
445 for app_label, migrations in self.migrations.items():
446 for migration in migrations:
447 migration.operations = MigrationOptimizer().optimize(
448 migration.operations, app_label
449 )
450
451 def check_dependency(self, operation, dependency):
452 """
453 Return True if the given operation depends on the given dependency,
454 False otherwise.
455 """
456 # Created model
457 if (
458 dependency.field_name is None
459 and dependency.type == OperationDependency.Type.CREATE
460 ):
461 return (
462 isinstance(operation, operations.CreateModel)
463 and operation.name_lower == dependency.model_name_lower
464 )
465 # Created field
466 elif (
467 dependency.field_name is not None
468 and dependency.type == OperationDependency.Type.CREATE
469 ):
470 return (
471 isinstance(operation, operations.CreateModel)
472 and operation.name_lower == dependency.model_name_lower
473 and any(dependency.field_name == x for x, y in operation.fields)
474 ) or (
475 isinstance(operation, operations.AddField)
476 and operation.model_name_lower == dependency.model_name_lower
477 and operation.name_lower == dependency.field_name_lower
478 )
479 # Removed field
480 elif (
481 dependency.field_name is not None
482 and dependency.type == OperationDependency.Type.REMOVE
483 ):
484 return (
485 isinstance(operation, operations.RemoveField)
486 and operation.model_name_lower == dependency.model_name_lower
487 and operation.name_lower == dependency.field_name_lower
488 )
489 # Removed model
490 elif (
491 dependency.field_name is None
492 and dependency.type == OperationDependency.Type.REMOVE
493 ):
494 return (
495 isinstance(operation, operations.DeleteModel)
496 and operation.name_lower == dependency.model_name_lower
497 )
498 # Field being altered
499 elif (
500 dependency.field_name is not None
501 and dependency.type == OperationDependency.Type.ALTER
502 ):
503 return (
504 isinstance(operation, operations.AlterField)
505 and operation.model_name_lower == dependency.model_name_lower
506 and operation.name_lower == dependency.field_name_lower
507 )
508 # order_with_respect_to being unset for a field
509 elif (
510 dependency.field_name is not None
511 and dependency.type == OperationDependency.Type.REMOVE_ORDER_WRT
512 ):
513 return (
514 isinstance(operation, operations.AlterOrderWithRespectTo)
515 and operation.name_lower == dependency.model_name_lower
516 and (operation.order_with_respect_to or "").lower()
517 != dependency.field_name_lower
518 )
519 # Field is removed and part of an index/unique_together
520 elif (
521 dependency.field_name is not None
522 and dependency.type == OperationDependency.Type.ALTER_FOO_TOGETHER
523 ):
524 return (
525 isinstance(
526 operation,
527 (operations.AlterUniqueTogether, operations.AlterIndexTogether),
528 )
529 and operation.name_lower == dependency.model_name_lower
530 )
531 # Unknown dependency. Raise an error.
532 else:
533 raise ValueError("Can't handle dependency %r" % (dependency,))
534
535 def add_operation(self, app_label, operation, dependencies=None, beginning=False):
536 # Dependencies are
537 # (app_label, model_name, field_name, create/delete as True/False)
538 operation._auto_deps = dependencies or []
539 if beginning:
540 self.generated_operations.setdefault(app_label, []).insert(0, operation)
541 else:
542 self.generated_operations.setdefault(app_label, []).append(operation)
543
544 def swappable_first_key(self, item):
545 """
546 Place potential swappable models first in lists of created models (only
547 real way to solve #22783).
548 """
549 try:
550 model_state = self.to_state.models[item]
551 base_names = {
552 base if isinstance(base, str) else base.__name__
553 for base in model_state.bases
554 }
555 string_version = "%s.%s" % (item[0], item[1])
556 if (
557 model_state.options.get("swappable")
558 or "AbstractUser" in base_names
559 or "AbstractBaseUser" in base_names
560 or settings.AUTH_USER_MODEL.lower() == string_version.lower()
561 ):
562 return ("___" + item[0], "___" + item[1])
563 except LookupError:
564 pass
565 return item
566
567 def generate_renamed_models(self):
568 """
569 Find any renamed models, generate the operations for them, and remove
570 the old entry from the model lists. Must be run before other
571 model-level generation.
572 """
573 self.renamed_models = {}
574 self.renamed_models_rel = {}
575 added_models = self.new_model_keys - self.old_model_keys
576 for app_label, model_name in sorted(added_models):
577 model_state = self.to_state.models[app_label, model_name]
578 model_fields_def = self.only_relation_agnostic_fields(model_state.fields)
579
580 removed_models = self.old_model_keys - self.new_model_keys
581 for rem_app_label, rem_model_name in removed_models:
582 if rem_app_label == app_label:
583 rem_model_state = self.from_state.models[
584 rem_app_label, rem_model_name
585 ]
586 rem_model_fields_def = self.only_relation_agnostic_fields(
587 rem_model_state.fields
588 )
589 if model_fields_def == rem_model_fields_def:
590 if self.questioner.ask_rename_model(
591 rem_model_state, model_state
592 ):
593 dependencies = []
594 fields = list(model_state.fields.values()) + [
595 field.remote_field
596 for relations in self.to_state.relations[
597 app_label, model_name
598 ].values()
599 for field in relations.values()
600 ]
601 for field in fields:
602 if field.is_relation:
603 dependencies.extend(
604 self._get_dependencies_for_foreign_key(
605 app_label,
606 model_name,
607 field,
608 self.to_state,
609 )
610 )
611 self.add_operation(
612 app_label,
613 operations.RenameModel(
614 old_name=rem_model_state.name,
615 new_name=model_state.name,
616 ),
617 dependencies=dependencies,
618 )
619 self.renamed_models[app_label, model_name] = rem_model_name
620 renamed_models_rel_key = "%s.%s" % (
621 rem_model_state.app_label,
622 rem_model_state.name_lower,
623 )
624 self.renamed_models_rel[renamed_models_rel_key] = (
625 "%s.%s"
626 % (
627 model_state.app_label,
628 model_state.name_lower,
629 )
630 )
631 self.old_model_keys.remove((rem_app_label, rem_model_name))
632 self.old_model_keys.add((app_label, model_name))
633 break
634
635 def generate_created_models(self):
636 """
637 Find all new models (both managed and unmanaged) and make create
638 operations for them as well as separate operations to create any
639 foreign key or M2M relationships (these are optimized later, if
640 possible).
641
642 Defer any model options that refer to collections of fields that might
643 be deferred (e.g. unique_together).
644 """
645 old_keys = self.old_model_keys | self.old_unmanaged_keys
646 added_models = self.new_model_keys - old_keys
647 added_unmanaged_models = self.new_unmanaged_keys - old_keys
648 all_added_models = chain(
649 sorted(added_models, key=self.swappable_first_key, reverse=True),
650 sorted(added_unmanaged_models, key=self.swappable_first_key, reverse=True),
651 )
652 for app_label, model_name in all_added_models:
653 model_state = self.to_state.models[app_label, model_name]
654 # Gather related fields
655 related_fields = {}
656 primary_key_rel = None
657 for field_name, field in model_state.fields.items():
658 if field.remote_field:
659 if field.remote_field.model:
660 if field.primary_key:
661 primary_key_rel = field.remote_field.model
662 elif not field.remote_field.parent_link:
663 related_fields[field_name] = field
664 if getattr(field.remote_field, "through", None):
665 related_fields[field_name] = field
666
667 # Are there indexes/unique_together to defer?
668 indexes = model_state.options.pop("indexes")
669 constraints = model_state.options.pop("constraints")
670 unique_together = model_state.options.pop("unique_together", None)
671 order_with_respect_to = model_state.options.pop(
672 "order_with_respect_to", None
673 )
674 # Depend on the deletion of any possible proxy version of us
675 dependencies = [
676 OperationDependency(
677 app_label, model_name, None, OperationDependency.Type.REMOVE
678 ),
679 ]
680 # Depend on all bases
681 for base in model_state.bases:
682 if isinstance(base, str) and "." in base:
683 base_app_label, base_name = base.split(".", 1)
684 dependencies.append(
685 OperationDependency(
686 base_app_label,
687 base_name,
688 None,
689 OperationDependency.Type.CREATE,
690 )
691 )
692 # Depend on the removal of base fields if the new model has
693 # a field with the same name.
694 old_base_model_state = self.from_state.models.get(
695 (base_app_label, base_name)
696 )
697 new_base_model_state = self.to_state.models.get(
698 (base_app_label, base_name)
699 )
700 if old_base_model_state and new_base_model_state:
701 removed_base_fields = (
702 set(old_base_model_state.fields)
703 .difference(
704 new_base_model_state.fields,
705 )
706 .intersection(model_state.fields)
707 )
708 for removed_base_field in removed_base_fields:
709 dependencies.append(
710 OperationDependency(
711 base_app_label,
712 base_name,
713 removed_base_field,
714 OperationDependency.Type.REMOVE,
715 )
716 )
717 # Depend on the other end of the primary key if it's a relation
718 if primary_key_rel:
719 dependencies.append(
720 OperationDependency(
721 *resolve_relation(primary_key_rel, app_label, model_name),
722 None,
723 OperationDependency.Type.CREATE,
724 ),
725 )
726 # Generate creation operation
727 self.add_operation(
728 app_label,
729 operations.CreateModel(
730 name=model_state.name,
731 fields=[
732 d
733 for d in model_state.fields.items()
734 if d[0] not in related_fields
735 ],
736 options=model_state.options,
737 bases=model_state.bases,
738 managers=model_state.managers,
739 ),
740 dependencies=dependencies,
741 beginning=True,
742 )
743
744 # Don't add operations which modify the database for unmanaged models
745 if not model_state.options.get("managed", True):
746 continue
747
748 # Generate operations for each related field
749 for name, field in sorted(related_fields.items()):
750 dependencies = self._get_dependencies_for_foreign_key(
751 app_label,
752 model_name,
753 field,
754 self.to_state,
755 )
756 # Depend on our own model being created
757 dependencies.append(
758 OperationDependency(
759 app_label, model_name, None, OperationDependency.Type.CREATE
760 )
761 )
762 # Make operation
763 self.add_operation(
764 app_label,
765 operations.AddField(
766 model_name=model_name,
767 name=name,
768 field=field,
769 ),
770 dependencies=list(set(dependencies)),
771 )
772 # Generate other opns
773 if order_with_respect_to:
774 self.add_operation(
775 app_label,
776 operations.AlterOrderWithRespectTo(
777 name=model_name,
778 order_with_respect_to=order_with_respect_to,
779 ),
780 dependencies=[
781 OperationDependency(
782 app_label,
783 model_name,
784 order_with_respect_to,
785 OperationDependency.Type.CREATE,
786 ),
787 OperationDependency(
788 app_label, model_name, None, OperationDependency.Type.CREATE
789 ),
790 ],
791 )
792 related_dependencies = [
793 OperationDependency(
794 app_label, model_name, name, OperationDependency.Type.CREATE
795 )
796 for name in sorted(related_fields)
797 ]
798 related_dependencies.append(
799 OperationDependency(
800 app_label, model_name, None, OperationDependency.Type.CREATE
801 )
802 )
803 for index in indexes:
804 self.add_operation(
805 app_label,
806 operations.AddIndex(
807 model_name=model_name,
808 index=index,
809 ),
810 dependencies=related_dependencies,
811 )
812 for constraint in constraints:
813 self.add_operation(
814 app_label,
815 operations.AddConstraint(
816 model_name=model_name,
817 constraint=constraint,
818 ),
819 dependencies=related_dependencies,
820 )
821 if unique_together:
822 self.add_operation(
823 app_label,
824 operations.AlterUniqueTogether(
825 name=model_name,
826 unique_together=unique_together,
827 ),
828 dependencies=related_dependencies,
829 )
830 # Fix relationships if the model changed from a proxy model to a
831 # concrete model.
832 relations = self.to_state.relations
833 if (app_label, model_name) in self.old_proxy_keys:
834 for related_model_key, related_fields in relations[
835 app_label, model_name
836 ].items():
837 related_model_state = self.to_state.models[related_model_key]
838 for related_field_name, related_field in related_fields.items():
839 self.add_operation(
840 related_model_state.app_label,
841 operations.AlterField(
842 model_name=related_model_state.name,
843 name=related_field_name,
844 field=related_field,
845 ),
846 dependencies=[
847 OperationDependency(
848 app_label,
849 model_name,
850 None,
851 OperationDependency.Type.CREATE,
852 )
853 ],
854 )
855
856 def generate_created_proxies(self):
857 """
858 Make CreateModel statements for proxy models. Use the same statements
859 as that way there's less code duplication, but for proxy models it's
860 safe to skip all the pointless field stuff and chuck out an operation.
861 """
862 added = self.new_proxy_keys - self.old_proxy_keys
863 for app_label, model_name in sorted(added):
864 model_state = self.to_state.models[app_label, model_name]
865 assert model_state.options.get("proxy")
866 # Depend on the deletion of any possible non-proxy version of us
867 dependencies = [
868 OperationDependency(
869 app_label, model_name, None, OperationDependency.Type.REMOVE
870 ),
871 ]
872 # Depend on all bases
873 for base in model_state.bases:
874 if isinstance(base, str) and "." in base:
875 base_app_label, base_name = base.split(".", 1)
876 dependencies.append(
877 OperationDependency(
878 base_app_label,
879 base_name,
880 None,
881 OperationDependency.Type.CREATE,
882 )
883 )
884 # Generate creation operation
885 self.add_operation(
886 app_label,
887 operations.CreateModel(
888 name=model_state.name,
889 fields=[],
890 options=model_state.options,
891 bases=model_state.bases,
892 managers=model_state.managers,
893 ),
894 # Depend on the deletion of any possible non-proxy version of us
895 dependencies=dependencies,
896 )
897
898 def generate_deleted_models(self):
899 """
900 Find all deleted models (managed and unmanaged) and make delete
901 operations for them as well as separate operations to delete any
902 foreign key or M2M relationships (these are optimized later, if
903 possible).
904
905 Also bring forward removal of any model options that refer to
906 collections of fields - the inverse of generate_created_models().
907 """
908 new_keys = self.new_model_keys | self.new_unmanaged_keys
909 deleted_models = self.old_model_keys - new_keys
910 deleted_unmanaged_models = self.old_unmanaged_keys - new_keys
911 all_deleted_models = chain(
912 sorted(deleted_models), sorted(deleted_unmanaged_models)
913 )
914 for app_label, model_name in all_deleted_models:
915 model_state = self.from_state.models[app_label, model_name]
916 # Gather related fields
917 related_fields = {}
918 for field_name, field in model_state.fields.items():
919 if field.remote_field:
920 if field.remote_field.model:
921 related_fields[field_name] = field
922 if getattr(field.remote_field, "through", None):
923 related_fields[field_name] = field
924 # Generate option removal first
925 unique_together = model_state.options.pop("unique_together", None)
926 if unique_together:
927 self.add_operation(
928 app_label,
929 operations.AlterUniqueTogether(
930 name=model_name,
931 unique_together=None,
932 ),
933 )
934 # Then remove each related field
935 for name in sorted(related_fields):
936 self.add_operation(
937 app_label,
938 operations.RemoveField(
939 model_name=model_name,
940 name=name,
941 ),
942 )
943 # Finally, remove the model.
944 # This depends on both the removal/alteration of all incoming fields
945 # and the removal of all its own related fields, and if it's
946 # a through model the field that references it.
947 dependencies = []
948 relations = self.from_state.relations
949 for (
950 related_object_app_label,
951 object_name,
952 ), relation_related_fields in relations[app_label, model_name].items():
953 for field_name, field in relation_related_fields.items():
954 dependencies.append(
955 OperationDependency(
956 related_object_app_label,
957 object_name,
958 field_name,
959 OperationDependency.Type.REMOVE,
960 ),
961 )
962 if not field.many_to_many:
963 dependencies.append(
964 OperationDependency(
965 related_object_app_label,
966 object_name,
967 field_name,
968 OperationDependency.Type.ALTER,
969 ),
970 )
971
972 for name in sorted(related_fields):
973 dependencies.append(
974 OperationDependency(
975 app_label, model_name, name, OperationDependency.Type.REMOVE
976 )
977 )
978 # We're referenced in another field's through=
979 through_user = self.through_users.get((app_label, model_state.name_lower))
980 if through_user:
981 dependencies.append(
982 OperationDependency(*through_user, OperationDependency.Type.REMOVE),
983 )
984 # Finally, make the operation, deduping any dependencies
985 self.add_operation(
986 app_label,
987 operations.DeleteModel(
988 name=model_state.name,
989 ),
990 dependencies=list(set(dependencies)),
991 )
992
993 def generate_deleted_proxies(self):
994 """Make DeleteModel options for proxy models."""
995 deleted = self.old_proxy_keys - self.new_proxy_keys
996 for app_label, model_name in sorted(deleted):
997 model_state = self.from_state.models[app_label, model_name]
998 assert model_state.options.get("proxy")
999 self.add_operation(
1000 app_label,
1001 operations.DeleteModel(
1002 name=model_state.name,
1003 ),
1004 )
1005
1006 def create_renamed_fields(self):
1007 """Work out renamed fields."""
1008 self.renamed_operations = []
1009 old_field_keys = self.old_field_keys.copy()
1010 for app_label, model_name, field_name in sorted(
1011 self.new_field_keys - old_field_keys
1012 ):
1013 old_model_name = self.renamed_models.get(
1014 (app_label, model_name), model_name
1015 )
1016 old_model_state = self.from_state.models[app_label, old_model_name]
1017 new_model_state = self.to_state.models[app_label, model_name]
1018 field = new_model_state.get_field(field_name)
1019 # Scan to see if this is actually a rename!
1020 field_dec = self.deep_deconstruct(field)
1021 for rem_app_label, rem_model_name, rem_field_name in sorted(
1022 old_field_keys - self.new_field_keys
1023 ):
1024 if rem_app_label == app_label and rem_model_name == model_name:
1025 old_field = old_model_state.get_field(rem_field_name)
1026 old_field_dec = self.deep_deconstruct(old_field)
1027 if (
1028 field.remote_field
1029 and field.remote_field.model
1030 and "to" in old_field_dec[2]
1031 ):
1032 old_rel_to = old_field_dec[2]["to"]
1033 if old_rel_to in self.renamed_models_rel:
1034 old_field_dec[2]["to"] = self.renamed_models_rel[old_rel_to]
1035 old_field.set_attributes_from_name(rem_field_name)
1036 old_db_column = old_field.get_attname_column()[1]
1037 if old_field_dec == field_dec or (
1038 # Was the field renamed and db_column equal to the
1039 # old field's column added?
1040 old_field_dec[0:2] == field_dec[0:2]
1041 and dict(old_field_dec[2], db_column=old_db_column)
1042 == field_dec[2]
1043 ):
1044 if self.questioner.ask_rename(
1045 model_name, rem_field_name, field_name, field
1046 ):
1047 self.renamed_operations.append(
1048 (
1049 rem_app_label,
1050 rem_model_name,
1051 old_field.db_column,
1052 rem_field_name,
1053 app_label,
1054 model_name,
1055 field,
1056 field_name,
1057 )
1058 )
1059 old_field_keys.remove(
1060 (rem_app_label, rem_model_name, rem_field_name)
1061 )
1062 old_field_keys.add((app_label, model_name, field_name))
1063 self.renamed_fields[app_label, model_name, field_name] = (
1064 rem_field_name
1065 )
1066 break
1067
1068 def generate_renamed_fields(self):
1069 """Generate RenameField operations."""
1070 for (
1071 rem_app_label,
1072 rem_model_name,
1073 rem_db_column,
1074 rem_field_name,
1075 app_label,
1076 model_name,
1077 field,
1078 field_name,
1079 ) in self.renamed_operations:
1080 # A db_column mismatch requires a prior noop AlterField for the
1081 # subsequent RenameField to be a noop on attempts at preserving the
1082 # old name.
1083 if rem_db_column != field.db_column:
1084 altered_field = field.clone()
1085 altered_field.name = rem_field_name
1086 self.add_operation(
1087 app_label,
1088 operations.AlterField(
1089 model_name=model_name,
1090 name=rem_field_name,
1091 field=altered_field,
1092 ),
1093 )
1094 self.add_operation(
1095 app_label,
1096 operations.RenameField(
1097 model_name=model_name,
1098 old_name=rem_field_name,
1099 new_name=field_name,
1100 ),
1101 )
1102 self.old_field_keys.remove((rem_app_label, rem_model_name, rem_field_name))
1103 self.old_field_keys.add((app_label, model_name, field_name))
1104
1105 def generate_added_fields(self):
1106 """Make AddField operations."""
1107 for app_label, model_name, field_name in sorted(
1108 self.new_field_keys - self.old_field_keys
1109 ):
1110 self._generate_added_field(app_label, model_name, field_name)
1111
1112 def _generate_added_field(self, app_label, model_name, field_name):
1113 field = self.to_state.models[app_label, model_name].get_field(field_name)
1114 # Adding a field always depends at least on its removal.
1115 dependencies = [
1116 OperationDependency(
1117 app_label, model_name, field_name, OperationDependency.Type.REMOVE
1118 )
1119 ]
1120 # Fields that are foreignkeys/m2ms depend on stuff.
1121 if field.remote_field and field.remote_field.model:
1122 dependencies.extend(
1123 self._get_dependencies_for_foreign_key(
1124 app_label,
1125 model_name,
1126 field,
1127 self.to_state,
1128 )
1129 )
1130 if field.generated:
1131 dependencies.extend(self._get_dependencies_for_generated_field(field))
1132 # You can't just add NOT NULL fields with no default or fields
1133 # which don't allow empty strings as default.
1134 time_fields = (models.DateField, models.DateTimeField, models.TimeField)
1135 auto_fields = (models.AutoField, models.SmallAutoField, models.BigAutoField)
1136 preserve_default = (
1137 field.null
1138 or field.has_default()
1139 or field.has_db_default()
1140 or field.many_to_many
1141 or (field.blank and field.empty_strings_allowed)
1142 or (isinstance(field, time_fields) and field.auto_now)
1143 or (isinstance(field, auto_fields))
1144 )
1145 if not preserve_default:
1146 field = field.clone()
1147 if isinstance(field, time_fields) and field.auto_now_add:
1148 field.default = self.questioner.ask_auto_now_add_addition(
1149 field_name, model_name
1150 )
1151 else:
1152 field.default = self.questioner.ask_not_null_addition(
1153 field_name, model_name
1154 )
1155 if field.unique and field.has_default() and callable(field.default):
1156 self.questioner.ask_unique_callable_default_addition(field_name, model_name)
1157 self.add_operation(
1158 app_label,
1159 operations.AddField(
1160 model_name=model_name,
1161 name=field_name,
1162 field=field,
1163 preserve_default=preserve_default,
1164 ),
1165 dependencies=dependencies,
1166 )
1167
1168 def generate_removed_fields(self):
1169 """Make RemoveField operations."""
1170 for app_label, model_name, field_name in sorted(
1171 self.old_field_keys - self.new_field_keys
1172 ):
1173 self._generate_removed_field(app_label, model_name, field_name)
1174
1175 def _generate_removed_field(self, app_label, model_name, field_name):
1176 self.add_operation(
1177 app_label,
1178 operations.RemoveField(
1179 model_name=model_name,
1180 name=field_name,
1181 ),
1182 # We might need to depend on the removal of an
1183 # order_with_respect_to or index/unique_together operation;
1184 # this is safely ignored if there isn't one
1185 dependencies=[
1186 OperationDependency(
1187 app_label,
1188 model_name,
1189 field_name,
1190 OperationDependency.Type.REMOVE_ORDER_WRT,
1191 ),
1192 OperationDependency(
1193 app_label,
1194 model_name,
1195 field_name,
1196 OperationDependency.Type.ALTER_FOO_TOGETHER,
1197 ),
1198 ],
1199 )
1200
1201 def generate_altered_fields(self):
1202 """
1203 Make AlterField operations, or possibly RemovedField/AddField if alter
1204 isn't possible.
1205 """
1206 for app_label, model_name, field_name in sorted(
1207 self.old_field_keys & self.new_field_keys
1208 ):
1209 # Did the field change?
1210 old_model_name = self.renamed_models.get(
1211 (app_label, model_name), model_name
1212 )
1213 old_field_name = self.renamed_fields.get(
1214 (app_label, model_name, field_name), field_name
1215 )
1216 old_field = self.from_state.models[app_label, old_model_name].get_field(
1217 old_field_name
1218 )
1219 new_field = self.to_state.models[app_label, model_name].get_field(
1220 field_name
1221 )
1222 dependencies = []
1223 # Implement any model renames on relations; these are handled by RenameModel
1224 # so we need to exclude them from the comparison
1225 if hasattr(new_field, "remote_field") and getattr(
1226 new_field.remote_field, "model", None
1227 ):
1228 rename_key = resolve_relation(
1229 new_field.remote_field.model, app_label, model_name
1230 )
1231 if rename_key in self.renamed_models:
1232 new_field.remote_field.model = old_field.remote_field.model
1233 # Handle ForeignKey which can only have a single to_field.
1234 remote_field_name = getattr(new_field.remote_field, "field_name", None)
1235 if remote_field_name:
1236 to_field_rename_key = rename_key + (remote_field_name,)
1237 if to_field_rename_key in self.renamed_fields:
1238 # Repoint both model and field name because to_field
1239 # inclusion in ForeignKey.deconstruct() is based on
1240 # both.
1241 new_field.remote_field.model = old_field.remote_field.model
1242 new_field.remote_field.field_name = (
1243 old_field.remote_field.field_name
1244 )
1245 # Handle ForeignObjects which can have multiple from_fields/to_fields.
1246 from_fields = getattr(new_field, "from_fields", None)
1247 if from_fields:
1248 from_rename_key = (app_label, model_name)
1249 new_field.from_fields = tuple(
1250 [
1251 self.renamed_fields.get(
1252 from_rename_key + (from_field,), from_field
1253 )
1254 for from_field in from_fields
1255 ]
1256 )
1257 new_field.to_fields = tuple(
1258 [
1259 self.renamed_fields.get(rename_key + (to_field,), to_field)
1260 for to_field in new_field.to_fields
1261 ]
1262 )
1263 if old_from_fields := getattr(old_field, "from_fields", None):
1264 old_field.from_fields = tuple(old_from_fields)
1265 old_field.to_fields = tuple(old_field.to_fields)
1266 dependencies.extend(
1267 self._get_dependencies_for_foreign_key(
1268 app_label,
1269 model_name,
1270 new_field,
1271 self.to_state,
1272 )
1273 )
1274 if hasattr(new_field, "remote_field") and getattr(
1275 new_field.remote_field, "through", None
1276 ):
1277 rename_key = resolve_relation(
1278 new_field.remote_field.through, app_label, model_name
1279 )
1280 if rename_key in self.renamed_models:
1281 new_field.remote_field.through = old_field.remote_field.through
1282 old_field_dec = self.deep_deconstruct(old_field)
1283 new_field_dec = self.deep_deconstruct(new_field)
1284 # If the field was confirmed to be renamed it means that only
1285 # db_column was allowed to change which generate_renamed_fields()
1286 # already accounts for by adding an AlterField operation.
1287 if old_field_dec != new_field_dec and old_field_name == field_name:
1288 both_m2m = old_field.many_to_many and new_field.many_to_many
1289 neither_m2m = not old_field.many_to_many and not new_field.many_to_many
1290 if both_m2m or neither_m2m:
1291 # Either both fields are m2m or neither is
1292 preserve_default = True
1293 if (
1294 old_field.null
1295 and not new_field.null
1296 and not new_field.has_default()
1297 and not new_field.has_db_default()
1298 and not new_field.many_to_many
1299 ):
1300 field = new_field.clone()
1301 new_default = self.questioner.ask_not_null_alteration(
1302 field_name, model_name
1303 )
1304 if new_default is not models.NOT_PROVIDED:
1305 field.default = new_default
1306 preserve_default = False
1307 else:
1308 field = new_field
1309 self.add_operation(
1310 app_label,
1311 operations.AlterField(
1312 model_name=model_name,
1313 name=field_name,
1314 field=field,
1315 preserve_default=preserve_default,
1316 ),
1317 dependencies=dependencies,
1318 )
1319 else:
1320 # We cannot alter between m2m and concrete fields
1321 self._generate_removed_field(app_label, model_name, field_name)
1322 self._generate_added_field(app_label, model_name, field_name)
1323
1324 def create_altered_indexes(self):
1325 option_name = operations.AddIndex.option_name
1326 self.renamed_index_together_values = defaultdict(list)
1327
1328 for app_label, model_name in sorted(self.kept_model_keys):
1329 old_model_name = self.renamed_models.get(
1330 (app_label, model_name), model_name
1331 )
1332 old_model_state = self.from_state.models[app_label, old_model_name]
1333 new_model_state = self.to_state.models[app_label, model_name]
1334
1335 old_indexes = old_model_state.options[option_name]
1336 new_indexes = new_model_state.options[option_name]
1337 added_indexes = [idx for idx in new_indexes if idx not in old_indexes]
1338 removed_indexes = [idx for idx in old_indexes if idx not in new_indexes]
1339 renamed_indexes = []
1340 # Find renamed indexes.
1341 remove_from_added = []
1342 remove_from_removed = []
1343 for new_index in added_indexes:
1344 new_index_dec = new_index.deconstruct()
1345 new_index_name = new_index_dec[2].pop("name")
1346 for old_index in removed_indexes:
1347 old_index_dec = old_index.deconstruct()
1348 old_index_name = old_index_dec[2].pop("name")
1349 # Indexes are the same except for the names.
1350 if (
1351 new_index_dec == old_index_dec
1352 and new_index_name != old_index_name
1353 ):
1354 renamed_indexes.append((old_index_name, new_index_name, None))
1355 remove_from_added.append(new_index)
1356 remove_from_removed.append(old_index)
1357 # Find index_together changed to indexes.
1358 for (
1359 old_value,
1360 new_value,
1361 index_together_app_label,
1362 index_together_model_name,
1363 dependencies,
1364 ) in self._get_altered_foo_together_operations(
1365 operations.AlterIndexTogether.option_name
1366 ):
1367 if (
1368 app_label != index_together_app_label
1369 or model_name != index_together_model_name
1370 ):
1371 continue
1372 removed_values = old_value.difference(new_value)
1373 for removed_index_together in removed_values:
1374 renamed_index_together_indexes = []
1375 for new_index in added_indexes:
1376 _, args, kwargs = new_index.deconstruct()
1377 # Ensure only 'fields' are defined in the Index.
1378 if (
1379 not args
1380 and new_index.fields == list(removed_index_together)
1381 and set(kwargs) == {"name", "fields"}
1382 ):
1383 renamed_index_together_indexes.append(new_index)
1384
1385 if len(renamed_index_together_indexes) == 1:
1386 renamed_index = renamed_index_together_indexes[0]
1387 remove_from_added.append(renamed_index)
1388 renamed_indexes.append(
1389 (None, renamed_index.name, removed_index_together)
1390 )
1391 self.renamed_index_together_values[
1392 index_together_app_label, index_together_model_name
1393 ].append(removed_index_together)
1394 # Remove renamed indexes from the lists of added and removed
1395 # indexes.
1396 added_indexes = [
1397 idx for idx in added_indexes if idx not in remove_from_added
1398 ]
1399 removed_indexes = [
1400 idx for idx in removed_indexes if idx not in remove_from_removed
1401 ]
1402
1403 self.altered_indexes.update(
1404 {
1405 (app_label, model_name): {
1406 "added_indexes": added_indexes,
1407 "removed_indexes": removed_indexes,
1408 "renamed_indexes": renamed_indexes,
1409 }
1410 }
1411 )
1412
1413 def generate_added_indexes(self):
1414 for (app_label, model_name), alt_indexes in self.altered_indexes.items():
1415 dependencies = self._get_dependencies_for_model(app_label, model_name)
1416 for index in alt_indexes["added_indexes"]:
1417 self.add_operation(
1418 app_label,
1419 operations.AddIndex(
1420 model_name=model_name,
1421 index=index,
1422 ),
1423 dependencies=dependencies,
1424 )
1425
1426 def generate_removed_indexes(self):
1427 for (app_label, model_name), alt_indexes in self.altered_indexes.items():
1428 for index in alt_indexes["removed_indexes"]:
1429 self.add_operation(
1430 app_label,
1431 operations.RemoveIndex(
1432 model_name=model_name,
1433 name=index.name,
1434 ),
1435 )
1436
1437 def generate_renamed_indexes(self):
1438 for (app_label, model_name), alt_indexes in self.altered_indexes.items():
1439 for old_index_name, new_index_name, old_fields in alt_indexes[
1440 "renamed_indexes"
1441 ]:
1442 self.add_operation(
1443 app_label,
1444 operations.RenameIndex(
1445 model_name=model_name,
1446 new_name=new_index_name,
1447 old_name=old_index_name,
1448 old_fields=old_fields,
1449 ),
1450 )
1451
1452 def _constraint_should_be_dropped_and_recreated(
1453 self, old_constraint, new_constraint
1454 ):
1455 old_path, old_args, old_kwargs = old_constraint.deconstruct()
1456 new_path, new_args, new_kwargs = new_constraint.deconstruct()
1457
1458 for attr in old_constraint.non_db_attrs:
1459 old_kwargs.pop(attr, None)
1460 for attr in new_constraint.non_db_attrs:
1461 new_kwargs.pop(attr, None)
1462
1463 return (old_path, old_args, old_kwargs) != (new_path, new_args, new_kwargs)
1464
1465 def create_altered_constraints(self):
1466 option_name = operations.AddConstraint.option_name
1467 for app_label, model_name in sorted(self.kept_model_keys):
1468 old_model_name = self.renamed_models.get(
1469 (app_label, model_name), model_name
1470 )
1471 old_model_state = self.from_state.models[app_label, old_model_name]
1472 new_model_state = self.to_state.models[app_label, model_name]
1473
1474 old_constraints = old_model_state.options[option_name]
1475 new_constraints = new_model_state.options[option_name]
1476
1477 alt_constraints = []
1478 alt_constraints_name = []
1479
1480 for old_c in old_constraints:
1481 for new_c in new_constraints:
1482 old_c_dec = old_c.deconstruct()
1483 new_c_dec = new_c.deconstruct()
1484 if (
1485 old_c_dec != new_c_dec
1486 and old_c.name == new_c.name
1487 and not self._constraint_should_be_dropped_and_recreated(
1488 old_c, new_c
1489 )
1490 ):
1491 alt_constraints.append(new_c)
1492 alt_constraints_name.append(new_c.name)
1493
1494 add_constraints = [
1495 c
1496 for c in new_constraints
1497 if c not in old_constraints and c.name not in alt_constraints_name
1498 ]
1499 rem_constraints = [
1500 c
1501 for c in old_constraints
1502 if c not in new_constraints and c.name not in alt_constraints_name
1503 ]
1504
1505 self.altered_constraints.update(
1506 {
1507 (app_label, model_name): {
1508 "added_constraints": add_constraints,
1509 "removed_constraints": rem_constraints,
1510 "altered_constraints": alt_constraints,
1511 }
1512 }
1513 )
1514
1515 def generate_added_constraints(self):
1516 for (
1517 app_label,
1518 model_name,
1519 ), alt_constraints in self.altered_constraints.items():
1520 dependencies = self._get_dependencies_for_model(app_label, model_name)
1521 for constraint in alt_constraints["added_constraints"]:
1522 self.add_operation(
1523 app_label,
1524 operations.AddConstraint(
1525 model_name=model_name,
1526 constraint=constraint,
1527 ),
1528 dependencies=dependencies,
1529 )
1530
1531 def generate_removed_constraints(self):
1532 for (
1533 app_label,
1534 model_name,
1535 ), alt_constraints in self.altered_constraints.items():
1536 for constraint in alt_constraints["removed_constraints"]:
1537 self.add_operation(
1538 app_label,
1539 operations.RemoveConstraint(
1540 model_name=model_name,
1541 name=constraint.name,
1542 ),
1543 )
1544
1545 def generate_altered_constraints(self):
1546 for (
1547 app_label,
1548 model_name,
1549 ), alt_constraints in self.altered_constraints.items():
1550 dependencies = self._get_dependencies_for_model(app_label, model_name)
1551 for constraint in alt_constraints["altered_constraints"]:
1552 self.add_operation(
1553 app_label,
1554 operations.AlterConstraint(
1555 model_name=model_name,
1556 name=constraint.name,
1557 constraint=constraint,
1558 ),
1559 dependencies=dependencies,
1560 )
1561
1562 @staticmethod
1563 def _get_dependencies_for_foreign_key(app_label, model_name, field, project_state):
1564 remote_field_model = None
1565 if hasattr(field.remote_field, "model"):
1566 remote_field_model = field.remote_field.model
1567 else:
1568 relations = project_state.relations[app_label, model_name]
1569 for (remote_app_label, remote_model_name), fields in relations.items():
1570 if any(
1571 field == related_field.remote_field
1572 for related_field in fields.values()
1573 ):
1574 remote_field_model = f"{remote_app_label}.{remote_model_name}"
1575 break
1576 # Account for FKs to swappable models
1577 swappable_setting = getattr(field, "swappable_setting", None)
1578 if swappable_setting is not None:
1579 dep_app_label = "__setting__"
1580 dep_object_name = swappable_setting
1581 else:
1582 dep_app_label, dep_object_name = resolve_relation(
1583 remote_field_model,
1584 app_label,
1585 model_name,
1586 )
1587 dependencies = [
1588 OperationDependency(
1589 dep_app_label, dep_object_name, None, OperationDependency.Type.CREATE
1590 )
1591 ]
1592 if getattr(field.remote_field, "through", None):
1593 through_app_label, through_object_name = resolve_relation(
1594 field.remote_field.through,
1595 app_label,
1596 model_name,
1597 )
1598 dependencies.append(
1599 OperationDependency(
1600 through_app_label,
1601 through_object_name,
1602 None,
1603 OperationDependency.Type.CREATE,
1604 )
1605 )
1606 return dependencies
1607
1608 def _get_dependencies_for_generated_field(self, field):
1609 dependencies = []
1610 referenced_base_fields = models.Q(field.expression).referenced_base_fields
1611 newly_added_fields = sorted(self.new_field_keys - self.old_field_keys)
1612 for app_label, model_name, added_field_name in newly_added_fields:
1613 added_field = self.to_state.models[app_label, model_name].get_field(
1614 added_field_name
1615 )
1616 if (
1617 added_field.remote_field and added_field.remote_field.model
1618 ) or added_field.name in referenced_base_fields:
1619 dependencies.append(
1620 OperationDependency(
1621 app_label,
1622 model_name,
1623 added_field.name,
1624 OperationDependency.Type.CREATE,
1625 )
1626 )
1627 return dependencies
1628
1629 def _get_dependencies_for_model(self, app_label, model_name):
1630 """Return foreign key dependencies of the given model."""
1631 dependencies = []
1632 model_state = self.to_state.models[app_label, model_name]
1633 for field in model_state.fields.values():
1634 if field.is_relation:
1635 dependencies.extend(
1636 self._get_dependencies_for_foreign_key(
1637 app_label,
1638 model_name,
1639 field,
1640 self.to_state,
1641 )
1642 )
1643 return dependencies
1644
1645 def _get_altered_foo_together_operations(self, option_name):
1646 for app_label, model_name in sorted(self.kept_model_keys):
1647 old_model_name = self.renamed_models.get(
1648 (app_label, model_name), model_name
1649 )
1650 old_model_state = self.from_state.models[app_label, old_model_name]
1651 new_model_state = self.to_state.models[app_label, model_name]
1652
1653 # We run the old version through the field renames to account for those
1654 old_value = old_model_state.options.get(option_name)
1655 old_value = (
1656 {
1657 tuple(
1658 self.renamed_fields.get((app_label, model_name, n), n)
1659 for n in unique
1660 )
1661 for unique in old_value
1662 }
1663 if old_value
1664 else set()
1665 )
1666
1667 new_value = new_model_state.options.get(option_name)
1668 new_value = set(new_value) if new_value else set()
1669
1670 if old_value != new_value:
1671 dependencies = []
1672 for foo_togethers in new_value:
1673 for field_name in foo_togethers:
1674 field = new_model_state.get_field(field_name)
1675 if field.remote_field and field.remote_field.model:
1676 dependencies.extend(
1677 self._get_dependencies_for_foreign_key(
1678 app_label,
1679 model_name,
1680 field,
1681 self.to_state,
1682 )
1683 )
1684 yield (
1685 old_value,
1686 new_value,
1687 app_label,
1688 model_name,
1689 dependencies,
1690 )
1691
1692 def _generate_removed_altered_foo_together(self, operation):
1693 for (
1694 old_value,
1695 new_value,
1696 app_label,
1697 model_name,
1698 dependencies,
1699 ) in self._get_altered_foo_together_operations(operation.option_name):
1700 if operation == operations.AlterIndexTogether:
1701 old_value = {
1702 value
1703 for value in old_value
1704 if value
1705 not in self.renamed_index_together_values[app_label, model_name]
1706 }
1707 removal_value = new_value.intersection(old_value)
1708 if removal_value or old_value:
1709 self.add_operation(
1710 app_label,
1711 operation(
1712 name=model_name, **{operation.option_name: removal_value}
1713 ),
1714 dependencies=dependencies,
1715 )
1716
1717 def generate_removed_altered_unique_together(self):
1718 self._generate_removed_altered_foo_together(operations.AlterUniqueTogether)
1719
1720 def _generate_altered_foo_together(self, operation):
1721 for (
1722 old_value,
1723 new_value,
1724 app_label,
1725 model_name,
1726 dependencies,
1727 ) in self._get_altered_foo_together_operations(operation.option_name):
1728 removal_value = new_value.intersection(old_value)
1729 if new_value != removal_value:
1730 self.add_operation(
1731 app_label,
1732 operation(name=model_name, **{operation.option_name: new_value}),
1733 dependencies=dependencies,
1734 )
1735
1736 def generate_altered_unique_together(self):
1737 self._generate_altered_foo_together(operations.AlterUniqueTogether)
1738
1739 def generate_altered_db_table(self):
1740 models_to_check = self.kept_model_keys.union(
1741 self.kept_proxy_keys, self.kept_unmanaged_keys
1742 )
1743 for app_label, model_name in sorted(models_to_check):
1744 old_model_name = self.renamed_models.get(
1745 (app_label, model_name), model_name
1746 )
1747 old_model_state = self.from_state.models[app_label, old_model_name]
1748 new_model_state = self.to_state.models[app_label, model_name]
1749 old_db_table_name = old_model_state.options.get("db_table")
1750 new_db_table_name = new_model_state.options.get("db_table")
1751 if old_db_table_name != new_db_table_name:
1752 self.add_operation(
1753 app_label,
1754 operations.AlterModelTable(
1755 name=model_name,
1756 table=new_db_table_name,
1757 ),
1758 )
1759
1760 def generate_altered_db_table_comment(self):
1761 models_to_check = self.kept_model_keys.union(
1762 self.kept_proxy_keys, self.kept_unmanaged_keys
1763 )
1764 for app_label, model_name in sorted(models_to_check):
1765 old_model_name = self.renamed_models.get(
1766 (app_label, model_name), model_name
1767 )
1768 old_model_state = self.from_state.models[app_label, old_model_name]
1769 new_model_state = self.to_state.models[app_label, model_name]
1770
1771 old_db_table_comment = old_model_state.options.get("db_table_comment")
1772 new_db_table_comment = new_model_state.options.get("db_table_comment")
1773 if old_db_table_comment != new_db_table_comment:
1774 self.add_operation(
1775 app_label,
1776 operations.AlterModelTableComment(
1777 name=model_name,
1778 table_comment=new_db_table_comment,
1779 ),
1780 )
1781
1782 def generate_altered_options(self):
1783 """
1784 Work out if any non-schema-affecting options have changed and make an
1785 operation to represent them in state changes (in case Python code in
1786 migrations needs them).
1787 """
1788 models_to_check = self.kept_model_keys.union(
1789 self.kept_proxy_keys,
1790 self.kept_unmanaged_keys,
1791 # unmanaged converted to managed
1792 self.old_unmanaged_keys & self.new_model_keys,
1793 # managed converted to unmanaged
1794 self.old_model_keys & self.new_unmanaged_keys,
1795 )
1796
1797 for app_label, model_name in sorted(models_to_check):
1798 old_model_name = self.renamed_models.get(
1799 (app_label, model_name), model_name
1800 )
1801 old_model_state = self.from_state.models[app_label, old_model_name]
1802 new_model_state = self.to_state.models[app_label, model_name]
1803 old_options = {
1804 key: value
1805 for key, value in old_model_state.options.items()
1806 if key in AlterModelOptions.ALTER_OPTION_KEYS
1807 }
1808 new_options = {
1809 key: value
1810 for key, value in new_model_state.options.items()
1811 if key in AlterModelOptions.ALTER_OPTION_KEYS
1812 }
1813 if old_options != new_options:
1814 self.add_operation(
1815 app_label,
1816 operations.AlterModelOptions(
1817 name=model_name,
1818 options=new_options,
1819 ),
1820 )
1821
1822 def generate_altered_order_with_respect_to(self):
1823 for app_label, model_name in sorted(self.kept_model_keys):
1824 old_model_name = self.renamed_models.get(
1825 (app_label, model_name), model_name
1826 )
1827 old_model_state = self.from_state.models[app_label, old_model_name]
1828 new_model_state = self.to_state.models[app_label, model_name]
1829 if old_model_state.options.get(
1830 "order_with_respect_to"
1831 ) != new_model_state.options.get("order_with_respect_to"):
1832 # Make sure it comes second if we're adding
1833 # (removal dependency is part of RemoveField)
1834 dependencies = []
1835 if new_model_state.options.get("order_with_respect_to"):
1836 dependencies.append(
1837 OperationDependency(
1838 app_label,
1839 model_name,
1840 new_model_state.options["order_with_respect_to"],
1841 OperationDependency.Type.CREATE,
1842 )
1843 )
1844 # Actually generate the operation
1845 self.add_operation(
1846 app_label,
1847 operations.AlterOrderWithRespectTo(
1848 name=model_name,
1849 order_with_respect_to=new_model_state.options.get(
1850 "order_with_respect_to"
1851 ),
1852 ),
1853 dependencies=dependencies,
1854 )
1855
1856 def generate_altered_managers(self):
1857 for app_label, model_name in sorted(self.kept_model_keys):
1858 old_model_name = self.renamed_models.get(
1859 (app_label, model_name), model_name
1860 )
1861 old_model_state = self.from_state.models[app_label, old_model_name]
1862 new_model_state = self.to_state.models[app_label, model_name]
1863 if old_model_state.managers != new_model_state.managers:
1864 self.add_operation(
1865 app_label,
1866 operations.AlterModelManagers(
1867 name=model_name,
1868 managers=new_model_state.managers,
1869 ),
1870 )
1871
1872 def arrange_for_graph(self, changes, graph, migration_name=None):
1873 """
1874 Take a result from changes() and a MigrationGraph, and fix the names
1875 and dependencies of the changes so they extend the graph from the leaf
1876 nodes for each app.
1877 """
1878 leaves = graph.leaf_nodes()
1879 name_map = {}
1880 for app_label, migrations in list(changes.items()):
1881 if not migrations:
1882 continue
1883 # Find the app label's current leaf node
1884 app_leaf = None
1885 for leaf in leaves:
1886 if leaf[0] == app_label:
1887 app_leaf = leaf
1888 break
1889 # Do they want an initial migration for this app?
1890 if app_leaf is None and not self.questioner.ask_initial(app_label):
1891 # They don't.
1892 for migration in migrations:
1893 name_map[(app_label, migration.name)] = (app_label, "__first__")
1894 del changes[app_label]
1895 continue
1896 # Work out the next number in the sequence
1897 if app_leaf is None:
1898 next_number = 1
1899 else:
1900 next_number = (self.parse_number(app_leaf[1]) or 0) + 1
1901 # Name each migration
1902 for i, migration in enumerate(migrations):
1903 if i == 0 and app_leaf:
1904 migration.dependencies.append(app_leaf)
1905 new_name_parts = ["%04i" % next_number]
1906 if migration_name:
1907 new_name_parts.append(migration_name)
1908 elif i == 0 and not app_leaf:
1909 new_name_parts.append("initial")
1910 else:
1911 new_name_parts.append(migration.suggest_name()[:100])
1912 new_name = "_".join(new_name_parts)
1913 name_map[(app_label, migration.name)] = (app_label, new_name)
1914 next_number += 1
1915 migration.name = new_name
1916 # Now fix dependencies
1917 for migrations in changes.values():
1918 for migration in migrations:
1919 migration.dependencies = [
1920 name_map.get(d, d) for d in migration.dependencies
1921 ]
1922 return changes
1923
1924 def _trim_to_apps(self, changes, app_labels):
1925 """
1926 Take changes from arrange_for_graph() and set of app labels, and return
1927 a modified set of changes which trims out as many migrations that are
1928 not in app_labels as possible. Note that some other migrations may
1929 still be present as they may be required dependencies.
1930 """
1931 # Gather other app dependencies in a first pass
1932 app_dependencies = {}
1933 for app_label, migrations in changes.items():
1934 for migration in migrations:
1935 for dep_app_label, name in migration.dependencies:
1936 app_dependencies.setdefault(app_label, set()).add(dep_app_label)
1937 required_apps = set(app_labels)
1938 # Keep resolving till there's no change
1939 old_required_apps = None
1940 while old_required_apps != required_apps:
1941 old_required_apps = set(required_apps)
1942 required_apps.update(
1943 *[app_dependencies.get(app_label, ()) for app_label in required_apps]
1944 )
1945 # Remove all migrations that aren't needed
1946 for app_label in list(changes):
1947 if app_label not in required_apps:
1948 del changes[app_label]
1949 return changes
1950
1951 @classmethod
1952 def parse_number(cls, name):
1953 """
1954 Given a migration name, try to extract a number from the beginning of
1955 it. For a squashed migration such as '0001_squashed_0004…', return the
1956 second number. If no number is found, return None.
1957 """
1958 if squashed_match := re.search(r".*_squashed_(\d+)", name):
1959 return int(squashed_match[1])
1960 match = re.match(r"^\d+", name)
1961 if match:
1962 return int(match[0])
1963 return None