Skip to content

Commit

Permalink
Add AlterField and RenameField operations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Jun 20, 2013
1 parent 6f66799 commit 80bdf68
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
2 changes: 1 addition & 1 deletion django/db/migrations/operations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .models import CreateModel, DeleteModel, AlterModelTable
from .fields import AddField, RemoveField
from .fields import AddField, RemoveField, AlterField, RenameField
68 changes: 68 additions & 0 deletions django/db/migrations/operations/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,71 @@ def database_backwards(self, app_label, schema_editor, from_state, to_state):

def describe(self):
return "Remove field %s from %s" % (self.name, self.model_name)


class AlterField(Operation):
"""
Alters a field's database column (e.g. null, max_length) to the provided new field
"""

def __init__(self, model_name, name, field):
self.model_name = model_name
self.name = name
self.field = field

def state_forwards(self, app_label, state):
state.models[app_label, self.model_name.lower()].fields = [
(n, self.field if n == self.name else f) for n, f in state.models[app_label, self.model_name.lower()].fields
]

def database_forwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.render().get_model(app_label, self.model_name)
to_model = to_state.render().get_model(app_label, self.model_name)
schema_editor.alter_field(
from_model,
from_model._meta.get_field_by_name(self.name)[0],
to_model._meta.get_field_by_name(self.name)[0],
)

def database_backwards(self, app_label, schema_editor, from_state, to_state):
self.database_forwards(app_label, schema_editor, from_state, to_state)

def describe(self):
return "Alter field %s on %s" % (self.name, self.model_name)


class RenameField(Operation):
"""
Renames a field on the model. Might affect db_column too.
"""

def __init__(self, model_name, old_name, new_name):
self.model_name = model_name
self.old_name = old_name
self.new_name = new_name

def state_forwards(self, app_label, state):
state.models[app_label, self.model_name.lower()].fields = [
(self.new_name if n == self.old_name else n, f) for n, f in state.models[app_label, self.model_name.lower()].fields
]

def database_forwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.render().get_model(app_label, self.model_name)
to_model = to_state.render().get_model(app_label, self.model_name)
schema_editor.alter_field(
from_model,
from_model._meta.get_field_by_name(self.old_name)[0],
to_model._meta.get_field_by_name(self.new_name)[0],
)

def database_backwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.render().get_model(app_label, self.model_name)
to_model = to_state.render().get_model(app_label, self.model_name)
schema_editor.alter_field(
from_model,
from_model._meta.get_field_by_name(self.new_name)[0],
to_model._meta.get_field_by_name(self.old_name)[0],
)

def describe(self):
return "Rename field %s on %s to %s" % (self.old_name, self.model_name, self.new_name)
9 changes: 8 additions & 1 deletion django/db/migrations/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,17 @@ def from_model(cls, model):

def clone(self):
"Returns an exact copy of this ModelState"
# We deep-clone the fields using deconstruction
fields = []
for name, field in self.fields:
_, path, args, kwargs = field.deconstruct()
field_class = import_by_path(path)
fields.append((name, field_class(*args, **kwargs)))
# Now make a copy
return self.__class__(
app_label = self.app_label,
name = self.name,
fields = list(self.fields),
fields = fields,
options = dict(self.options),
bases = self.bases,
)
Expand Down
53 changes: 52 additions & 1 deletion tests/migrations/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def assertColumnExists(self, table, column):
def assertColumnNotExists(self, table, column):
self.assertNotIn(column, [c.name for c in connection.introspection.get_table_description(connection.cursor(), table)])

def assertColumnNull(self, table, column):
self.assertEqual([c.null_ok for c in connection.introspection.get_table_description(connection.cursor(), table) if c.name == column][0], True)

def assertColumnNotNull(self, table, column):
self.assertEqual([c.null_ok for c in connection.introspection.get_table_description(connection.cursor(), table) if c.name == column][0], False)

def set_up_test_model(self, app_label):
"""
Creates a test model state and database table.
Expand Down Expand Up @@ -50,7 +56,7 @@ def test_create_model(self):
"Pony",
[
("id", models.AutoField(primary_key=True)),
("pink", models.BooleanField(default=True)),
("pink", models.IntegerField(default=1)),
],
)
# Test the state alteration
Expand Down Expand Up @@ -157,3 +163,48 @@ def test_alter_model_table(self):
operation.database_backwards("test_almota", editor, new_state, project_state)
self.assertTableExists("test_almota_pony")
self.assertTableNotExists("test_almota_pony_2")

def test_alter_field(self):
"""
Tests the AlterField operation.
"""
project_state = self.set_up_test_model("test_alfl")
# Test the state alteration
operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
new_state = project_state.clone()
operation.state_forwards("test_alfl", new_state)
self.assertEqual([f for n, f in project_state.models["test_alfl", "pony"].fields if n == "pink"][0].null, False)
self.assertEqual([f for n, f in new_state.models["test_alfl", "pony"].fields if n == "pink"][0].null, True)
# Test the database alteration
self.assertColumnNotNull("test_alfl_pony", "pink")
with connection.schema_editor() as editor:
operation.database_forwards("test_alfl", editor, project_state, new_state)
self.assertColumnNull("test_alfl_pony", "pink")
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_alfl", editor, new_state, project_state)
self.assertColumnNotNull("test_alfl_pony", "pink")

def test_rename_field(self):
"""
Tests the RenameField operation.
"""
project_state = self.set_up_test_model("test_rnfl")
# Test the state alteration
operation = migrations.RenameField("Pony", "pink", "blue")
new_state = project_state.clone()
operation.state_forwards("test_rnfl", new_state)
self.assertIn("blue", [n for n, f in new_state.models["test_rnfl", "pony"].fields])
self.assertNotIn("pink", [n for n, f in new_state.models["test_rnfl", "pony"].fields])
# Test the database alteration
self.assertColumnExists("test_rnfl_pony", "pink")
self.assertColumnNotExists("test_rnfl_pony", "blue")
with connection.schema_editor() as editor:
operation.database_forwards("test_rnfl", editor, project_state, new_state)
self.assertColumnExists("test_rnfl_pony", "blue")
self.assertColumnNotExists("test_rnfl_pony", "pink")
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_rnfl", editor, new_state, project_state)
self.assertColumnExists("test_rnfl_pony", "pink")
self.assertColumnNotExists("test_rnfl_pony", "blue")

0 comments on commit 80bdf68

Please sign in to comment.