mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Refs #22608 -- Made app_label required when optimizing migrations.
This paved the way for the removal of lot of logic when app_label was not specified.
This commit is contained in:
committed by
Mariusz Felisiak
parent
911545da1d
commit
25bf15c0da
@@ -369,7 +369,7 @@ class MigrationAutodetector:
|
||||
# Optimize migrations
|
||||
for app_label, migrations in self.migrations.items():
|
||||
for migration in migrations:
|
||||
migration.operations = MigrationOptimizer().optimize(migration.operations, app_label=app_label)
|
||||
migration.operations = MigrationOptimizer().optimize(migration.operations, app_label)
|
||||
|
||||
def check_dependency(self, operation, dependency):
|
||||
"""
|
||||
|
||||
@@ -113,7 +113,7 @@ class Operation:
|
||||
|
||||
return router.allow_migrate_model(connection_alias, model)
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
"""
|
||||
Return either a list of operations the actual operation should be
|
||||
replaced with or a boolean that indicates whether or not the specified
|
||||
|
||||
@@ -60,9 +60,9 @@ class FieldOperation(Operation):
|
||||
return True
|
||||
return False
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
return (
|
||||
super().reduce(operation, app_label=app_label) or
|
||||
super().reduce(operation, app_label) or
|
||||
not operation.references_field(self.model_name, self.name, app_label)
|
||||
)
|
||||
|
||||
@@ -122,7 +122,7 @@ class AddField(FieldOperation):
|
||||
def describe(self):
|
||||
return "Add field %s to %s" % (self.name, self.model_name)
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
if isinstance(operation, FieldOperation) and self.is_same_field_operation(operation):
|
||||
if isinstance(operation, AlterField):
|
||||
return [
|
||||
@@ -142,7 +142,7 @@ class AddField(FieldOperation):
|
||||
field=self.field,
|
||||
),
|
||||
]
|
||||
return super().reduce(operation, app_label=app_label)
|
||||
return super().reduce(operation, app_label)
|
||||
|
||||
|
||||
class RemoveField(FieldOperation):
|
||||
@@ -186,11 +186,11 @@ class RemoveField(FieldOperation):
|
||||
def describe(self):
|
||||
return "Remove field %s from %s" % (self.name, self.model_name)
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
from .models import DeleteModel
|
||||
if isinstance(operation, DeleteModel) and operation.name_lower == self.model_name_lower:
|
||||
return [operation]
|
||||
return super().reduce(operation, app_label=app_label)
|
||||
return super().reduce(operation, app_label)
|
||||
|
||||
|
||||
class AlterField(FieldOperation):
|
||||
@@ -256,7 +256,7 @@ class AlterField(FieldOperation):
|
||||
def describe(self):
|
||||
return "Alter field %s on %s" % (self.name, self.model_name)
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
if isinstance(operation, RemoveField) and self.is_same_field_operation(operation):
|
||||
return [operation]
|
||||
elif isinstance(operation, RenameField) and self.is_same_field_operation(operation):
|
||||
@@ -268,7 +268,7 @@ class AlterField(FieldOperation):
|
||||
field=self.field,
|
||||
),
|
||||
]
|
||||
return super().reduce(operation, app_label=app_label)
|
||||
return super().reduce(operation, app_label)
|
||||
|
||||
|
||||
class RenameField(FieldOperation):
|
||||
@@ -382,7 +382,7 @@ class RenameField(FieldOperation):
|
||||
name.lower() == self.new_name_lower
|
||||
)
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
if (isinstance(operation, RenameField) and
|
||||
self.is_same_model_operation(operation) and
|
||||
self.new_name_lower == operation.old_name_lower):
|
||||
@@ -396,6 +396,6 @@ class RenameField(FieldOperation):
|
||||
# Skip `FieldOperation.reduce` as we want to run `references_field`
|
||||
# against self.new_name.
|
||||
return (
|
||||
super(FieldOperation, self).reduce(operation, app_label=app_label) or
|
||||
super(FieldOperation, self).reduce(operation, app_label) or
|
||||
not operation.references_field(self.model_name, self.new_name, app_label)
|
||||
)
|
||||
|
||||
@@ -31,9 +31,9 @@ class ModelOperation(Operation):
|
||||
def references_model(self, name, app_label=None):
|
||||
return name.lower() == self.name_lower
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
return (
|
||||
super().reduce(operation, app_label=app_label) or
|
||||
super().reduce(operation, app_label) or
|
||||
not operation.references_model(self.name, app_label)
|
||||
)
|
||||
|
||||
@@ -117,7 +117,7 @@ class CreateModel(ModelOperation):
|
||||
return True
|
||||
return False
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
if (isinstance(operation, DeleteModel) and
|
||||
self.name_lower == operation.name_lower and
|
||||
not self.options.get("proxy", False)):
|
||||
@@ -236,7 +236,7 @@ class CreateModel(ModelOperation):
|
||||
managers=self.managers,
|
||||
),
|
||||
]
|
||||
return super().reduce(operation, app_label=app_label)
|
||||
return super().reduce(operation, app_label)
|
||||
|
||||
|
||||
class DeleteModel(ModelOperation):
|
||||
@@ -411,7 +411,7 @@ class RenameModel(ModelOperation):
|
||||
def describe(self):
|
||||
return "Rename model %s to %s" % (self.old_name, self.new_name)
|
||||
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
if (isinstance(operation, RenameModel) and
|
||||
self.new_name_lower == operation.old_name_lower):
|
||||
return [
|
||||
@@ -423,16 +423,16 @@ class RenameModel(ModelOperation):
|
||||
# Skip `ModelOperation.reduce` as we want to run `references_model`
|
||||
# against self.new_name.
|
||||
return (
|
||||
super(ModelOperation, self).reduce(operation, app_label=app_label) or
|
||||
super(ModelOperation, self).reduce(operation, app_label) or
|
||||
not operation.references_model(self.new_name, app_label)
|
||||
)
|
||||
|
||||
|
||||
class ModelOptionOperation(ModelOperation):
|
||||
def reduce(self, operation, app_label=None):
|
||||
def reduce(self, operation, app_label):
|
||||
if isinstance(operation, (self.__class__, DeleteModel)) and self.name_lower == operation.name_lower:
|
||||
return [operation]
|
||||
return super().reduce(operation, app_label=app_label)
|
||||
return super().reduce(operation, app_label)
|
||||
|
||||
|
||||
class AlterModelTable(ModelOptionOperation):
|
||||
|
||||
@@ -9,7 +9,7 @@ class MigrationOptimizer:
|
||||
nothing.
|
||||
"""
|
||||
|
||||
def optimize(self, operations, app_label=None):
|
||||
def optimize(self, operations, app_label):
|
||||
"""
|
||||
Main optimization entry point. Pass in a list of Operation instances,
|
||||
get out a new list of Operation instances.
|
||||
@@ -25,11 +25,10 @@ class MigrationOptimizer:
|
||||
The inner loop is run until the starting list is the same as the result
|
||||
list, and then the result is returned. This means that operation
|
||||
optimization must be stable and always return an equal or shorter list.
|
||||
|
||||
The app_label argument is optional, but if you pass it you'll get more
|
||||
efficient optimization.
|
||||
"""
|
||||
# Internal tracking variable for test assertions about # of loops
|
||||
if app_label is None:
|
||||
raise TypeError('app_label must be a str.')
|
||||
self._iterations = 0
|
||||
while True:
|
||||
result = self.optimize_inner(operations, app_label)
|
||||
@@ -38,7 +37,7 @@ class MigrationOptimizer:
|
||||
return result
|
||||
operations = result
|
||||
|
||||
def optimize_inner(self, operations, app_label=None):
|
||||
def optimize_inner(self, operations, app_label):
|
||||
"""Inner optimization loop."""
|
||||
new_operations = []
|
||||
for i, operation in enumerate(operations):
|
||||
|
||||
Reference in New Issue
Block a user