summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2013-07-02 11:25:18 +0100
committerAndrew Godwin <andrew@aeracode.org>2013-07-02 11:25:18 +0100
commit3b20af3e96b45fd3cfd9beb74bd09f65b4e38aa8 (patch)
tree1c2dd996884c4464c4d577bdb054c774d0ced960
parent67dcea711e92025d0e8676b869b7ef15dbc6db73 (diff)
Autodetection of unique_together changes
-rw-r--r--django/db/migrations/autodetector.py9
-rw-r--r--tests/migrations/test_autodetector.py38
2 files changed, 47 insertions, 0 deletions
diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py
index bb065e99bf..d524f96a0b 100644
--- a/django/db/migrations/autodetector.py
+++ b/django/db/migrations/autodetector.py
@@ -191,6 +191,15 @@ class MigrationAutodetector(object):
field = new_model_state.get_field_by_name(field_name),
)
)
+ # unique_together changes
+ if old_model_state.options.get("unique_together", set()) != new_model_state.options.get("unique_together", set()):
+ self.add_to_migration(
+ app_label,
+ operations.AlterUniqueTogether(
+ name = model_name,
+ unique_together = new_model_state.options.get("unique_together", set()),
+ )
+ )
# Alright, now add internal dependencies
for app_label, migrations in self.migrations.items():
for m1, m2 in zip(migrations, migrations[1:]):
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index 659b45dbd2..7bed4eb57e 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -20,6 +20,8 @@ class AutodetectorTests(TestCase):
other_stable = ModelState("otherapp", "Stable", [("id", models.AutoField(primary_key=True))])
third_thing = ModelState("thirdapp", "Thing", [("id", models.AutoField(primary_key=True))])
book = ModelState("otherapp", "Book", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("title", models.CharField(max_length=200))])
+ book_unique = ModelState("otherapp", "Book", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("title", models.CharField(max_length=200))], {"unique_together": [("author", "title")]})
+ book_unique_2 = ModelState("otherapp", "Book", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("title", models.CharField(max_length=200))], {"unique_together": [("title", "author")]})
edition = ModelState("thirdapp", "Edition", [("id", models.AutoField(primary_key=True)), ("book", models.ForeignKey("otherapp.Book"))])
def make_project_state(self, model_states):
@@ -234,3 +236,39 @@ class AutodetectorTests(TestCase):
self.assertEqual(migration1.dependencies, [("otherapp", "auto_1")])
self.assertEqual(migration2.dependencies, [])
self.assertEqual(set(migration3.dependencies), set([("otherapp", "auto_1"), ("testapp", "auto_1")]))
+
+ def test_unique_together(self):
+ "Tests unique_together detection"
+ # Make state
+ before = self.make_project_state([self.author_empty, self.book])
+ after = self.make_project_state([self.author_empty, self.book_unique])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector.changes()
+ # Right number of migrations?
+ self.assertEqual(len(changes['otherapp']), 1)
+ # Right number of actions?
+ migration = changes['otherapp'][0]
+ self.assertEqual(len(migration.operations), 1)
+ # Right action?
+ action = migration.operations[0]
+ self.assertEqual(action.__class__.__name__, "AlterUniqueTogether")
+ self.assertEqual(action.name, "book")
+ self.assertEqual(action.unique_together, set([("author", "title")]))
+
+ def test_unique_together_ordering(self):
+ "Tests that unique_together also triggers on ordering changes"
+ # Make state
+ before = self.make_project_state([self.author_empty, self.book_unique])
+ after = self.make_project_state([self.author_empty, self.book_unique_2])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector.changes()
+ # Right number of migrations?
+ self.assertEqual(len(changes['otherapp']), 1)
+ # Right number of actions?
+ migration = changes['otherapp'][0]
+ self.assertEqual(len(migration.operations), 1)
+ # Right action?
+ action = migration.operations[0]
+ self.assertEqual(action.__class__.__name__, "AlterUniqueTogether")
+ self.assertEqual(action.name, "book")
+ self.assertEqual(action.unique_together, set([("title", "author")]))