Skip to content

Commit

Permalink
Fixed #22319 -- Fixed migration external dependencies when there are …
Browse files Browse the repository at this point in the history
…internal dependencies.
  • Loading branch information
loic authored and timgraham committed Mar 31, 2014
1 parent a449e7f commit 0fd51cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions django/db/migrations/autodetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def _rel_agnostic_fields_def(fields):
bases=model_state.bases,
)
)

# Phase 2 is progressively adding pending models, splitting up into two
# migrations if required.
pending_new_fks = []
Expand Down Expand Up @@ -161,14 +162,6 @@ def _rel_agnostic_fields_def(fields):
# migration for safety.
new=any((al, mn) in added_phase_2 for f, al, mn in related_fields),
)
for field_name, other_app_label, other_model_name in related_fields:
# If it depends on a swappable something, add a dynamic depend'cy
swappable_setting = new_apps.get_model(app_label, model_name)._meta.get_field_by_name(field_name)[0].swappable_setting
if swappable_setting is not None:
self.add_swappable_dependency(app_label, swappable_setting)
elif app_label != other_app_label:
self.add_dependency(app_label, other_app_label)
del pending_add[app_label, model_name]
added_phase_2.add((app_label, model_name))
# Ah well, we'll need to split one. Pick deterministically.
else:
Expand All @@ -194,8 +187,16 @@ def _rel_agnostic_fields_def(fields):
# Add the bad fields to be made in a phase 3
for field_name, (other_app_label, other_model_name) in bad_fields.items():
pending_new_fks.append((app_label, model_name, field_name, other_app_label))
del pending_add[app_label, model_name]
# Phase 3 is adding the final set of FKs as separate new migrations
for field_name, other_app_label, other_model_name in related_fields:
# If it depends on a swappable something, add a dynamic depend'cy
swappable_setting = new_apps.get_model(app_label, model_name)._meta.get_field_by_name(field_name)[0].swappable_setting
if swappable_setting is not None:
self.add_swappable_dependency(app_label, swappable_setting)
elif app_label != other_app_label:
self.add_dependency(app_label, other_app_label)
del pending_add[app_label, model_name]

# Phase 3 is adding the final set of FKs as separate new migrations.
for app_label, model_name, field_name, other_app_label in pending_new_fks:
model_state = self.to_state.models[app_label, model_name]
self.add_to_migration(
Expand Down Expand Up @@ -370,7 +371,7 @@ def add_dependency(self, app_label, other_app_label):
Adds a dependency to app_label's newest migration on
other_app_label's latest migration.
"""
if self.migrations.get(other_app_label, []):
if self.migrations.get(other_app_label):
dependency = (other_app_label, self.migrations[other_app_label][-1].name)
else:
dependency = (other_app_label, "__first__")
Expand Down
2 changes: 1 addition & 1 deletion tests/migrations/test_autodetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def test_circular_fk_dependency(self):
self.assertEqual(action.name, "author")
# Right dependencies?
self.assertEqual(migration1.dependencies, [("otherapp", "auto_1")])
self.assertEqual(migration2.dependencies, [])
self.assertEqual(migration2.dependencies, [('testapp', '__first__')])
self.assertEqual(set(migration3.dependencies), set([("otherapp", "auto_1"), ("testapp", "auto_1")]))

def test_same_app_circular_fk_dependency(self):
Expand Down

0 comments on commit 0fd51cf

Please sign in to comment.