Permalink
Browse files

Autodetect field alters

  • Loading branch information...
1 parent 80bdf68 commit 47e4b86ddf67c3ab0725f41d4802bc05ed0a6423 @andrewgodwin andrewgodwin committed Jun 20, 2013
@@ -89,6 +89,20 @@ def changes(self):
name = field_name,
)
)
+ # The same fields
+ for field_name in old_field_names.intersection(new_field_names):
+ # Did the field change?
+ old_field_dec = old_model_state.get_field_by_name(field_name).deconstruct()
+ new_field_dec = new_model_state.get_field_by_name(field_name).deconstruct()
+ if old_field_dec != new_field_dec:
+ self.add_to_migration(
+ app_label,
+ operations.AlterField(
+ model_name = model_name,
+ name = field_name,
+ field = new_model_state.get_field_by_name(field_name),
+ )
+ )
# Alright, now add internal dependencies
for app_label, migrations in self.migrations.items():
for m1, m2 in zip(migrations, migrations[1:]):
@@ -126,3 +126,9 @@ def render(self, app_cache):
tuple(self.bases),
body,
)
+
+ def get_field_by_name(self, name):
+ for fname, field in self.fields:
+ if fname == name:
+ return field
+ raise ValueError("No field called %s on model %s" % (name, self.name))
@@ -13,6 +13,7 @@ class AutodetectorTests(TestCase):
author_empty = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True))])
author_name = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200))])
+ author_name_longer = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=400))])
other_pony = ModelState("otherapp", "Pony", [("id", models.AutoField(primary_key=True))])
other_stable = ModelState("otherapp", "Stable", [("id", models.AutoField(primary_key=True))])
third_thing = ModelState("thirdapp", "Thing", [("id", models.AutoField(primary_key=True))])
@@ -130,3 +131,20 @@ def test_remove_field(self):
action = migration.operations[0]
self.assertEqual(action.__class__.__name__, "RemoveField")
self.assertEqual(action.name, "name")
+
+ def test_alter_field(self):
+ "Tests autodetection of new fields"
+ # Make state
+ before = self.make_project_state([self.author_name])
+ after = self.make_project_state([self.author_name_longer])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector.changes()
+ # Right number of migrations?
+ self.assertEqual(len(changes['testapp']), 1)
+ # Right number of actions?
+ migration = changes['testapp'][0]
+ self.assertEqual(len(migration.operations), 1)
+ # Right action?
+ action = migration.operations[0]
+ self.assertEqual(action.__class__.__name__, "AlterField")
+ self.assertEqual(action.name, "name")
@@ -173,8 +173,8 @@ def test_alter_field(self):
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)
+ self.assertEqual(project_state.models["test_alfl", "pony"].get_field_by_name("pink").null, False)
+ self.assertEqual(new_state.models["test_alfl", "pony"].get_field_by_name("pink").null, True)
# Test the database alteration
self.assertColumnNotNull("test_alfl_pony", "pink")
with connection.schema_editor() as editor:

0 comments on commit 47e4b86

Please sign in to comment.