Skip to content

Commit

Permalink
Fixed #21114 -- Migrations must not have a dependency to themselves.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusH authored and timgraham committed Sep 17, 2013
1 parent 73a610d commit 5a424c2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
6 changes: 4 additions & 2 deletions django/db/migrations/autodetector.py
Expand Up @@ -106,7 +106,8 @@ def _detect_changes(self):
) )
) )
for field_name, other_app_label, other_model_name in related_fields: for field_name, other_app_label, other_model_name in related_fields:
self.add_dependency(app_label, other_app_label) if app_label != other_app_label:
self.add_dependency(app_label, other_app_label)
del pending_add[app_label, model_name] del pending_add[app_label, model_name]
# Ah well, we'll need to split one. Pick deterministically. # Ah well, we'll need to split one. Pick deterministically.
else: else:
Expand Down Expand Up @@ -140,7 +141,8 @@ def _detect_changes(self):
), ),
new = True, new = True,
) )
self.add_dependency(app_label, other_app_label) if app_label != other_app_label:
self.add_dependency(app_label, other_app_label)
# Removing models # Removing models
removed_models = set(old_model_keys) - set(new_model_keys) removed_models = set(old_model_keys) - set(new_model_keys)
for app_label, model_name in removed_models: for app_label, model_name in removed_models:
Expand Down
55 changes: 55 additions & 0 deletions tests/migrations/test_autodetector.py
Expand Up @@ -16,8 +16,11 @@ class AutodetectorTests(TestCase):
author_name_longer = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=400))]) author_name_longer = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=400))])
author_name_renamed = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("names", models.CharField(max_length=200))]) author_name_renamed = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("names", models.CharField(max_length=200))])
author_with_book = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))]) author_with_book = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
author_with_publisher = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("publisher", models.ForeignKey("testapp.Publisher"))])
author_proxy = ModelState("testapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author", )) author_proxy = ModelState("testapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author", ))
author_proxy_notproxy = ModelState("testapp", "AuthorProxy", [], {}, ("testapp.author", )) author_proxy_notproxy = ModelState("testapp", "AuthorProxy", [], {}, ("testapp.author", ))
publisher = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=100))])
publisher_with_author = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("name", models.CharField(max_length=100))])
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))])
Expand Down Expand Up @@ -205,6 +208,29 @@ def test_fk_dependency(self):
self.assertEqual(migration2.dependencies, [("testapp", "auto_1")]) self.assertEqual(migration2.dependencies, [("testapp", "auto_1")])
self.assertEqual(migration3.dependencies, [("otherapp", "auto_1")]) self.assertEqual(migration3.dependencies, [("otherapp", "auto_1")])


def test_same_app_no_fk_dependency(self):
"""
Tests that a migration with a FK between two models of the same app
does not have a dependency to itself.
"""
# Make state
before = self.make_project_state([])
after = self.make_project_state([self.author_with_publisher, self.publisher])
autodetector = MigrationAutodetector(before, after)
changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
migration = changes['testapp'][0]
self.assertEqual(len(migration.operations), 2)
# Right actions?
action = migration.operations[0]
self.assertEqual(action.__class__.__name__, "CreateModel")
action = migration.operations[1]
self.assertEqual(action.__class__.__name__, "CreateModel")
# Right dependencies?
self.assertEqual(migration.dependencies, [])

def test_circular_fk_dependency(self): def test_circular_fk_dependency(self):
""" """
Tests that having a circular ForeignKey dependency automatically Tests that having a circular ForeignKey dependency automatically
Expand Down Expand Up @@ -239,6 +265,35 @@ def test_circular_fk_dependency(self):
self.assertEqual(migration2.dependencies, []) self.assertEqual(migration2.dependencies, [])
self.assertEqual(set(migration3.dependencies), set([("otherapp", "auto_1"), ("testapp", "auto_1")])) self.assertEqual(set(migration3.dependencies), set([("otherapp", "auto_1"), ("testapp", "auto_1")]))


def test_same_app_circular_fk_dependency(self):
"""
Tests that a migration with a FK between two models of the same app
does not have a dependency to itself.
"""
# Make state
before = self.make_project_state([])
after = self.make_project_state([self.author_with_publisher, self.publisher_with_author])
autodetector = MigrationAutodetector(before, after)
changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 2)
# Right number of actions?
migration1 = changes['testapp'][0]
self.assertEqual(len(migration1.operations), 2)
migration2 = changes['testapp'][1]
self.assertEqual(len(migration2.operations), 1)
# Right actions?
action = migration1.operations[0]
self.assertEqual(action.__class__.__name__, "CreateModel")
action = migration1.operations[1]
self.assertEqual(action.__class__.__name__, "CreateModel")
action = migration2.operations[0]
self.assertEqual(action.__class__.__name__, "AddField")
self.assertEqual(action.name, "publisher")
# Right dependencies?
self.assertEqual(migration1.dependencies, [])
self.assertEqual(migration2.dependencies, [("testapp", "auto_1")])

def test_unique_together(self): def test_unique_together(self):
"Tests unique_together detection" "Tests unique_together detection"
# Make state # Make state
Expand Down

0 comments on commit 5a424c2

Please sign in to comment.