src/cm/monkey_patches.py
author Simon Descarpentries <sid@sopinspace.com>
Tue, 18 Feb 2014 19:27:58 +0100
changeset 579 869f02c75687
parent 531 80859749e6b1
permissions -rw-r--r--
daily progress : suite/test style, config from single config file, attempt to test i18n in footer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
504
b2e0186daa5b Adds a category to comments, painted with colored vertical bar.
gibus
parents: 347
diff changeset
     1
from south.management.commands.migrate import  Command, list_migrations
347
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     2
from south.db import DEFAULT_DB_ALIAS
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     3
from south import migration
530
d2803c60af53 Add some import from south in order for django migrate to work properly
gibus
parents: 504
diff changeset
     4
from south.migration import Migrations
d2803c60af53 Add some import from south in order for django migrate to work properly
gibus
parents: 504
diff changeset
     5
from south.exceptions import NoMigrations
347
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     6
import sys
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     7
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     8
### RBA+GIB: prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
     9
def new_handle(self, app=None, target=None, skip=False, merge=False, backwards=False, fake=False, db_dry_run=False, show_list=False, database=DEFAULT_DB_ALIAS, delete_ghosts=False, ignore_ghosts=False, **options):
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    10
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    11
    # NOTE: THIS IS DUPLICATED FROM django.core.management.commands.syncdb
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    12
    # This code imports any module named 'management' in INSTALLED_APPS.
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    13
    # The 'management' module is the preferred way of listening to post_syncdb
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    14
    # signals, and since we're sending those out with create_table migrations,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    15
    # we need apps to behave correctly.
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    16
    from django.conf import settings
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    17
    for app_name in settings.INSTALLED_APPS:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    18
        try:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    19
            __import__(app_name + '.management', {}, {}, [''])
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    20
        except ImportError, exc:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    21
            msg = exc.args[0]
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    22
            if (not msg.startswith('No module named') and not msg.endswith(' is unknown') ) or 'management' not in msg:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    23
                raise
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    24
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    25
    # END DJANGO DUPE CODE
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    26
        
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    27
    # if all_apps flag is set, shift app over to target
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    28
    if options.get('all_apps', False):
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    29
        target = app
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    30
        app = None
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    31
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    32
    # Migrate each app
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    33
    if app:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    34
        try:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    35
            apps = [Migrations(app)]
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    36
        except NoMigrations:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    37
            print "The app '%s' does not appear to use migrations." % app
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    38
            print "./manage.py migrate " + self.args
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    39
            return
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    40
    else:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    41
        apps = list(migration.all_migrations())
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    42
        
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    43
    # Do we need to show the list of migrations?
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    44
    if show_list and apps:
531
80859749e6b1 Fix django migrate --list
gibus
parents: 530
diff changeset
    45
        list_migrations(apps, database, **options)
347
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    46
        
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    47
    if not show_list:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    48
            
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    49
        for app in apps:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    50
            result = migration.migrate_app(
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    51
                app,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    52
                target_name = target,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    53
                fake = fake,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    54
                db_dry_run = db_dry_run,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    55
                verbosity = int(options.get('verbosity', 0)),
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    56
                interactive = options.get('interactive', True),
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    57
                load_initial_data = not options.get('no_initial_data', False),
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    58
                merge = merge,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    59
                skip = skip,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    60
                database = database,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    61
                delete_ghosts = delete_ghosts,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    62
                ignore_ghosts = ignore_ghosts,
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    63
            )
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    64
            if result is False:
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    65
                sys.exit(1) # Migration failed, so the command fails.
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    66
0a359382701b prevent uno custom __import__ from messing with south import machinery (to discover south enabled dj apps)
Production Moz <dev@sopinspace.com>
parents:
diff changeset
    67
Command.handle = new_handle