class documentation

class MigrationAutodetector: (source)

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. For a squashed migration such as '0001_squashed_0004…', return the second number. If no number is found, return None.
Method __init__ Undocumented
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 create_renamed_fields Work out renamed fields.
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 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).
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 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).
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 Generate RenameField operations.
Method generate_renamed_indexes Undocumented
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_index_together_values Undocumented
Instance Variable renamed_models Undocumented
Instance Variable renamed_models_rel Undocumented
Instance Variable renamed_operations Undocumented
Instance Variable through_users Undocumented
Instance Variable to_state Undocumented
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 _build_migration_list 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)...
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 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.
@classmethod
def parse_number(cls, name): (source)

Given a migration name, try to extract a number from the beginning of it. For a squashed migration such as '0001_squashed_0004…', return the second number. If no number is found, return None.

def __init__(self, from_state, to_state, questioner=None): (source)

Undocumented

def add_operation(self, app_label, operation, dependencies=None, beginning=False): (source)

Undocumented

def arrange_for_graph(self, changes, graph, migration_name=None): (source)

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): (source)

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): (source)

Return True if the given operation depends on the given dependency, False otherwise.

def create_altered_constraints(self): (source)

Undocumented

def create_altered_indexes(self): (source)

Undocumented

def create_renamed_fields(self): (source)

Work out renamed fields.

def deep_deconstruct(self, obj): (source)

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): (source)

Undocumented

def generate_added_fields(self): (source)

Make AddField operations.

def generate_added_indexes(self): (source)

Undocumented

def generate_altered_db_table(self): (source)

Undocumented

def generate_altered_fields(self): (source)

Make AlterField operations, or possibly RemovedField/AddField if alter isn't possible.

def generate_altered_index_together(self): (source)

Undocumented

def generate_altered_managers(self): (source)

Undocumented

def generate_altered_options(self): (source)

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): (source)

Undocumented

def generate_altered_unique_together(self): (source)

Undocumented

def generate_created_models(self): (source)

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): (source)

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): (source)

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): (source)

Make DeleteModel options for proxy models.

def generate_removed_altered_index_together(self): (source)

Undocumented

def generate_removed_altered_unique_together(self): (source)

Undocumented

def generate_removed_constraints(self): (source)

Undocumented

def generate_removed_fields(self): (source)

Make RemoveField operations.

def generate_removed_indexes(self): (source)

Undocumented

def generate_renamed_fields(self): (source)

Generate RenameField operations.

def generate_renamed_indexes(self): (source)

Undocumented

def generate_renamed_models(self): (source)

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): (source)

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): (source)

Place potential swappable models first in lists of created models (only real way to solve #22783).

altered_constraints: dict = (source)

Undocumented

altered_indexes: dict = (source)

Undocumented

existing_apps = (source)

Undocumented

from_state = (source)

Undocumented

generated_operations: dict = (source)

Undocumented

kept_model_keys = (source)

Undocumented

kept_proxy_keys = (source)

Undocumented

kept_unmanaged_keys = (source)

Undocumented

migrations: dict = (source)

Undocumented

new_field_keys = (source)

Undocumented

new_model_keys = (source)

Undocumented

new_proxy_keys = (source)

Undocumented

new_unmanaged_keys = (source)

Undocumented

old_field_keys = (source)

Undocumented

old_model_keys = (source)

Undocumented

old_proxy_keys = (source)

Undocumented

old_unmanaged_keys = (source)

Undocumented

questioner = (source)

Undocumented

renamed_fields: dict = (source)

Undocumented

renamed_index_together_values = (source)

Undocumented

renamed_models: dict = (source)

Undocumented

renamed_models_rel: dict = (source)

Undocumented

renamed_operations: list = (source)

Undocumented

through_users: dict = (source)

Undocumented

to_state = (source)

Undocumented

@staticmethod
def _get_dependencies_for_foreign_key(app_label, model_name, field, project_state): (source)

Undocumented

@staticmethod
def _resolve_dependency(dependency): (source)

Return the resolved dependency and a boolean denoting whether or not it was swappable.

def _build_migration_list(self, graph=None): (source)

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): (source)

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): (source)

Undocumented

def _generate_altered_foo_together(self, operation): (source)

Undocumented

def _generate_removed_altered_foo_together(self, operation): (source)

Undocumented

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

Undocumented

def _generate_through_model_map(self): (source)

Through model map generation.

def _get_altered_foo_together_operations(self, option_name): (source)

Undocumented

def _optimize_migrations(self): (source)

Undocumented

def _prepare_field_lists(self): (source)

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): (source)

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): (source)

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.