check_dependencies(migration.dependencies(), here)

-def forwards_problems(forwards, done, verbosity=0):

+def to_apply(forwards, done):

+ return [m for m in forwards if m not in done]

+def to_unapply(backwards, done):

+ return [m for m in backwards if m in done]

+def forwards_problems(pending, done, verbosity=0):

- for migration in forwards:

- if migration not in done:

- for m in migration.backwards_plan()[:-1]:

- print " ! Migration %s should not have been applied before %s but was." % (m, migration)

- problems.append((migration, m))

+ for migration in pending:

+ for m in migration.backwards_plan()[:-1]:

+ print " ! Migration %s should not have been applied before %s but was." % (m, migration)

+ problems.append((migration, m))

-def backwards_problems(~~backwards~~, done, verbosity=0):

+def backwards_problems(pending, done, verbosity=0):

- for migration in ~~backwards~~:

+ for migration in pending:

for m in migration.forwards_plan()[:-1]:

# Is the whole forward branch applied?

- missing~~ = [step for step in forwards if step not in~~ current_migrations~~]~~

+ missing_forwards = to_apply(forwards, current_migrations)

# If they're all applied, we only know it's not backwards

+ if not missing_forwards:

# If the remaining migrations are strictly a right segment of the forwards

# trace, we just need to go forwards to our target (and check for badness)

- problems = forwards_problems(forwards, current_migrations, verbosity=verbosity)

+ problems = forwards_problems(missing_forwards, current_migrations, verbosity=verbosity)

# What about the whole backward trace then?

- missing~~ = [step for step in backwards if step not in~~ current_migrations~~]~~

+ missing_backwards = to_apply(backwards, current_migrations)

# If they're all missing, stick with the forwards decision

- if missing == backwards:

+ if missing_backwards == backwards:

# If what's missing is a strict left segment of backwards (i.e.

# all the higher migrations) then we need to go backwards

- problems = backwards_problems(backwards, current_migrations, verbosity=verbosity)

+ present_backwards = to_unapply(backwards, current_migrations)

+ problems = backwards_problems(present_backwards, current_migrations, verbosity=verbosity)

print " - Migrating forwards to %s." % target_name

- for migration in forwards:

- if migration not in current_migrations:

- result = migrator.migrate(migration)

- if result is False: # The migrations errored, but nicely.

+ for migration in missing_forwards:

+ result = migrator.migrate(migration)

+ if result is False: # The migrations errored, but nicely.

# Call any pending post_syncdb signals

db.send_pending_create_signals()

migrator = FakeMigrator(migrator=migrator)

print " - Migrating backwards to just after %s." % target_name

- for migration in backwards:

- if migration in current_migrations:

- migrator.migrate(migration)

+ for migration in present_backwards:

+ migrator.migrate(migration)

print "- Nothing to migrate."