Project

General

Profile

Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / south / management / commands / migrationcheck.py @ d1a4905f

History | View | Annotate | Download (2.59 KB)

1
from django.core.exceptions import ImproperlyConfigured
2
from django.core.management import call_command, CommandError
3
from django.core.management.base import BaseCommand
4
from django.conf import settings
5
from django.db.models import loading
6
from django.test import simple
7

    
8
from south.migration import Migrations
9
from south.exceptions import NoMigrations
10
from south.hacks import hacks
11

    
12
class Command(BaseCommand):
13
    help = "Runs migrations for each app in turn, detecting missing depends_on values."
14
    usage_str = "Usage: ./manage.py migrationcheck"
15

    
16
    def handle(self, check_app_name=None, **options):
17
        runner = simple.DjangoTestSuiteRunner(verbosity=0)
18
        err_msg = "Failed to migrate %s; see output for hints at missing dependencies:\n"
19
        hacks.patch_flush_during_test_db_creation()
20
        failures = 0
21
        if check_app_name is None:
22
            app_names = settings.INSTALLED_APPS
23
        else:
24
            app_names = [check_app_name]
25
        for app_name in app_names:
26
            app_label = app_name.split(".")[-1]
27
            if app_name == 'south':
28
                continue
29

    
30
            try:
31
                Migrations(app_name)
32
            except (NoMigrations, ImproperlyConfigured):
33
                continue
34
            app = loading.get_app(app_label)
35

    
36
            verbosity = int(options.get('verbosity', 1))
37
            if verbosity >= 1:
38
                self.stderr.write("processing %s\n" % app_name)
39

    
40
            old_config = runner.setup_databases()
41
            try:
42
                call_command('migrate', app_label, noinput=True, verbosity=verbosity)
43
                for model in loading.get_models(app):
44
                    dummy = model._default_manager.exists()
45
            except (KeyboardInterrupt, SystemExit):
46
                raise
47
            except Exception, e:
48
                failures += 1
49
                if verbosity >= 1:
50
                    self.stderr.write(err_msg % app_name)
51
                    self.stderr.write("%s\n" % e)
52
            finally:
53
                runner.teardown_databases(old_config)
54
        if failures > 0:
55
            raise CommandError("Missing depends_on found in %s app(s)." % failures)
56
        self.stderr.write("No missing depends_on found.\n")
57
#
58
#for each app:
59
#    start with blank db.
60
#    syncdb only south (and contrib?)
61
#
62
#    migrate a single app all the way up.  any errors is missing depends_on.
63
#    for all models of that app, try the default manager:
64
#        from django.db.models import loading
65
#        for m in loading.get_models(loading.get_app('a')):
66
#            m._default_manager.exists()
67
#    Any error is also a missing depends on.