Andrew Godwin avatar Andrew Godwin committed be6c3a5

Fix multi-db recording of migrations

Comments (0)

Files changed (2)

south/migration/migrators.py

     @staticmethod
     def record(migration, database):
         # Record us as having done this
-        record = MigrationHistory.for_migration(migration)
+        record = MigrationHistory.for_migration(migration, database)
         record.applied = datetime.datetime.utcnow()
         if database != DEFAULT_DB_ALIAS:
             record.save(using=database)
     @staticmethod
     def record(migration, database):
         # Record us as having not done this
-        record = MigrationHistory.for_migration(migration)
+        record = MigrationHistory.for_migration(migration, database)
         if record.id is not None:
             if database != DEFAULT_DB_ALIAS:
                 record.delete(using=database)
 from django.db import models
+from south.db import DEFAULT_DB_ALIAS
 
 class MigrationHistory(models.Model):
     app_name = models.CharField(max_length=255)
     applied = models.DateTimeField(blank=True)
 
     @classmethod
-    def for_migration(cls, migration):
+    def for_migration(cls, migration, database):
         try:
-            return cls.objects.get(app_name=migration.app_label(),
-                                   migration=migration.name())
+            # Switch on multi-db-ness
+            if database != DEFAULT_DB_ALIAS:
+                # Django 1.2
+                objects = cls.objects.using(database)
+            else:
+                # Django <= 1.1
+                objects = cls.objects
+            return objects.get(
+                app_name=migration.app_label(),
+                migration=migration.name(),
+            )
         except cls.DoesNotExist:
-            return cls(app_name=migration.app_label(),
-                       migration=migration.name())
+            return cls(
+                app_name=migration.app_label(),
+                migration=migration.name(),
+            )
 
     def get_migrations(self):
         from south.migration.base import Migrations
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.