Skip to content

Commit

Permalink
[1.7.x] Fixed #22331 -- Made MigrationAutodetector ignore unmanaged m…
Browse files Browse the repository at this point in the history
…odels.

This commit reverts 69d4b1c and tackle the issue from a different angle.
Models remain present in the project state, but are now ignored by the
autodetector.

Backport of 42336c8 from master
  • Loading branch information
loic authored and timgraham committed Mar 26, 2014
1 parent 1cb6729 commit 39fc8d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
21 changes: 11 additions & 10 deletions django/db/migrations/autodetector.py
Expand Up @@ -51,16 +51,17 @@ def _detect_changes(self):
new_apps = self.to_state.render() new_apps = self.to_state.render()
# Prepare lists of old/new model keys that we care about # Prepare lists of old/new model keys that we care about
# (i.e. ignoring proxy ones) # (i.e. ignoring proxy ones)
old_model_keys = [ old_model_keys = []
(al, mn) for al, mn in self.from_state.models.keys():
for al, mn in self.from_state.models.keys() model = old_apps.get_model(al, mn)
if not old_apps.get_model(al, mn)._meta.proxy if not model._meta.proxy and model._meta.managed:
] old_model_keys.append((al, mn))
new_model_keys = [
(al, mn) new_model_keys = []
for al, mn in self.to_state.models.keys() for al, mn in self.to_state.models.keys():
if not new_apps.get_model(al, mn)._meta.proxy model = new_apps.get_model(al, mn)
] if not model._meta.proxy and model._meta.managed:
new_model_keys.append((al, mn))


def _rel_agnostic_fields_def(fields): def _rel_agnostic_fields_def(fields):
""" """
Expand Down
27 changes: 27 additions & 0 deletions tests/migrations/test_autodetector.py
Expand Up @@ -24,6 +24,8 @@ class AutodetectorTests(TestCase):
author_with_custom_user = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("user", models.ForeignKey("thirdapp.CustomUser"))]) author_with_custom_user = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("user", models.ForeignKey("thirdapp.CustomUser"))])
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", ))
author_unmanaged = ModelState("testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author", ))
author_unmanaged_managed = ModelState("testapp", "AuthorUnmanaged", [], {}, ("testapp.author", ))
publisher = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=100))]) 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))]) publisher_with_author = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("testapp.Author")), ("name", models.CharField(max_length=100))])
publisher_with_book = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("otherapp.Book")), ("name", models.CharField(max_length=100))]) publisher_with_book = ModelState("testapp", "Publisher", [("id", models.AutoField(primary_key=True)), ("author", models.ForeignKey("otherapp.Book")), ("name", models.CharField(max_length=100))])
Expand Down Expand Up @@ -488,6 +490,31 @@ def test_proxy_ignorance(self):
self.assertEqual(action.__class__.__name__, "CreateModel") self.assertEqual(action.__class__.__name__, "CreateModel")
self.assertEqual(action.name, "AuthorProxy") self.assertEqual(action.name, "AuthorProxy")


def test_unmanaged_ignorance(self):
"Tests that the autodetector correctly ignores managed models"
# First, we test adding an unmanaged model
before = self.make_project_state([self.author_empty])
after = self.make_project_state([self.author_empty, self.author_unmanaged])
autodetector = MigrationAutodetector(before, after)
changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes), 0)

# Now, we test turning an unmanaged model into a managed model
before = self.make_project_state([self.author_empty, self.author_unmanaged])
after = self.make_project_state([self.author_empty, self.author_unmanaged_managed])
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), 1)
# Right action?
action = migration.operations[0]
self.assertEqual(action.__class__.__name__, "CreateModel")
self.assertEqual(action.name, "AuthorUnmanaged")

@override_settings(AUTH_USER_MODEL="thirdapp.CustomUser") @override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
def test_swappable(self): def test_swappable(self):
before = self.make_project_state([self.custom_user]) before = self.make_project_state([self.custom_user])
Expand Down

0 comments on commit 39fc8d4

Please sign in to comment.