class documentation

class MigrationAutodetector:

View In Hierarchy

Take a pair of ProjectStates and compare them to see what the first would need doing to make it match the second (the second usually being the project's current state).

Note that this naturally operates on entire projects at a time, as it's likely that changes interact (for example, you can't add a ForeignKey without having a migration to add the table it depends on first). A user interface may offer single-app usage if it wishes, with the caveat that it may not always be possible.

Class Method parse​_number Given a migration name, try to extract a number from the beginning of it. If no number is found, return None.
Static Method ​_get​_dependencies​_for​_foreign​_key Undocumented
Static Method ​_resolve​_dependency Return the resolved dependency and a boolean denoting whether or not it was swappable.
Method __init__ Undocumented
Method ​_build​_migration​_list No summary
Method ​_detect​_changes Return a dict of migration plans which will achieve the change from from_state to to_state. The dict has app labels as keys and a list of migrations as values.
Method ​_generate​_added​_field Undocumented
Method ​_generate​_altered​_foo​_together Undocumented
Method ​_generate​_removed​_altered​_foo​_together Undocumented
Method ​_generate​_removed​_field Undocumented
Method ​_generate​_through​_model​_map Through model map generation.
Method ​_get​_altered​_foo​_together​_operations Undocumented
Method ​_optimize​_migrations Undocumented
Method ​_prepare​_field​_lists Prepare field lists and a list of the fields that used through models in the old state so dependencies can be made from the through model deletion to the field that uses it.
Method ​_sort​_migrations Reorder to make things possible. Reordering may be needed so FKs work nicely inside the same app.
Method ​_trim​_to​_apps No summary
Method add​_operation Undocumented
Method arrange​_for​_graph Take a result from changes() and a MigrationGraph, and fix the names and dependencies of the changes so they extend the graph from the leaf nodes for each app.
Method changes Main entry point to produce a list of applicable changes. Take a graph to base names on and an optional set of apps to try and restrict to (restriction is not guaranteed)
Method check​_dependency Return True if the given operation depends on the given dependency, False otherwise.
Method create​_altered​_constraints Undocumented
Method create​_altered​_indexes Undocumented
Method deep​_deconstruct Recursive deconstruction for a field and its arguments. Used for full comparison for rename/alter; sometimes a single-level deconstruction will not compare correctly.
Method generate​_added​_constraints Undocumented
Method generate​_added​_fields Make AddField operations.
Method generate​_added​_indexes Undocumented
Method generate​_altered​_db​_table Undocumented
Method generate​_altered​_fields Make AlterField operations, or possibly RemovedField/AddField if alter isn't possible.
Method generate​_altered​_index​_together Undocumented
Method generate​_altered​_managers Undocumented
Method generate​_altered​_options Work out if any non-schema-affecting options have changed and make an operation to represent them in state changes (in case Python code in migrations needs them).
Method generate​_altered​_order​_with​_respect​_to Undocumented
Method generate​_altered​_unique​_together Undocumented
Method generate​_created​_models No summary
Method generate​_created​_proxies Make CreateModel statements for proxy models. Use the same statements as that way there's less code duplication, but for proxy models it's safe to skip all the pointless field stuff and chuck out an operation.
Method generate​_deleted​_models No summary
Method generate​_deleted​_proxies Make DeleteModel options for proxy models.
Method generate​_removed​_altered​_index​_together Undocumented
Method generate​_removed​_altered​_unique​_together Undocumented
Method generate​_removed​_constraints Undocumented
Method generate​_removed​_fields Make RemoveField operations.
Method generate​_removed​_indexes Undocumented
Method generate​_renamed​_fields Work out renamed fields.
Method generate​_renamed​_models Find any renamed models, generate the operations for them, and remove the old entry from the model lists. Must be run before other model-level generation.
Method only​_relation​_agnostic​_fields Return a definition of the fields that ignores field names and what related fields actually relate to. Used for detecting renames (as the related fields change during renames).
Method swappable​_first​_key Place potential swappable models first in lists of created models (only real way to solve #22783).
Instance Variable altered​_constraints Undocumented
Instance Variable altered​_indexes Undocumented
Instance Variable existing​_apps Undocumented
Instance Variable from​_state Undocumented
Instance Variable generated​_operations Undocumented
Instance Variable kept​_model​_keys Undocumented
Instance Variable kept​_proxy​_keys Undocumented
Instance Variable kept​_unmanaged​_keys Undocumented
Instance Variable migrations Undocumented
Instance Variable new​_field​_keys Undocumented
Instance Variable new​_model​_keys Undocumented
Instance Variable new​_proxy​_keys Undocumented
Instance Variable new​_unmanaged​_keys Undocumented
Instance Variable old​_field​_keys Undocumented
Instance Variable old​_model​_keys Undocumented
Instance Variable old​_proxy​_keys Undocumented
Instance Variable old​_unmanaged​_keys Undocumented
Instance Variable questioner Undocumented
Instance Variable renamed​_fields Undocumented
Instance Variable renamed​_models Undocumented
Instance Variable renamed​_models​_rel Undocumented
Instance Variable through​_users Undocumented
Instance Variable to​_state Undocumented
@classmethod
def parse_number(cls, name):
Given a migration name, try to extract a number from the beginning of it. If no number is found, return None.
@staticmethod
def _get_dependencies_for_foreign_key(app_label, model_name, field, project_state):

Undocumented

@staticmethod
def _resolve_dependency(dependency):
Return the resolved dependency and a boolean denoting whether or not it was swappable.
def __init__(self, from_state, to_state, questioner=None):

Undocumented

def _build_migration_list(self, graph=None):
Chop the lists of operations up into migrations with dependencies on each other. Do this by going through an app's list of operations until one is found that has an outgoing dependency that isn't in another app's migration yet (hasn't been chopped off its list). Then chop off the operations before it into a migration and move onto the next app. If the loops completes without doing anything, there's a circular dependency (which _should_ be impossible as the operations are all split at this point so they can't depend and be depended on).
def _detect_changes(self, convert_apps=None, graph=None):

