def generate_altered_index_together(self): for app_label, model_name in sorted(self.kept_model_keys): old_model_name = self.renamed_models.get((app_label, model_name), model_name) old_model_state = self.from_state.models[app_label, old_model_name] new_model_state = self.to_state.models[app_label, model_name] if old_model_state.options.get( "index_together", None) != new_model_state.options.get( "index_together", None): self.add_operation( app_label, operations.AlterIndexTogether( name=model_name, index_together=new_model_state. options['index_together'], ))
def generate_deleted_models(self): """ Find all deleted models and make creation operations for them, and separate operations to delete any foreign key or M2M relationships (we'll optimise these back in later if we can) We also bring forward removal of any model options that refer to collections of fields - the inverse of generate_created_models. """ deleted_models = set(self.old_model_keys) - set(self.new_model_keys) for app_label, model_name in sorted(deleted_models): model_state = self.from_state.models[app_label, model_name] model = self.old_apps.get_model(app_label, model_name) # Gather related fields related_fields = {} for field in model._meta.local_fields: if field.rel: if field.rel.to: related_fields[field.name] = field if hasattr(field.rel, "through" ) and not field.rel.through._meta.auto_created: related_fields[field.name] = field for field in model._meta.local_many_to_many: if field.rel.to: related_fields[field.name] = field if hasattr(field.rel, "through" ) and not field.rel.through._meta.auto_created: related_fields[field.name] = field # Generate option removal first unique_together = model_state.options.pop('unique_together', None) index_together = model_state.options.pop('index_together', None) if unique_together: self.add_operation( app_label, operations.AlterUniqueTogether( name=model_name, unique_together=None, )) if index_together: self.add_operation( app_label, operations.AlterIndexTogether( name=model_name, index_together=None, )) # Then remove each related field for name, field in sorted(related_fields.items()): self.add_operation( app_label, operations.RemoveField( model_name=model_name, name=name, )) # Finally, remove the model. # This depends on both the removal of all incoming fields # and the removal of all its own related fields, and if it's # a through model the field that references it. dependencies = [] for related_object in model._meta.get_all_related_objects(): dependencies.append(( related_object.model._meta.app_label, related_object.model._meta.object_name, related_object.field.name, False, )) for related_object in model._meta.get_all_related_many_to_many_objects( ): dependencies.append(( related_object.model._meta.app_label, related_object.model._meta.object_name, related_object.field.name, False, )) for name, field in sorted(related_fields.items()): dependencies.append((app_label, model_name, name, False)) # We're referenced in another field's through= through_user = self.through_users.get( (app_label, model_state.name.lower()), None) if through_user: dependencies.append( (through_user[0], through_user[1], through_user[2], False)) # Finally, make the operation, deduping any dependencies self.add_operation( app_label, operations.DeleteModel(name=model_state.name, ), dependencies=list(set(dependencies)), )
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 (we'll optimize these back in later if we can). We also defer any model options that refer to collections of fields that might be deferred (e.g. unique_together, index_together). """ old_keys = set(self.old_model_keys).union(self.old_unmanaged_keys) added_models = set(self.new_model_keys) - old_keys added_unmanaged_models = set(self.new_unmanaged_keys) - old_keys all_added_models = chain( sorted(added_models, key=self.swappable_first_key, reverse=True), sorted(added_unmanaged_models, key=self.swappable_first_key, reverse=True)) for app_label, model_name in all_added_models: model_state = self.to_state.models[app_label, model_name] model_opts = self.new_apps.get_model(app_label, model_name)._meta # CHANGED # < # Gather related fields # < related_fields = {} # < primary_key_rel = None # < for field in model_opts.local_fields: # < if field.remote_field: # < if field.remote_field.model: # < if field.primary_key: # < primary_key_rel = field.remote_field.model # < elif not field.remote_field.parent_link: # < related_fields[field.name] = field # < # through will be none on M2Ms on swapped-out models; # < # we can treat lack of through as auto_created=True, though. # < if (getattr(field.remote_field, "through", None) and # < not field.remote_field.through._meta.auto_created): # < related_fields[field.name] = field # < for field in model_opts.local_many_to_many: # < if field.remote_field.model: # < related_fields[field.name] = field # < if getattr(field.remote_field, "through", None) and not field.remote_field.through._meta.auto_created: # < related_fields[field.name] = field # Generate dict of fields with dependencies (excluding primary key) dependent_fields = { field.name: field for field in chain(model_opts.local_fields, model_opts.local_many_to_many) if field.has_dependencies and not field.primary_key } # XXX # Are there indexes/unique|index_together to defer? indexes = model_state.options.pop('indexes') unique_together = model_state.options.pop('unique_together', None) index_together = model_state.options.pop('index_together', None) order_with_respect_to = model_state.options.pop( 'order_with_respect_to', None) # Depend on the deletion of any possible proxy version of us dependencies = [ # CHANGED # < (app_label, model_name, None, False), dependency_tuple(app_label=app_label, object_name=model_name, field=None, created=False), # XXX ] # Depend on all bases for base in model_state.bases: if isinstance(base, str) and "." in base: base_app_label, base_name = base.split(".", 1) # CHANGED # < dependencies.append((base_app_label, base_name, None, True)) dependencies.append( dependency_tuple(base_app_label, base_name, None, True)) # XXX # Depend on the other end of the primary key if it's a relation # CHANGED # < if primary_key_rel: # < dependencies.append(( # < primary_key_rel._meta.app_label, # < primary_key_rel._meta.object_name, # < None, # < True # < )) if model_opts.pk.remote_field: dependencies.append( dependency_tuple(app_label=model_opts.pk.remote_field. model._meta.app_label, object_name=model_opts.pk.remote_field. model._meta.object_name, field=None, created=True)) # XXX # Generate creation operation self.add_operation( app_label, operations.CreateModel( name=model_state.name, # CHANGED # < fields=[d for d in model_state.fields if d[0] not in related_fields], fields=[ d for d in model_state.fields if d[0] not in dependent_fields ], # XXX options=model_state.options, bases=model_state.bases, managers=model_state.managers, ), dependencies=dependencies, beginning=True, ) # Don't add operations which modify the database for unmanaged models if not model_opts.managed: continue # CHANGED # Dependency for this model model_dependency = dependency_tuple(app_label=app_label, object_name=model_name, field=None, created=True) # < # Generate operations for each related field # < for name, field in sorted(related_fields.items()): # < dependencies = self._get_dependencies_for_foreign_key(field) # < # Depend on our own model being created # < dependencies.append((app_label, model_name, None, True)) # < # Make operation # < self.add_operation( # < app_label, # < operations.AddField( # < model_name=model_name, # < name=name, # < field=field, # < ), # < dependencies=list(set(dependencies)), # < ) # Generate operations for each dependent field for name, field in sorted(dependent_fields.items()): # Make operation self.add_operation(app_label, operations.AddField(model_name=model_name, name=name, field=field), dependencies=list( set(field.dependencies + [model_dependency]))) # < # Generate other opns # < related_dependencies = [ # < (app_label, model_name, name, True) # < for name, field in sorted(related_fields.items()) # < ] # Generate a dependency for each field with dependencies (as any may be deferred) field_dependencies = [ dependency_tuple(app_label=app_label, object_name=model_name, field=name, created=True) for name, field in sorted(dependent_fields.items()) ] # < related_dependencies.append((app_label, model_name, None, True)) field_dependencies.append(model_dependency) # XXX for index in indexes: self.add_operation( app_label, operations.AddIndex( model_name=model_name, index=index, ), # CHANGED # < dependencies=related_dependencies dependencies=field_dependencies, # XXX ) if unique_together: self.add_operation( app_label, operations.AlterUniqueTogether( name=model_name, unique_together=unique_together, ), # CHANGED # < dependencies=related_dependencies dependencies=field_dependencies, # XXX ) if index_together: self.add_operation( app_label, operations.AlterIndexTogether( name=model_name, index_together=index_together, ), # CHANGED # < dependencies=related_dependencies dependencies=field_dependencies, # XXX ) if order_with_respect_to: self.add_operation( app_label, operations.AlterOrderWithRespectTo( name=model_name, order_with_respect_to=order_with_respect_to, ), dependencies=[ # CHANGED # < (app_label, model_name, order_with_respect_to, True), # < (app_label, model_name, None, True), dependency_tuple(app_label, model_name, order_with_respect_to, True), model_dependency, # XXX ]) # Fix relationships if the model changed from a proxy model to a # concrete model. if (app_label, model_name) in self.old_proxy_keys: for related_object in model_opts.related_objects: self.add_operation( related_object.related_model._meta.app_label, operations.AlterField( model_name=related_object.related_model._meta. object_name, name=related_object.field.name, field=related_object.field, ), # CHANGED # < dependencies=[(app_label, model_name, None, True)], dependencies=[model_dependency], # XXX )
def generate_created_models(self): """ Find all new models and make creation operations for them, and separate operations to create any foreign key or M2M relationships (we'll optimise these back in later if we can) We also defer any model options that refer to collections of fields that might be deferred (e.g. unique_together, index_together) """ added_models = set(self.new_model_keys) - set(self.old_model_keys) for app_label, model_name in sorted(added_models): model_state = self.to_state.models[app_label, model_name] # Gather related fields related_fields = {} for field in self.new_apps.get_model( app_label, model_name)._meta.local_fields: if field.rel: if field.rel.to: related_fields[field.name] = field if hasattr(field.rel, "through" ) and not field.rel.through._meta.auto_created: related_fields[field.name] = field for field in self.new_apps.get_model( app_label, model_name)._meta.local_many_to_many: if field.rel.to: related_fields[field.name] = field if hasattr(field.rel, "through" ) and not field.rel.through._meta.auto_created: related_fields[field.name] = field # Are there unique/index_together to defer? unique_together = model_state.options.pop('unique_together', None) index_together = model_state.options.pop('index_together', None) # Generate creation operatoin self.add_operation( app_label, operations.CreateModel( name=model_state.name, fields=[ d for d in model_state.fields if d[0] not in related_fields ], options=model_state.options, bases=model_state.bases, )) # Generate operations for each related field for name, field in sorted(related_fields.items()): # Account for FKs to swappable models swappable_setting = getattr(field, 'swappable_setting', None) if swappable_setting is not None: dep_app_label = "__setting__" dep_object_name = swappable_setting else: dep_app_label = field.rel.to._meta.app_label dep_object_name = field.rel.to._meta.object_name dependencies = [(dep_app_label, dep_object_name, None, True)] if getattr(field.rel, "through", None) and not field.rel.through._meta.auto_created: dependencies.append( (field.rel.through._meta.app_label, field.rel.through._meta.object_name, None, True)) # Make operation self.add_operation( app_label, operations.AddField( model_name=model_name, name=name, field=field, ), dependencies=list(set(dependencies)), ) # Generate other opns if unique_together: self.add_operation( app_label, operations.AlterUniqueTogether( name=model_name, unique_together=unique_together, ), dependencies=[ (app_label, model_name, name, True) for name, field in sorted(related_fields.items()) ]) if index_together: self.add_operation( app_label, operations.AlterIndexTogether( name=model_name, index_together=index_together, ), dependencies=[ (app_label, model_name, name, True) for name, field in sorted(related_fields.items()) ])
def generate_created_models(self): """ Find all new models and make creation operations for them, and separate operations to create any foreign key or M2M relationships (we'll optimise these back in later if we can) We also defer any model options that refer to collections of fields that might be deferred (e.g. unique_together, index_together) """ added_models = set(self.new_model_keys) - set(self.old_model_keys) for app_label, model_name in sorted(added_models, key=self.swappable_first_key): model_state = self.to_state.models[app_label, model_name] # Gather related fields related_fields = {} primary_key_rel = None for field in self.new_apps.get_model(app_label, model_name)._meta.local_fields: if field.rel: if field.rel.to: if field.primary_key: primary_key_rel = field.rel.to else: related_fields[field.name] = field # through will be none on M2Ms on swapped-out models; # we can treat lack of through as auto_created=True, though. if getattr(field.rel, "through", None) and not field.rel.through._meta.auto_created: related_fields[field.name] = field for field in self.new_apps.get_model(app_label, model_name)._meta.local_many_to_many: if field.rel.to: related_fields[field.name] = field if getattr(field.rel, "through", None) and not field.rel.through._meta.auto_created: related_fields[field.name] = field # Are there unique/index_together to defer? unique_together = model_state.options.pop('unique_together', None) index_together = model_state.options.pop('index_together', None) order_with_respect_to = model_state.options.pop('order_with_respect_to', None) # Depend on the deletion of any possible proxy version of us dependencies = [ (app_label, model_name, None, False), ] # Depend on all bases for base in model_state.bases: if isinstance(base, six.string_types) and "." in base: base_app_label, base_name = base.split(".", 1) dependencies.append((base_app_label, base_name, None, True)) # Depend on the other end of the primary key if it's a relation if primary_key_rel: dependencies.append(( primary_key_rel._meta.app_label, primary_key_rel._meta.object_name, None, True )) # Generate creation operation self.add_operation( app_label, operations.CreateModel( name=model_state.name, fields=[d for d in model_state.fields if d[0] not in related_fields], options=model_state.options, bases=model_state.bases, ), dependencies=dependencies, ) # Generate operations for each related field for name, field in sorted(related_fields.items()): # Account for FKs to swappable models swappable_setting = getattr(field, 'swappable_setting', None) if swappable_setting is not None: dep_app_label = "__setting__" dep_object_name = swappable_setting else: dep_app_label = field.rel.to._meta.app_label dep_object_name = field.rel.to._meta.object_name dependencies = [(dep_app_label, dep_object_name, None, True)] if getattr(field.rel, "through", None) and not field.rel.through._meta.auto_created: dependencies.append(( field.rel.through._meta.app_label, field.rel.through._meta.object_name, None, True )) # Depend on our own model being created dependencies.append((app_label, model_name, None, True)) # Make operation self.add_operation( app_label, operations.AddField( model_name=model_name, name=name, field=field, ), dependencies=list(set(dependencies)), ) # Generate other opns related_dependencies = [ (app_label, model_name, name, True) for name, field in sorted(related_fields.items()) ] related_dependencies.append((app_label, model_name, None, True)) if unique_together: self.add_operation( app_label, operations.AlterUniqueTogether( name=model_name, unique_together=unique_together, ), dependencies=related_dependencies ) if index_together: self.add_operation( app_label, operations.AlterIndexTogether( name=model_name, index_together=index_together, ), dependencies=related_dependencies ) if order_with_respect_to: self.add_operation( app_label, operations.AlterOrderWithRespectTo( name=model_name, order_with_respect_to=order_with_respect_to, ), dependencies=[ (app_label, model_name, order_with_respect_to, True), (app_label, model_name, None, True), ] )