1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Don't make a second migration if there was a force-null-default addcol.

This commit is contained in:
Andrew Godwin
2013-12-04 13:56:22 +00:00
parent df800b1609
commit ce05b8a69e
3 changed files with 63 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
from django.db import connection, models, migrations, router
from django.db.models.fields import NOT_PROVIDED
from django.db.transaction import atomic
from django.db.utils import IntegrityError
from django.db.migrations.state import ProjectState
@@ -130,10 +131,19 @@ class OperationTests(MigrationTestBase):
"""
project_state = self.set_up_test_model("test_adfl")
# Test the state alteration
operation = migrations.AddField("Pony", "height", models.FloatField(null=True))
operation = migrations.AddField(
"Pony",
"height",
models.FloatField(null=True, default=5),
)
new_state = project_state.clone()
operation.state_forwards("test_adfl", new_state)
self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 4)
field = [
f for n, f in new_state.models["test_adfl", "pony"].fields
if n == "height"
][0]
self.assertEqual(field.default, 5)
# Test the database alteration
self.assertColumnNotExists("test_adfl_pony", "height")
with connection.schema_editor() as editor:
@@ -144,6 +154,28 @@ class OperationTests(MigrationTestBase):
operation.database_backwards("test_adfl", editor, new_state, project_state)
self.assertColumnNotExists("test_adfl_pony", "height")
def test_add_field_preserve_default(self):
"""
Tests the AddField operation's state alteration
when preserve_default = False.
"""
project_state = self.set_up_test_model("test_adflpd")
# Test the state alteration
operation = migrations.AddField(
"Pony",
"height",
models.FloatField(null=True, default=4),
preserve_default = False,
)
new_state = project_state.clone()
operation.state_forwards("test_adflpd", new_state)
self.assertEqual(len(new_state.models["test_adflpd", "pony"].fields), 4)
field = [
f for n, f in new_state.models["test_adflpd", "pony"].fields
if n == "height"
][0]
self.assertEqual(field.default, NOT_PROVIDED)
def test_add_field_m2m(self):
"""
Tests the AddField operation with a ManyToManyField.