Projet

Général

Profil

« Précédent | Suivant » 

Révision bf1da717

Ajouté par Thomas Noël il y a plus de 9 ans

improve safemigrate command (#5781)

better readability, handle verbosity, prepare for safemigrate_schema

Voir les différences:

entrouvert/djommon/safemigrate/management/commands/safemigrate.py
1 1
from django.core.management.base import NoArgsCommand
2
from django.db import connections, router, models
3

  
2
from django.db import connection, models
4 3
from south.models import MigrationHistory
5
from south.management.commands import syncdb, migrate
6
from django.core.management.commands import syncdb as django_syncdb
4

  
5
from django.core.management.commands.syncdb import Command as DjangoSyncdbCommand
6
from south.management.commands.syncdb import Command as SouthSyncdbCommand
7
from south.management.commands.migrate import Command as SouthMigrateCommand
7 8

  
8 9

  
9 10
class Command(NoArgsCommand):
10
    option_list = django_syncdb.Command.option_list
11
    help = '''syncdb + migrate, with "migrate --fake app 0001" for apps with new initial migrations'''
11
    option_list = DjangoSyncdbCommand.option_list
12
    help = "syncdb and migrate the project, migrate '--fake app 0001' each app that has a new initial migration"
12 13

  
13 14
    def handle_noargs(self, *args, **options):
14

  
15
        verbosity = int(options['verbosity'])
15 16
        # step 1 : syncdb, create all apps without migrations
16
        syncdb.Command().execute(migrate_all=False, migrate=False, **options)
17

  
17
        SouthSyncdbCommand().execute(migrate_all=False, migrate=False, **options)
18 18
        # step 2 : detect and "fake 0001" all installed apps that had never
19 19
        # migrated (applications in database but not in south history)
20
        if verbosity > 0:
21
            print
22
        self.fake_if_needed(verbosity)
23
        # step 3 : migrate
24
        if verbosity > 0:
25
            print
26
        SouthMigrateCommand().execute(**options)
20 27

  
28
    def fake_if_needed(self, verbosity=1):
21 29
        # detect installed models
22 30
        # (code borrowed from django syncdb command)
23
        db = options.get('database')
24
        connection = connections[db]
25
        cursor = connection.cursor()
26 31
        tables = connection.introspection.table_names()
27 32
        def model_in_database(model):
28 33
            opts = model._meta
......
37 42
        applied_migrations = MigrationHistory.objects.filter(app_name__in=[app.app_label() for app in apps])
38 43
        applied_migrations_lookup = dict(('%s.%s' % (mi.app_name, mi.migration), mi) for mi in applied_migrations)
39 44

  
40
        print
41
        print 'Status after syncdb:'
42

  
45
        if verbosity > 0:
46
            print 'Status after syncdb:'
43 47
        for app in apps:
44 48
            # for each app with migrations, list already applied migrations
45 49
            applied_migrations = []
......
49 53
                    applied_migration = applied_migrations_lookup[full_name]
50 54
                    if applied_migration.applied:
51 55
                        applied_migrations.append(migration.name())
52

  
53 56
            # try all models in database, if there none, the application is new
54 57
            # (because south syncdb does not create any tables)
55 58
            new = True
......
57 60
                if model_in_database(m):
58 61
                    new = False
59 62
                    break
60

  
61 63
            if new:
62 64
                status = 'application-is-new'
63 65
            elif not applied_migrations:
......
65 67
            else:
66 68
                status = 'normal'
67 69

  
68
            print ' - %s: %s' % (app.app_label(), status)
69

  
70
            if verbosity > 0:
71
                print ' - %s: %s' % (app.app_label(), status)
70 72
            if status == 'migration-is-new':
71
                print '   need fake migration to 0001_initial'
73
                if verbosity > 0:
74
                    print '   need fake migration to 0001_initial'
72 75
                # migrate --fake gdc 0001
73
                migrate.Command().execute(app=app.app_label(), target='0001', fake=True)
74

  
75
        print
76
        migrate.Command().execute(**options)
76
                SouthMigrateCommand().execute(app=app.app_label(), target='0001', fake=True, verbosity=verbosity)

Formats disponibles : Unified diff