root / env / lib / python2.7 / site-packages / south / management / commands / syncdb.py @ d1a4905f
History | View | Annotate | Download (4.35 KB)
1 |
"""
|
---|---|
2 |
Overridden syncdb command
|
3 |
"""
|
4 |
|
5 |
import sys |
6 |
from optparse import make_option |
7 |
|
8 |
from django.core.management.base import NoArgsCommand, BaseCommand |
9 |
from django.core.management.color import no_style |
10 |
from django.utils.datastructures import SortedDict |
11 |
from django.core.management.commands import syncdb |
12 |
from django.conf import settings |
13 |
from django.db import models |
14 |
from django.db.models.loading import cache |
15 |
from django.core import management |
16 |
|
17 |
from south.db import dbs |
18 |
from south import migration |
19 |
from south.exceptions import NoMigrations |
20 |
|
21 |
def get_app_label(app): |
22 |
return '.'.join( app.__name__.split('.')[0:-1] ) |
23 |
|
24 |
class Command(NoArgsCommand): |
25 |
option_list = syncdb.Command.option_list + ( |
26 |
make_option('--migrate', action='store_true', dest='migrate', default=False, |
27 |
help='Tells South to also perform migrations after the sync. Default for during testing, and other internal calls.'),
|
28 |
make_option('--all', action='store_true', dest='migrate_all', default=False, |
29 |
help='Makes syncdb work on all apps, even migrated ones. Be careful!'),
|
30 |
) |
31 |
if '--verbosity' not in [opt.get_opt_string() for opt in syncdb.Command.option_list]: |
32 |
option_list += ( |
33 |
make_option('--verbosity', action='store', dest='verbosity', default='1', |
34 |
type='choice', choices=['0', '1', '2'], |
35 |
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
|
36 |
) |
37 |
help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created, except those which use migrations."
|
38 |
|
39 |
def handle_noargs(self, migrate_all=False, **options): |
40 |
|
41 |
# Import the 'management' module within each installed app, to register
|
42 |
# dispatcher events.
|
43 |
# This is copied from Django, to fix bug #511.
|
44 |
try:
|
45 |
from django.utils.importlib import import_module |
46 |
except ImportError: |
47 |
pass # TODO: Remove, only for Django1.0 |
48 |
else:
|
49 |
for app_name in settings.INSTALLED_APPS: |
50 |
try:
|
51 |
import_module('.management', app_name)
|
52 |
except ImportError, exc: |
53 |
msg = exc.args[0]
|
54 |
if not msg.startswith('No module named') or 'management' not in msg: |
55 |
raise
|
56 |
|
57 |
# Work out what uses migrations and so doesn't need syncing
|
58 |
apps_needing_sync = [] |
59 |
apps_migrated = [] |
60 |
for app in models.get_apps(): |
61 |
app_label = get_app_label(app) |
62 |
if migrate_all:
|
63 |
apps_needing_sync.append(app_label) |
64 |
else:
|
65 |
try:
|
66 |
migrations = migration.Migrations(app_label) |
67 |
except NoMigrations:
|
68 |
# It needs syncing
|
69 |
apps_needing_sync.append(app_label) |
70 |
else:
|
71 |
# This is a migrated app, leave it
|
72 |
apps_migrated.append(app_label) |
73 |
verbosity = int(options.get('verbosity', 0)) |
74 |
|
75 |
# Run syncdb on only the ones needed
|
76 |
if verbosity:
|
77 |
print "Syncing..." |
78 |
|
79 |
old_installed, settings.INSTALLED_APPS = settings.INSTALLED_APPS, apps_needing_sync |
80 |
old_app_store, cache.app_store = cache.app_store, SortedDict([ |
81 |
(k, v) for (k, v) in cache.app_store.items() |
82 |
if get_app_label(k) in apps_needing_sync |
83 |
]) |
84 |
|
85 |
# This will allow the setting of the MySQL storage engine, for example.
|
86 |
for db in dbs.values(): |
87 |
db.connection_init() |
88 |
|
89 |
# OK, run the actual syncdb
|
90 |
syncdb.Command().execute(**options) |
91 |
|
92 |
settings.INSTALLED_APPS = old_installed |
93 |
cache.app_store = old_app_store |
94 |
|
95 |
# Migrate if needed
|
96 |
if options.get('migrate', True): |
97 |
if verbosity:
|
98 |
print "Migrating..." |
99 |
management.call_command('migrate', **options)
|
100 |
|
101 |
# Be obvious about what we did
|
102 |
if verbosity:
|
103 |
print "\nSynced:\n > %s" % "\n > ".join(apps_needing_sync) |
104 |
|
105 |
if options.get('migrate', True): |
106 |
if verbosity:
|
107 |
print "\nMigrated:\n - %s" % "\n - ".join(apps_migrated) |
108 |
else:
|
109 |
if verbosity:
|
110 |
print "\nNot synced (use migrations):\n - %s" % "\n - ".join(apps_migrated) |
111 |
print "(use ./manage.py migrate to migrate these)" |