コード例 #1
0
ファイル: models.py プロジェクト: edwinwow/django-appschema
def create_schema(name, **options):
    """
    This function creates a schema and perform a syncdb on it.
    As we call some syncdb and migrate management commands, we can't rely on
    transaction support.
    We are going to catch any exception (including SystemExit).
    """
    try:
        cursor = connection.cursor()
        # We can't use params with system names
        cursor.execute('CREATE SCHEMA "%s"' % escape_schema_name(name))
        transaction.commit_unless_managed()
        
        defaults = {
            'verbosity': 0,
            'traceback': None,
            'noinput': True
        }
        defaults.update(options)
        
        sync_options = options
        # We never want migration to launch with syncdb call
        sync_options['migrate'] = False
        
        _, isolated_apps = get_apps()
        
        syncdb_apps(isolated_apps, name, **sync_options)
        migrate_apps(get_migration_candidates(isolated_apps), name, **options)
        schema_store.reset_path()
    except BaseException, e:
        drop_schema(name)
        raise Exception(str(e))
コード例 #2
0
    def 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):
        if not 'south' in settings.INSTALLED_APPS:
            raise CommandError('South is not installed.')

        verbosity = int(options.get('verbosity', 0))
        shared, isolated = get_apps()

        if options.get('all_apps', False):
            target = app
            app = None

        # Migrate each app
        if app:
            apps = [app]
        else:
            apps = settings.INSTALLED_APPS

        options.update({
            'target': target,
            'skip': skip,
            'merge': merge,
            'backwards': backwards,
            'fake': fake,
            'db_dry_run': db_dry_run,
            'show_list': show_list,
            'database': database,
            'delete_ghosts': delete_ghosts,
            'ignore_ghosts': ignore_ghosts
        })

        shared_apps = [x for x in get_migration_candidates(shared) if x in apps]
        isolated_apps = [x for x in get_migration_candidates(isolated) if x in apps]

        try:
            if len(shared_apps) > 0:
                if verbosity:
                    print "---------------------"
                    print "SHARED APPS migration"
                    print "---------------------\n"

                migrate_apps(shared_apps, None, **options)

            if len(isolated_apps) == 0:
                return

            schema_list = [x.name for x in Schema.objects.active()]
            for schema in schema_list:
                if verbosity:
                    print "\n----------------------------------"
                    print   "ISOLATED APPS migration on schema: %s" % schema
                    print   "----------------------------------\n"
                migrate_apps(isolated_apps, schema=schema, **options)
        finally:
            load_post_syncdb_signals()