Skip to content

Commit

Permalink
Autodetect field alters
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Jun 20, 2013
1 parent 80bdf68 commit 47e4b86
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
14 changes: 14 additions & 0 deletions django/db/migrations/autodetector.py
Expand Up @@ -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:]):
Expand Down
6 changes: 6 additions & 0 deletions django/db/migrations/state.py
Expand Up @@ -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))
18 changes: 18 additions & 0 deletions tests/migrations/test_autodetector.py
Expand Up @@ -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))])
Expand Down Expand Up @@ -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")
4 changes: 2 additions & 2 deletions tests/migrations/test_operations.py
Expand Up @@ -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:
Expand Down

0 comments on commit 47e4b86

Please sign in to comment.