root / env / lib / python2.7 / site-packages / south / management / commands / convert_to_south.py @ d1a4905f
History | View | Annotate | Download (3.59 KB)
1 |
"""
|
---|---|
2 |
Quick conversion command module.
|
3 |
"""
|
4 |
|
5 |
from optparse import make_option |
6 |
import sys |
7 |
|
8 |
from django.core.management.base import BaseCommand |
9 |
from django.core.management.color import no_style |
10 |
from django.conf import settings |
11 |
from django.db import models |
12 |
from django.core import management |
13 |
from django.core.exceptions import ImproperlyConfigured |
14 |
|
15 |
from south.migration import Migrations |
16 |
from south.hacks import hacks |
17 |
from south.exceptions import NoMigrations |
18 |
|
19 |
class Command(BaseCommand): |
20 |
|
21 |
option_list = BaseCommand.option_list |
22 |
if '--verbosity' not in [opt.get_opt_string() for opt in BaseCommand.option_list]: |
23 |
option_list += ( |
24 |
make_option('--verbosity', action='store', dest='verbosity', default='1', |
25 |
type='choice', choices=['0', '1', '2'], |
26 |
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
|
27 |
) |
28 |
option_list += ( |
29 |
make_option('--delete-ghost-migrations', action='store_true', dest='delete_ghosts', default=False, |
30 |
help="Tells South to delete any 'ghost' migrations (ones in the database but not on disk)."),
|
31 |
make_option('--ignore-ghost-migrations', action='store_true', dest='ignore_ghosts', default=False, |
32 |
help="Tells South to ignore any 'ghost' migrations (ones in the database but not on disk) and continue to apply new migrations."),
|
33 |
) |
34 |
|
35 |
help = "Quickly converts the named application to use South if it is currently using syncdb."
|
36 |
|
37 |
def handle(self, app=None, *args, **options): |
38 |
|
39 |
# Make sure we have an app
|
40 |
if not app: |
41 |
print "Please specify an app to convert." |
42 |
return
|
43 |
|
44 |
# See if the app exists
|
45 |
app = app.split(".")[-1] |
46 |
try:
|
47 |
app_module = models.get_app(app) |
48 |
except ImproperlyConfigured:
|
49 |
print "There is no enabled application matching '%s'." % app |
50 |
return
|
51 |
|
52 |
# Try to get its list of models
|
53 |
model_list = models.get_models(app_module) |
54 |
if not model_list: |
55 |
print "This application has no models; this command is for applications that already have models syncdb'd." |
56 |
print "Make some models, and then use ./manage.py schemamigration %s --initial instead." % app |
57 |
return
|
58 |
|
59 |
# Ask South if it thinks it's already got migrations
|
60 |
try:
|
61 |
Migrations(app) |
62 |
except NoMigrations:
|
63 |
pass
|
64 |
else:
|
65 |
print "This application is already managed by South." |
66 |
return
|
67 |
|
68 |
# Finally! It seems we've got a candidate, so do the two-command trick
|
69 |
verbosity = int(options.get('verbosity', 0)) |
70 |
management.call_command("schemamigration", app, initial=True, verbosity=verbosity) |
71 |
|
72 |
# Now, we need to re-clean and sanitise appcache
|
73 |
hacks.clear_app_cache() |
74 |
hacks.repopulate_app_cache() |
75 |
|
76 |
# And also clear our cached Migration classes
|
77 |
Migrations._clear_cache() |
78 |
|
79 |
# Now, migrate
|
80 |
management.call_command( |
81 |
"migrate",
|
82 |
app, |
83 |
"0001",
|
84 |
fake=True,
|
85 |
verbosity=verbosity, |
86 |
ignore_ghosts=options.get("ignore_ghosts", False), |
87 |
delete_ghosts=options.get("delete_ghosts", False), |
88 |
) |
89 |
|
90 |
print
|
91 |
print "App '%s' converted. Note that South assumed the application's models matched the database" % app |
92 |
print "(i.e. you haven't changed it since last syncdb); if you have, you should delete the %s/migrations" % app |
93 |
print "directory, revert models.py so it matches the database, and try again." |