From 47e4b86ddf67c3ab0725f41d4802bc05ed0a6423 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 20 Jun 2013 15:19:30 +0100 Subject: [PATCH] Autodetect field alters --- django/db/migrations/autodetector.py | 14 ++++++++++++++ django/db/migrations/state.py | 6 ++++++ tests/migrations/test_autodetector.py | 18 ++++++++++++++++++ tests/migrations/test_operations.py | 4 ++-- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index a5aec671f8..508d4526e4 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -89,6 +89,20 @@ class MigrationAutodetector(object): 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:]): diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index 65b749c80c..d1ed22bc29 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -126,3 +126,9 @@ class ModelState(object): 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)) diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 7a90110127..8f32174e7f 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -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 @@ class AutodetectorTests(TestCase): 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") diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 6ee60afa5b..2e72e11954 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -173,8 +173,8 @@ class OperationTests(TestCase): 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: