Explicitly imported migration operations in optimizer
Thanks Andrew Godwin for the review.
This commit is contained in:
parent
128caa1e16
commit
4a7d374fd5
|
@ -1,6 +1,10 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.db import migrations
|
from django.db.migrations import (
|
||||||
|
AddField, AlterField, AlterIndexTogether, AlterModelTable,
|
||||||
|
AlterOrderWithRespectTo, AlterUniqueTogether, CreateModel, DeleteModel,
|
||||||
|
RemoveField, RenameField, RenameModel,
|
||||||
|
)
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,120 +21,120 @@ class MigrationOptimizer(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.model_level_operations = (
|
self.model_level_operations = (
|
||||||
migrations.CreateModel,
|
CreateModel,
|
||||||
migrations.AlterModelTable,
|
AlterModelTable,
|
||||||
migrations.AlterUniqueTogether,
|
AlterUniqueTogether,
|
||||||
migrations.AlterIndexTogether,
|
AlterIndexTogether,
|
||||||
migrations.AlterOrderWithRespectTo,
|
AlterOrderWithRespectTo,
|
||||||
)
|
)
|
||||||
self.field_level_operations = (
|
self.field_level_operations = (
|
||||||
migrations.AddField,
|
AddField,
|
||||||
migrations.AlterField,
|
AlterField,
|
||||||
)
|
)
|
||||||
self.reduce_methods = [
|
self.reduce_methods = [
|
||||||
(
|
(
|
||||||
migrations.CreateModel,
|
CreateModel,
|
||||||
migrations.DeleteModel,
|
DeleteModel,
|
||||||
self.reduce_model_create_delete,
|
self.reduce_model_create_delete,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterModelTable,
|
AlterModelTable,
|
||||||
migrations.DeleteModel,
|
DeleteModel,
|
||||||
self.reduce_model_alter_delete,
|
self.reduce_model_alter_delete,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterUniqueTogether,
|
AlterUniqueTogether,
|
||||||
migrations.DeleteModel,
|
DeleteModel,
|
||||||
self.reduce_model_alter_delete,
|
self.reduce_model_alter_delete,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterIndexTogether,
|
AlterIndexTogether,
|
||||||
migrations.DeleteModel,
|
DeleteModel,
|
||||||
self.reduce_model_alter_delete,
|
self.reduce_model_alter_delete,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterOrderWithRespectTo,
|
AlterOrderWithRespectTo,
|
||||||
migrations.DeleteModel,
|
DeleteModel,
|
||||||
self.reduce_model_alter_delete,
|
self.reduce_model_alter_delete,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterModelTable,
|
AlterModelTable,
|
||||||
migrations.AlterModelTable,
|
AlterModelTable,
|
||||||
self.reduce_model_alter_alter,
|
self.reduce_model_alter_alter,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterUniqueTogether,
|
AlterUniqueTogether,
|
||||||
migrations.AlterUniqueTogether,
|
AlterUniqueTogether,
|
||||||
self.reduce_model_alter_alter,
|
self.reduce_model_alter_alter,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterIndexTogether,
|
AlterIndexTogether,
|
||||||
migrations.AlterIndexTogether,
|
AlterIndexTogether,
|
||||||
self.reduce_model_alter_alter,
|
self.reduce_model_alter_alter,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterOrderWithRespectTo,
|
AlterOrderWithRespectTo,
|
||||||
migrations.AlterOrderWithRespectTo,
|
AlterOrderWithRespectTo,
|
||||||
self.reduce_model_alter_alter,
|
self.reduce_model_alter_alter,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.CreateModel,
|
CreateModel,
|
||||||
migrations.RenameModel,
|
RenameModel,
|
||||||
self.reduce_model_create_rename,
|
self.reduce_model_create_rename,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.RenameModel,
|
RenameModel,
|
||||||
migrations.RenameModel,
|
RenameModel,
|
||||||
self.reduce_model_rename_self,
|
self.reduce_model_rename_self,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.CreateModel,
|
CreateModel,
|
||||||
migrations.AddField,
|
AddField,
|
||||||
self.reduce_create_model_add_field,
|
self.reduce_create_model_add_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.CreateModel,
|
CreateModel,
|
||||||
migrations.AlterField,
|
AlterField,
|
||||||
self.reduce_create_model_alter_field,
|
self.reduce_create_model_alter_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.CreateModel,
|
CreateModel,
|
||||||
migrations.RemoveField,
|
RemoveField,
|
||||||
self.reduce_create_model_remove_field,
|
self.reduce_create_model_remove_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AddField,
|
AddField,
|
||||||
migrations.AlterField,
|
AlterField,
|
||||||
self.reduce_add_field_alter_field,
|
self.reduce_add_field_alter_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AddField,
|
AddField,
|
||||||
migrations.RemoveField,
|
RemoveField,
|
||||||
self.reduce_add_field_delete_field,
|
self.reduce_add_field_delete_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterField,
|
AlterField,
|
||||||
migrations.RemoveField,
|
RemoveField,
|
||||||
self.reduce_alter_field_delete_field,
|
self.reduce_alter_field_delete_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AddField,
|
AddField,
|
||||||
migrations.RenameField,
|
RenameField,
|
||||||
self.reduce_add_field_rename_field,
|
self.reduce_add_field_rename_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.AlterField,
|
AlterField,
|
||||||
migrations.RenameField,
|
RenameField,
|
||||||
self.reduce_alter_field_rename_field,
|
self.reduce_alter_field_rename_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.CreateModel,
|
CreateModel,
|
||||||
migrations.RenameField,
|
RenameField,
|
||||||
self.reduce_create_model_rename_field,
|
self.reduce_create_model_rename_field,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
migrations.RenameField,
|
RenameField,
|
||||||
migrations.RenameField,
|
RenameField,
|
||||||
self.reduce_rename_field_self,
|
self.reduce_rename_field_self,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
@ -240,7 +244,7 @@ class MigrationOptimizer(object):
|
||||||
"""
|
"""
|
||||||
if operation.name_lower == other.old_name_lower:
|
if operation.name_lower == other.old_name_lower:
|
||||||
return [
|
return [
|
||||||
migrations.CreateModel(
|
CreateModel(
|
||||||
other.new_name,
|
other.new_name,
|
||||||
fields=operation.fields,
|
fields=operation.fields,
|
||||||
options=operation.options,
|
options=operation.options,
|
||||||
|
@ -255,7 +259,7 @@ class MigrationOptimizer(object):
|
||||||
"""
|
"""
|
||||||
if operation.new_name_lower == other.old_name_lower:
|
if operation.new_name_lower == other.old_name_lower:
|
||||||
return [
|
return [
|
||||||
migrations.RenameModel(
|
RenameModel(
|
||||||
operation.old_name,
|
operation.old_name,
|
||||||
other.new_name,
|
other.new_name,
|
||||||
)
|
)
|
||||||
|
@ -277,7 +281,7 @@ class MigrationOptimizer(object):
|
||||||
return None
|
return None
|
||||||
# OK, that's fine
|
# OK, that's fine
|
||||||
return [
|
return [
|
||||||
migrations.CreateModel(
|
CreateModel(
|
||||||
operation.name,
|
operation.name,
|
||||||
fields=operation.fields + [(other.name, other.field)],
|
fields=operation.fields + [(other.name, other.field)],
|
||||||
options=operation.options,
|
options=operation.options,
|
||||||
|
@ -289,7 +293,7 @@ class MigrationOptimizer(object):
|
||||||
def reduce_create_model_alter_field(self, operation, other, in_between):
|
def reduce_create_model_alter_field(self, operation, other, in_between):
|
||||||
if operation.name_lower == other.model_name_lower:
|
if operation.name_lower == other.model_name_lower:
|
||||||
return [
|
return [
|
||||||
migrations.CreateModel(
|
CreateModel(
|
||||||
operation.name,
|
operation.name,
|
||||||
fields=[
|
fields=[
|
||||||
(n, other.field if n == other.name else v)
|
(n, other.field if n == other.name else v)
|
||||||
|
@ -304,7 +308,7 @@ class MigrationOptimizer(object):
|
||||||
def reduce_create_model_rename_field(self, operation, other, in_between):
|
def reduce_create_model_rename_field(self, operation, other, in_between):
|
||||||
if operation.name_lower == other.model_name_lower:
|
if operation.name_lower == other.model_name_lower:
|
||||||
return [
|
return [
|
||||||
migrations.CreateModel(
|
CreateModel(
|
||||||
operation.name,
|
operation.name,
|
||||||
fields=[
|
fields=[
|
||||||
(other.new_name if n == other.old_name else n, v)
|
(other.new_name if n == other.old_name else n, v)
|
||||||
|
@ -319,7 +323,7 @@ class MigrationOptimizer(object):
|
||||||
def reduce_create_model_remove_field(self, operation, other, in_between):
|
def reduce_create_model_remove_field(self, operation, other, in_between):
|
||||||
if operation.name_lower == other.model_name_lower:
|
if operation.name_lower == other.model_name_lower:
|
||||||
return [
|
return [
|
||||||
migrations.CreateModel(
|
CreateModel(
|
||||||
operation.name,
|
operation.name,
|
||||||
fields=[
|
fields=[
|
||||||
(n, v)
|
(n, v)
|
||||||
|
@ -336,7 +340,7 @@ class MigrationOptimizer(object):
|
||||||
if (operation.model_name_lower == other.model_name_lower and
|
if (operation.model_name_lower == other.model_name_lower and
|
||||||
operation.name_lower == other.name_lower):
|
operation.name_lower == other.name_lower):
|
||||||
return [
|
return [
|
||||||
migrations.AddField(
|
AddField(
|
||||||
model_name=operation.model_name,
|
model_name=operation.model_name,
|
||||||
name=operation.name,
|
name=operation.name,
|
||||||
field=other.field,
|
field=other.field,
|
||||||
|
@ -357,7 +361,7 @@ class MigrationOptimizer(object):
|
||||||
if (operation.model_name_lower == other.model_name_lower and
|
if (operation.model_name_lower == other.model_name_lower and
|
||||||
operation.name_lower == other.old_name_lower):
|
operation.name_lower == other.old_name_lower):
|
||||||
return [
|
return [
|
||||||
migrations.AddField(
|
AddField(
|
||||||
model_name=operation.model_name,
|
model_name=operation.model_name,
|
||||||
name=other.new_name,
|
name=other.new_name,
|
||||||
field=operation.field,
|
field=operation.field,
|
||||||
|
@ -369,7 +373,7 @@ class MigrationOptimizer(object):
|
||||||
operation.name_lower == other.old_name_lower):
|
operation.name_lower == other.old_name_lower):
|
||||||
return [
|
return [
|
||||||
other,
|
other,
|
||||||
migrations.AlterField(
|
AlterField(
|
||||||
model_name=operation.model_name,
|
model_name=operation.model_name,
|
||||||
name=other.new_name,
|
name=other.new_name,
|
||||||
field=operation.field,
|
field=operation.field,
|
||||||
|
@ -380,7 +384,7 @@ class MigrationOptimizer(object):
|
||||||
if (operation.model_name_lower == other.model_name_lower and
|
if (operation.model_name_lower == other.model_name_lower and
|
||||||
operation.new_name_lower == other.old_name_lower):
|
operation.new_name_lower == other.old_name_lower):
|
||||||
return [
|
return [
|
||||||
migrations.RenameField(
|
RenameField(
|
||||||
operation.model_name,
|
operation.model_name,
|
||||||
operation.old_name,
|
operation.old_name,
|
||||||
other.new_name,
|
other.new_name,
|
||||||
|
|
Loading…
Reference in New Issue