mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #31468 -- Allowed specifying migration filename in Operation.
This adds also suggested filename for many built-in operations.
This commit is contained in:
committed by
Mariusz Felisiak
parent
5bd585a82d
commit
fa58450a9a
@@ -1313,19 +1313,14 @@ class MigrationAutodetector:
|
||||
represent. Names are not guaranteed to be unique, but put some effort
|
||||
into the fallback name to avoid VCS conflicts if possible.
|
||||
"""
|
||||
name = None
|
||||
if len(ops) == 1:
|
||||
if isinstance(ops[0], operations.CreateModel):
|
||||
return ops[0].name_lower
|
||||
elif isinstance(ops[0], operations.DeleteModel):
|
||||
return "delete_%s" % ops[0].name_lower
|
||||
elif isinstance(ops[0], operations.AddField):
|
||||
return "%s_%s" % (ops[0].model_name_lower, ops[0].name_lower)
|
||||
elif isinstance(ops[0], operations.RemoveField):
|
||||
return "remove_%s_%s" % (ops[0].model_name_lower, ops[0].name_lower)
|
||||
elif ops:
|
||||
if all(isinstance(o, operations.CreateModel) for o in ops):
|
||||
return "_".join(sorted(o.name_lower for o in ops))
|
||||
return "auto_%s" % get_migration_name_timestamp()
|
||||
name = ops[0].migration_name_fragment
|
||||
elif len(ops) > 1 and all(isinstance(o, operations.CreateModel) for o in ops):
|
||||
name = '_'.join(sorted(o.migration_name_fragment for o in ops))
|
||||
if name is None:
|
||||
name = 'auto_%s' % get_migration_name_timestamp()
|
||||
return name
|
||||
|
||||
@classmethod
|
||||
def parse_number(cls, name):
|
||||
|
||||
@@ -79,6 +79,14 @@ class Operation:
|
||||
"""
|
||||
return "%s: %s" % (self.__class__.__name__, self._constructor_args)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
"""
|
||||
A filename part suitable for automatically naming a migration
|
||||
containing this operation, or None if not applicable.
|
||||
"""
|
||||
return None
|
||||
|
||||
def references_model(self, name, app_label):
|
||||
"""
|
||||
Return True if there is a chance this operation references the given
|
||||
|
||||
@@ -116,6 +116,10 @@ class AddField(FieldOperation):
|
||||
def describe(self):
|
||||
return "Add field %s to %s" % (self.name, self.model_name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return '%s_%s' % (self.model_name_lower, self.name_lower)
|
||||
|
||||
def reduce(self, operation, app_label):
|
||||
if isinstance(operation, FieldOperation) and self.is_same_field_operation(operation):
|
||||
if isinstance(operation, AlterField):
|
||||
@@ -174,6 +178,10 @@ class RemoveField(FieldOperation):
|
||||
def describe(self):
|
||||
return "Remove field %s from %s" % (self.name, self.model_name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'remove_%s_%s' % (self.model_name_lower, self.name_lower)
|
||||
|
||||
def reduce(self, operation, app_label):
|
||||
from .models import DeleteModel
|
||||
if isinstance(operation, DeleteModel) and operation.name_lower == self.model_name_lower:
|
||||
@@ -243,6 +251,10 @@ class AlterField(FieldOperation):
|
||||
def describe(self):
|
||||
return "Alter field %s on %s" % (self.name, self.model_name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'alter_%s_%s' % (self.model_name_lower, self.name_lower)
|
||||
|
||||
def reduce(self, operation, app_label):
|
||||
if isinstance(operation, RemoveField) and self.is_same_field_operation(operation):
|
||||
return [operation]
|
||||
@@ -354,6 +366,14 @@ class RenameField(FieldOperation):
|
||||
def describe(self):
|
||||
return "Rename field %s on %s to %s" % (self.old_name, self.model_name, self.new_name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'rename_%s_%s_%s' % (
|
||||
self.old_name_lower,
|
||||
self.model_name_lower,
|
||||
self.new_name_lower,
|
||||
)
|
||||
|
||||
def references_field(self, model_name, name, app_label):
|
||||
return self.references_model(model_name, app_label) and (
|
||||
name.lower() == self.old_name_lower or
|
||||
|
||||
@@ -99,6 +99,10 @@ class CreateModel(ModelOperation):
|
||||
def describe(self):
|
||||
return "Create %smodel %s" % ("proxy " if self.options.get("proxy", False) else "", self.name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return self.name_lower
|
||||
|
||||
def references_model(self, name, app_label):
|
||||
name_lower = name.lower()
|
||||
if name_lower == self.name_lower:
|
||||
@@ -273,6 +277,10 @@ class DeleteModel(ModelOperation):
|
||||
def describe(self):
|
||||
return "Delete model %s" % self.name
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'delete_%s' % self.name_lower
|
||||
|
||||
|
||||
class RenameModel(ModelOperation):
|
||||
"""Rename a model."""
|
||||
@@ -397,6 +405,10 @@ class RenameModel(ModelOperation):
|
||||
def describe(self):
|
||||
return "Rename model %s to %s" % (self.old_name, self.new_name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'rename_%s_%s' % (self.old_name_lower, self.new_name_lower)
|
||||
|
||||
def reduce(self, operation, app_label):
|
||||
if (isinstance(operation, RenameModel) and
|
||||
self.new_name_lower == operation.old_name_lower):
|
||||
@@ -470,6 +482,10 @@ class AlterModelTable(ModelOptionOperation):
|
||||
self.table if self.table is not None else "(default)"
|
||||
)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'alter_%s_table' % self.name_lower
|
||||
|
||||
|
||||
class AlterTogetherOptionOperation(ModelOptionOperation):
|
||||
option_name = None
|
||||
@@ -526,6 +542,10 @@ class AlterTogetherOptionOperation(ModelOptionOperation):
|
||||
def describe(self):
|
||||
return "Alter %s for %s (%s constraint(s))" % (self.option_name, self.name, len(self.option_value or ''))
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'alter_%s_%s' % (self.name_lower, self.option_name)
|
||||
|
||||
|
||||
class AlterUniqueTogether(AlterTogetherOptionOperation):
|
||||
"""
|
||||
@@ -607,6 +627,10 @@ class AlterOrderWithRespectTo(ModelOptionOperation):
|
||||
def describe(self):
|
||||
return "Set order_with_respect_to on %s to %s" % (self.name, self.order_with_respect_to)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'alter_%s_order_with_respect_to' % self.name_lower
|
||||
|
||||
|
||||
class AlterModelOptions(ModelOptionOperation):
|
||||
"""
|
||||
@@ -662,6 +686,10 @@ class AlterModelOptions(ModelOptionOperation):
|
||||
def describe(self):
|
||||
return "Change Meta options on %s" % self.name
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'alter_%s_options' % self.name_lower
|
||||
|
||||
|
||||
class AlterModelManagers(ModelOptionOperation):
|
||||
"""Alter the model's managers."""
|
||||
@@ -693,6 +721,10 @@ class AlterModelManagers(ModelOptionOperation):
|
||||
def describe(self):
|
||||
return "Change managers on %s" % self.name
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'alter_%s_managers' % self.name_lower
|
||||
|
||||
|
||||
class IndexOperation(Operation):
|
||||
option_name = 'indexes'
|
||||
@@ -747,6 +779,10 @@ class AddIndex(IndexOperation):
|
||||
self.model_name,
|
||||
)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return '%s_%s' % (self.model_name_lower, self.index.name.lower())
|
||||
|
||||
|
||||
class RemoveIndex(IndexOperation):
|
||||
"""Remove an index from a model."""
|
||||
@@ -789,6 +825,10 @@ class RemoveIndex(IndexOperation):
|
||||
def describe(self):
|
||||
return 'Remove index %s from %s' % (self.name, self.model_name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'remove_%s_%s' % (self.model_name_lower, self.name.lower())
|
||||
|
||||
|
||||
class AddConstraint(IndexOperation):
|
||||
option_name = 'constraints'
|
||||
@@ -821,6 +861,10 @@ class AddConstraint(IndexOperation):
|
||||
def describe(self):
|
||||
return 'Create constraint %s on model %s' % (self.constraint.name, self.model_name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return '%s_%s' % (self.model_name_lower, self.constraint.name.lower())
|
||||
|
||||
|
||||
class RemoveConstraint(IndexOperation):
|
||||
option_name = 'constraints'
|
||||
@@ -857,3 +901,7 @@ class RemoveConstraint(IndexOperation):
|
||||
|
||||
def describe(self):
|
||||
return 'Remove constraint %s from model %s' % (self.name, self.model_name)
|
||||
|
||||
@property
|
||||
def migration_name_fragment(self):
|
||||
return 'remove_%s_%s' % (self.model_name_lower, self.name.lower())
|
||||
|
||||
Reference in New Issue
Block a user