Return a dict of migration plans which will achieve the change from from_state to to_state. The dict has app labels as keys and a list of migrations as values.

The resulting migrations aren't specially named, but the names do matter for dependencies inside the set.

convert_apps is the list of apps to convert to use migrations (i.e. to make initial migrations for, in the usual case)

graph is an optional argument that, if provided, can help improve dependency generation and avoid potential circular dependencies.

def _generate_added_field(self, app_label, model_name, field_name):

Undocumented

def _generate_altered_foo_together(self, operation):

Undocumented

def _generate_removed_altered_foo_together(self, operation):

Undocumented

def _generate_removed_field(self, app_label, model_name, field_name):

Undocumented

def _generate_through_model_map(self):
Through model map generation.
def _get_altered_foo_together_operations(self, option_name):

Undocumented

def _optimize_migrations(self):

Undocumented

def _prepare_field_lists(self):
Prepare field lists and a list of the fields that used through models in the old state so dependencies can be made from the through model deletion to the field that uses it.
def _sort_migrations(self):
Reorder to make things possible. Reordering may be needed so FKs work nicely inside the same app.
def _trim_to_apps(self, changes, app_labels):
Take changes from arrange_for_graph() and set of app labels, and return a modified set of changes which trims out as many migrations that are not in app_labels as possible. Note that some other migrations may still be present as they may be required dependencies.
def add_operation(self, app_label, operation, dependencies=None, beginning=False):

Undocumented

def arrange_for_graph(self, changes, graph, migration_name=None):
Take a result from changes() and a MigrationGraph, and fix the names and dependencies of the changes so they extend the graph from the leaf nodes for each app.
def changes(self, graph, trim_to_apps=None, convert_apps=None, migration_name=None):
Main entry point to produce a list of applicable changes. Take a graph to base names on and an optional set of apps to try and restrict to (restriction is not guaranteed)
def check_dependency(self, operation, dependency):
Return True if the given operation depends on the given dependency, False otherwise.
def create_altered_constraints(self):

Undocumented

def create_altered_indexes(self):

Undocumented

def deep_deconstruct(self, obj):
Recursive deconstruction for a field and its arguments. Used for full comparison for rename/alter; sometimes a single-level deconstruction will not compare correctly.
def generate_added_constraints(self):

Undocumented

def generate_added_fields(self):
Make AddField operations.
def generate_added_indexes(self):

Undocumented

def generate_altered_db_table(self):

Undocumented

def generate_altered_fields(self):
Make AlterField operations, or possibly RemovedField/AddField if alter isn't possible.
def generate_altered_index_together(self):

Undocumented

def generate_altered_managers(self):

Undocumented

def generate_altered_options(self):
Work out if any non-schema-affecting options have changed and make an operation to represent them in state changes (in case Python code in migrations needs them).
def generate_altered_order_with_respect_to(self):

Undocumented

def generate_altered_unique_together(self):

Undocumented

def generate_created_models(self):

Find all new models (both managed and unmanaged) and make create operations for them as well as separate operations to create any foreign key or M2M relationships (these are optimized later, if possible).

Defer any model options that refer to collections of fields that might be deferred (e.g. unique_together, index_together).

def generate_created_proxies(self):
Make CreateModel statements for proxy models. Use the same statements as that way there's less code duplication, but for proxy models it's safe to skip all the pointless field stuff and chuck out an operation.
def generate_deleted_models(self):

Find all deleted models (managed and unmanaged) and make delete operations for them as well as separate operations to delete any foreign key or M2M relationships (these are optimized later, if possible).

Also bring forward removal of any model options that refer to collections of fields - the inverse of generate_created_models().

def generate_deleted_proxies(self):
Make DeleteModel options for proxy models.
def generate_removed_altered_index_together(self):

Undocumented

def generate_removed_altered_unique_together(self):

Undocumented

def generate_removed_constraints(self):

Undocumented

def generate_removed_fields(self):
Make RemoveField operations.
def generate_removed_indexes(self):

Undocumented

def generate_renamed_fields(self):
Work out renamed fields.
def generate_renamed_models(self):
Find any renamed models, generate the operations for them, and remove the old entry from the model lists. Must be run before other model-level generation.
def only_relation_agnostic_fields(self, fields):
Return a definition of the fields that ignores field names and what related fields actually relate to. Used for detecting renames (as the related fields change during renames).
def swappable_first_key(self, item):
Place potential swappable models first in lists of created models (only real way to solve #22783).
altered_constraints: dict =

Undocumented

altered_indexes: dict =

Undocumented

existing_apps =

Undocumented

from_state =

Undocumented

generated_operations: dict =

Undocumented

kept_model_keys =

Undocumented

kept_proxy_keys =

Undocumented

kept_unmanaged_keys =

Undocumented

migrations: dict =

Undocumented

new_field_keys =

Undocumented

new_model_keys: set =

Undocumented

new_proxy_keys: set =

Undocumented

new_unmanaged_keys: set =

Undocumented

old_field_keys =

Undocumented

old_model_keys: set =

Undocumented

old_proxy_keys: set =

Undocumented

old_unmanaged_keys: set =

Undocumented

questioner =

Undocumented

renamed_fields: dict =

Undocumented

renamed_models: dict =

Undocumented

renamed_models_rel: dict =

Undocumented

through_users: dict =

Undocumented

to_state =

Undocumented