Commits

Simon Law committed 6073337

Create a GhostMigrations exception

Comments (0)

Files changed (2)

south/exceptions.py

                 "    %(matches_list)s") % self.__dict__
 
 
+class GhostMigrations(SouthError):
+    def __init__(self, ghosts):
+        self.ghosts = ghosts
+
+    def __str__(self):
+        self.ghosts_list = "\n    ".join([unicode(m) for m in self.ghosts])
+        return (" ! These migrations are in the database but not on disk:\n"
+                "    %(ghosts_list)s\n"
+                " ! I'm not trusting myself; fix this yourself by fiddling"
+                " ! with the south_migrationhistory table.") % self.__dict__
+
+
 class CircularDependency(SouthError):
     def __init__(self, trace):
         self.trace = trace

south/migration/__init__.py

             result.append(migration)
     return result
 
+def check_migration_histories(histories):
+    ghosts = find_ghost_migrations(histories)
+    if ghosts:
+        raise exceptions.GhostMigrations(ghosts)
+
 def migrate_app(migrations, target_name=None, resolve_mode=None, fake=False, db_dry_run=False, yes=False, verbosity=0, load_inital_data=False, skip=False):
     
     app_name = migrations.app_name()
     check_dependencies(migrations)
     # Check there's no strange ones in the database
     histories = MigrationHistory.objects.filter(applied__isnull=False)
-    ghost_migrations = find_ghost_migrations(histories)
-    if ghost_migrations:
-        print " ! These migrations are in the database but not on disk:"
-        print "   - " + "\n   - ".join([str(m) for m in ghost_migrations])
-        print " ! I'm not trusting myself; fix this yourself by fiddling"
-        print " ! with the south_migrationhistory table."
-        return
+    check_migration_histories(histories)
     # Say what we're doing
     if verbosity:
         print "Running migrations for %s:" % app_name