1 from django.core.management.base import BaseCommand
2 from django.core.management.color import no_style
3 from django.conf import settings
4 from django.db import models
5 from optparse import make_option
6 from south import migration
9 class Command(BaseCommand):
10 option_list = BaseCommand.option_list + (
11 make_option('--skip', action='store_true', dest='skip', default=False,
12 help='Will skip over out-of-order missing migrations'),
13 make_option('--merge', action='store_true', dest='merge', default=False,
14 help='Will run out-of-order missing migrations as they are - no rollbacks.'),
15 make_option('--only', action='store_true', dest='only', default=False,
16 help='Only runs or rolls back the migration specified, and none around it.'),
17 make_option('--fake', action='store_true', dest='fake', default=False,
18 help="Pretends to do the migrations, but doesn't actually execute them."),
20 make_option('--db-dry-run', action='store_true', dest='db_dry_run', default=False,
21 help="Doesn't execute the SQL generated by the db methods, and doesn't store a record that the migration(s) occurred. Useful to test migrations before applying them."),
23 if '--verbosity' not in [opt.get_opt_string() for opt in BaseCommand.option_list]:
25 make_option('--verbosity', action='store', dest='verbosity', default='1',
26 type='choice', choices=['0', '1', '2'],
27 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
29 help = "Runs migrations for all apps."
31 def handle(self, app=None, target=None, skip=False, merge=False, only=False, backwards=False, fake=False, db_dry_run=False, **options):
33 # Work out what the resolve mode is
34 resolve_mode = merge and "merge" or (skip and "skip" or None)
35 # Turn on db debugging
36 from south.db import db
39 # NOTE: THIS IS DUPLICATED FROM django.core.management.commands.syncdb
40 # This code imports any module named 'management' in INSTALLED_APPS.
41 # The 'management' module is the preferred way of listening to post_syncdb
42 # signals, and since we're sending those out with create_table migrations,
43 # we need apps to behave correctly.
44 for app_name in settings.INSTALLED_APPS:
46 __import__(app_name + '.management', {}, {}, [''])
47 except ImportError, exc:
49 if not msg.startswith('No module named') or 'management' not in msg:
51 # END DJANGO DUPE CODE
55 apps = [migration.get_app(app)]
57 apps = migration.get_migrated_apps()
58 silent = options.get('verbosity', 0) == 0
60 migration.migrate_app(
62 resolve_mode = resolve_mode,
65 db_dry_run = db_dry_run,
67 load_inital_data = True,