Autodetect field alters
This commit is contained in:
parent
80bdf68d6b
commit
47e4b86ddf
|
@ -89,6 +89,20 @@ class MigrationAutodetector(object):
|
||||||
name = field_name,
|
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
|
# Alright, now add internal dependencies
|
||||||
for app_label, migrations in self.migrations.items():
|
for app_label, migrations in self.migrations.items():
|
||||||
for m1, m2 in zip(migrations, migrations[1:]):
|
for m1, m2 in zip(migrations, migrations[1:]):
|
||||||
|
|
|
@ -126,3 +126,9 @@ class ModelState(object):
|
||||||
tuple(self.bases),
|
tuple(self.bases),
|
||||||
body,
|
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_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 = 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_pony = ModelState("otherapp", "Pony", [("id", models.AutoField(primary_key=True))])
|
||||||
other_stable = ModelState("otherapp", "Stable", [("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))])
|
third_thing = ModelState("thirdapp", "Thing", [("id", models.AutoField(primary_key=True))])
|
||||||
|
@ -130,3 +131,20 @@ class AutodetectorTests(TestCase):
|
||||||
action = migration.operations[0]
|
action = migration.operations[0]
|
||||||
self.assertEqual(action.__class__.__name__, "RemoveField")
|
self.assertEqual(action.__class__.__name__, "RemoveField")
|
||||||
self.assertEqual(action.name, "name")
|
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 @@ class OperationTests(TestCase):
|
||||||
operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
|
operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
|
||||||
new_state = project_state.clone()
|
new_state = project_state.clone()
|
||||||
operation.state_forwards("test_alfl", new_state)
|
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(project_state.models["test_alfl", "pony"].get_field_by_name("pink").null, False)
|
||||||
self.assertEqual([f for n, f in new_state.models["test_alfl", "pony"].fields if n == "pink"][0].null, True)
|
self.assertEqual(new_state.models["test_alfl", "pony"].get_field_by_name("pink").null, True)
|
||||||
# Test the database alteration
|
# Test the database alteration
|
||||||
self.assertColumnNotNull("test_alfl_pony", "pink")
|
self.assertColumnNotNull("test_alfl_pony", "pink")
|
||||||
with connection.schema_editor() as editor:
|
with connection.schema_editor() as editor:
|
||||||
|
|
Loading…
Reference in New Issue