Skip to content

Instantly share code, notes, and snippets.

@artemrizhov
Created October 30, 2013 15:49
Show Gist options
  • Save artemrizhov/7234995 to your computer and use it in GitHub Desktop.
Save artemrizhov/7234995 to your computer and use it in GitHub Desktop.
This adds multiple databases support to South. Import the module in your migration. Set `database` attribute on your migration class to the alias of db that you want to use. Access the database api via self.db.
from south import db as db_module
from south.migration.migrators import Migrator, DryRunMigrator
def with_db(run_migration):
def run_migration_with_db(self, migration, database):
migration_db = getattr(migration._migration.Migration,
'database', None)
if migration_db is not None:
print "Switching to db '%s'" % migration_db
_pev_db = db_module.db
db_module.db = db_module.dbs[migration_db]
migration.migration_instance().db = db_module.db
database = migration_db
try:
run_migration(self, migration, database)
finally:
if migration_db is not None:
print "Switching back to previous db"
db_module.db = _pev_db
delattr(migration.migration_instance(), 'db')
return run_migration_with_db
Migrator.run_migration = with_db(Migrator.run_migration)
DryRunMigrator.run_migration = with_db(DryRunMigrator.run_migration)
@chescales
Copy link

Incredible but works 😮 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment