2 from django
.core
.management
.base
import BaseCommand
, CommandError
3 from optparse
import make_option
4 from django
.conf
import settings
5 from StringIO
import StringIO
7 from dmigrations
.migration_state
import MigrationState
, table_present
8 from dmigrations
.migration_db
import MigrationDb
9 from dmigrations
.exceptions
import *
11 class Command(BaseCommand
):
13 ./manage.py dmigrate apply M1 M1 - Apply specified migrations
14 ./manage.py dmigrate unapply M1 M2 - Unapply specified migration
15 ./manage.py dmigrate mark_as_applied M1 M2 - Mark specified migrations as applied without running them
16 ./manage.py dmigrate mark_as_unapplied M1 M2 - Unapply specified migration as unapplied without running them
17 ./manage.py dmigrate cat M1 M2 - Print specified migrations
19 ./manage.py dmigrate all - Run all migrations
20 ./manage.py dmigrate up - Apply oldest unapplied migration
21 ./manage.py dmigrate down - Unapply newest applied migration
23 ./manage.py dmigrate to M - Apply all migrations up to M, upapply all migrations newer than M
24 ./manage.py dmigrate upto M - Apply all migrations up to M
25 ./manage.py dmigrate downto M - Upapply all migrations newer than M
27 ./manage.py dmigrate init - Ensure migration system is initialized
29 ./manage.py dmigrate list - List all migrations and their state
30 ./manage.py dmigrate help - Display this message
32 args
= '[command] [arguments]'
33 option_list
= BaseCommand
.option_list
+ (
34 # NOTE: dev has three values, True, False, and None
35 # None works like False except when we're migrating
36 # from the old migration system and we need to know
37 # if DEV migrations were run or not.
38 make_option('--dev', action
='store_true', dest
='dev',
39 help='Run development migrations (DEV in the filename) as well'),
40 make_option('--nodev', action
='store_false', dest
='dev',
41 help='Exclude development migrations (DEV in the filename)'),
42 make_option('--print-plan', action
='store_true', dest
='print_plan',
43 help='Only print plan'),
44 make_option('--verbosity', action
='store', dest
='verbosity',
45 default
='1', type='choice', choices
=['0', '1', '2'],
46 help='Verbosity level; 0=minimal, 1=normal output, 2=all output'),
48 requires_model_validation
= False
50 def handle(self
, *args
, **options
):
52 migrations_dir
= settings
.DMIGRATIONS_DIR
53 except AttributeError:
54 print "You need to add DMIGRATIONS_DIR to your settings"
56 migration_db
= MigrationDb(directory
= migrations_dir
)
57 migration_state
= MigrationState(
58 migration_db
= migration_db
, dev
= options
.get('dev')
60 verbosity
= int(options
.get('verbosity', 1))
62 if not args
or args
[0] == 'help':
63 # TODO: Do this without calling os.system
64 os
.system('./manage.py help dmigrate')
67 elif args
[0] in 'all up down upto downto to apply unapply'.split():
68 migration_state
.init()
69 for (migration_name
, action
) in migration_state
.plan(*args
):
70 migration
= migration_db
.load_migration_object(migration_name
)
73 print "Applying migration %s" % migration
.name
74 if not options
.get('print_plan'):
75 migration_state
.apply(migration_name
)
78 print "Unapplying migration %s" % migration
.name
79 if not options
.get('print_plan'):
80 migration_state
.unapply(migration_name
)
82 elif args
[0] == 'mark_as_applied':
83 migration_state
.init()
85 resolved_name
= migration_state
.resolve_name(name
)
86 if resolved_name
== None:
87 raise NoSuchMigrationError(name
)
88 migration_state
.mark_as_applied(resolved_name
)
90 elif args
[0] == 'mark_as_unapplied':
91 migration_state
.init()
93 resolved_name
= migration_state
.resolve_name(name
)
94 if resolved_name
== None:
95 raise NoSuchMigrationError(name
)
96 migration_state
.mark_as_unapplied(resolved_name
)
98 elif args
[0] == 'list':
99 migration_state
.init()
100 for migration_name
in migration_db
.list():
101 if migration_state
.is_applied(migration_name
):
102 print "* [+] %s" % migration_name
104 print "* [ ] %s" % migration_name
105 migrations_not_in_db
= migration_state
.applied_but_not_in_db()
106 if migrations_not_in_db
:
107 print "These migrations are marked as applied but cannot " \
109 for migration_name
in migrations_not_in_db
:
110 print "* [?] %s" % migration_name
113 elif args
[0] == 'init':
114 migration_state
.init()
116 elif args
[0] == 'cat':
117 for name
in args
[1:]:
119 migration_db
.resolve_migration_path(name
), 'r'
125 'Argument should be one of: list, help, up, down, all, init, '
126 'apply, unapply, to, downto, upto, mark_as_applied, '
130 # Ensure Django permissions and content_types have been created
131 # NOTE: Don't run if django_content_type doesn't exist yet.
132 if table_present('django_content_type'):
133 from django
.contrib
.auth
.management
import create_permissions
134 from django
.db
import models
135 for app
in models
.get_apps():
137 create_permissions(app
, set(), 2)
139 create_permissions(app
, set(), 1)