Commits

Simon Law committed c87c93a

Move all ORM creation to the Migration class

Comments (0)

Files changed (2)

south/migration/__init__.py

 from south.models import MigrationHistory
 from south.db import db
 from south.migration.base import all_migrations, Migrations
-from south.orm import FakeORM
 from south.signals import pre_migrate, post_migrate, ran_migration
 
 
         self.print_status(migration)
         # Get migration class
         klass = migration.migration().Migration
-        # Find its predecessor, and attach the ORM from that as prev_orm.
-        previous = migration.previous()
-        # First migration? The 'previous ORM' is empty.
-        if previous is None:
-            klass.prev_orm = FakeORM(None, app)
-        else:
-            klass.prev_orm = previous.migration().Migration.orm
         if self.fake:
             # If this is a 'fake' migration, do nothing.
             if self.verbosity:
             args = inspect.getargspec(runfunc)
             # Get the correct ORM.
             if self.torun == 'forwards':
-                orm = klass.orm
+                orm = migration.orm()
             else:
-                orm = klass.prev_orm
+                orm = migration.prev_orm()
             db.current_orm = orm
             # If the database doesn't support running DDL inside a transaction
             # *cough*MySQL*cough* then do a dry run first.
                         if len(args[0]) == 1:
                             klass().backwards()
                         else:
-                            klass().backwards(klass.prev_orm)
+                            klass().backwards(migration.prev_orm())
                     print
                     print ' ! The South developers regret this has happened, and would'
                     print ' ! like to gently persuade you to consider a slightly'

south/migration/base.py

 
 from south import exceptions
 from south.migration.utils import get_app_name
-from south.orm import LazyFakeORM
+from south.orm import LazyFakeORM, FakeORM
 from south.utils import memoize
 
 
         # Override some imports
         migration._ = lambda x: x  # Fake i18n
         migration.datetime = datetime
-        # Setup our FakeORM
-        migclass = migration.Migration
-        migclass.orm = LazyFakeORM(migclass, app_name)
         return migration
     migration = memoize(migration)
 
                 return True
             return False
 
+    def prev_orm(self):
+        previous = self.previous()
+        if previous is None:
+            # First migration? The 'previous ORM' is empty.
+            return FakeORM(None, self.app_name())
+        return previous.orm()
+    prev_orm = memoize(prev_orm)
+
+    def orm(self):
+        return LazyFakeORM(self.migration().Migration, self.app_name())
+    orm = memoize(orm)
+
 
 def get_app_name(app):
     """