Esempio n. 1
0
    def handle(self, *args, **kwargs):
        if len(args) == 0:
            raise CommandError('No SQL file specified')

        filename = args[0]
        if not os.path.exists(filename):
            raise CommandError('File "%s" does not exist.' % filename)

        with open(filename, 'rb') as fp:
            sql = fp.read()

        for instance in Schema.objects.all():
            schema_store.reset_path()
            schema_store.schema = instance.name
            schema_store.force_path()

            sys.stdout.write('%s ... ' % instance.name)
            try:
                cur = connections[DEFAULT_DB_ALIAS].cursor()
                cur.execute(sql)
                cur.close()
                sys.stdout.write(self.style.SQL_COLTYPE('✓'))
                sys.stdout.write('\n')
            except:
                sys.stdout.write(self.style.NOTICE('✗'))
                sys.stdout.write('\n')
                raise
Esempio n. 2
0
    def handle_noargs(self, hostname=None, schemaname=None, allschemas=True, **options):
        self.debug = bool(options.get('verbosity', 0))
        
        public_apps = get_public_apps()
        if (hostname is None and schemaname is None) and len(public_apps):
            with RunWithApps(public_apps):
                installed_apps = list(settings.INSTALLED_APPS)
                self.logging('Syncing "master" for %d apps:' % len(installed_apps))
                schema_store.reset_path()
                for app in installed_apps:
                    self.logging('\t- %s' % app)
                super(Command, self).handle_noargs(**options)
                self.logging('\t* Synced')

        schemas = get_schemas(hostname=hostname, schemaname=schemaname, allschemas=allschemas)
        isolated_apps = get_isolated_apps()
        
        for schema in schemas:
            with schema:
                with RunWithApps(isolated_apps):
                    installed_apps = list(settings.INSTALLED_APPS)
                    self.logging('\n')
                    self.logging('Syncing "%s" for %d apps:' % (schema, len(installed_apps)))
                    for app in installed_apps:
                        self.logging('\t- %s' % app)
                    super(Command, self).handle_noargs(**options)
                    self.logging('\t* Synced')
Esempio n. 3
0
def migrate_apps(apps, schema=None, **options):
    def wrapper(_apps, *args, **kwargs):
        load_post_syncdb_signals()
        for _app in _apps:
            migrate.Command().execute(_app, **kwargs)
    
    # Migrate without schema (on public)
    if not schema:
        schema_store.reset_path()
        run_with_apps(apps, wrapper, **options)
        return
    
    # Migrate with schema
    schema_store.schema = schema
    
    if len(db.connections.databases) > 1:
        raise Exception('Appschema doest not support multi databases (yet?)')
    
    try:
        # For other apps, we work with seach_path <schema>,public to
        # properly handle cross schema foreign keys.
        schema_store.set_path()
        
        # South sometimes needs a schema settings to be set and take it from
        # Django db settings SCHEMA
        db.connection.settings_dict['SCHEMA'] = schema
        
        run_with_apps(apps, wrapper, **options)
    finally:
        schema_store.clear()
        del db.connection.settings_dict['SCHEMA']
Esempio n. 4
0
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))
Esempio n. 5
0
    def handle_noargs(self,
                      hostname=None,
                      schemaname=None,
                      allschemas=True,
                      **options):
        self.debug = bool(options.get('verbosity', 0))

        public_apps = get_public_apps()
        if (hostname is None and schemaname is None) and len(public_apps):
            with RunWithApps(public_apps):
                installed_apps = list(settings.INSTALLED_APPS)
                self.logging('Syncing "master" for %d apps:' %
                             len(installed_apps))
                schema_store.reset_path()
                for app in installed_apps:
                    self.logging('\t- %s' % app)
                super(Command, self).handle_noargs(**options)
                self.logging('\t* Synced')

        schemas = get_schemas(hostname=hostname,
                              schemaname=schemaname,
                              allschemas=allschemas)
        isolated_apps = get_isolated_apps()

        for schema in schemas:
            with schema:
                with RunWithApps(isolated_apps):
                    installed_apps = list(settings.INSTALLED_APPS)
                    self.logging('\n')
                    self.logging('Syncing "%s" for %d apps:' %
                                 (schema, len(installed_apps)))
                    for app in installed_apps:
                        self.logging('\t- %s' % app)
                    super(Command, self).handle_noargs(**options)
                    self.logging('\t* Synced')
Esempio n. 6
0
    def handle(self, *args, **options):
        self.debug = bool(options.get('verbosity', 0))
        hostname = options.pop('hostname')
        schemaname = options.pop('schemaname')
        allschemas = options.pop('allschemas')

        args = list(args)
        appName = args[0] if len(args) > 0 else None
        public_apps = [
            app for app in get_public_apps(appName=appName) if migratable(app)
        ]

        if (hostname is None and schemaname is None) and len(public_apps):
            self.logging('Migrating "master" for %d apps:' % len(public_apps))
            schema_store.reset_path()
            for app in public_apps:
                self.logging('\t- ' + app)
                if len(args) > 0:
                    args[0] = app
                else:
                    args.append(app)
                super(Command, self).handle(*args, **options)

        schemas = get_schemas(hostname=hostname,
                              schemaname=schemaname,
                              allschemas=allschemas)
        isolated_apps = [
            app for app in get_isolated_apps(appName=appName)
            if migratable(app)
        ]

        for schema in schemas:
            self.logging('\n')
            with schema:
                self.logging('Migrating "%s" for %d apps:' %
                             (schema, len(isolated_apps)))
                for app in isolated_apps:
                    with RunWithApps([app]):
                        if len(args) > 0:
                            args[0] = app
                        else:
                            args.append(app)

                        super(Command, self).handle(*args, **options)
                        self.logging('\t- %s (Migrated)' % app)

        if hostname is None and schemaname is None and len(schemas):
            migrations = []
            with schemas[0]:
                for migration in MigrationHistory.objects.all():
                    migrations.append(migration)
            MigrationHistory.objects.all().delete()
            for migration in migrations:
                migration.save()
Esempio n. 7
0
def _syncdb_apps(apps, schema=None, force_close=True, **options):
    """
    This function simply call syncdb command (Django or South one) for
    select apps only.
    """
    def wrapper(_apps, *args, **kwargs):
        load_post_syncdb_signals()
        return syncdb.Command().execute(**kwargs)

    # Force connection close
    if force_close:
        db.connection.close()
        db.connection.connection = None

    # Force default DB if not specified
    options['database'] = options.get('database', db.DEFAULT_DB_ALIAS)

    # Syncdb without schema (on public)
    if not schema:
        schema_store.reset_path()
        return run_with_apps(apps, wrapper, **options)

    # Syncdb with schema
    #
    # We first handle the case of apps that are both shared and isolated.
    # As this tables are already present in public schema, we can't sync
    # with a search_path <schema>,public

    shared_apps, _ = get_apps()
    both_apps = [x for x in apps if x in shared_apps]
    shared_apps = [x for x in apps if x not in both_apps]

    schema_store.schema = schema

    if 'south' in apps:
        try:
            schema_store.force_path()
            run_with_apps(both_apps, wrapper, **options)
        except ValueError:
            pass

    try:
        # For other apps, we work with seach_path <schema>,public to
        # properly handle cross schema foreign keys.
        schema_store.set_path()
        run_with_apps(shared_apps, wrapper, **options)
    finally:
        schema_store.clear()
Esempio n. 8
0
 def handle(self, *args, **options):
     self.debug = bool(options.get('verbosity', 0))
     hostname = options.pop('hostname')
     schemaname = options.pop('schemaname')
     allschemas = options.pop('allschemas')
     
     args = list(args)
     appName = args[0] if len(args) > 0 else None
     public_apps = [app for app in get_public_apps(appName=appName) if migratable(app)]
     
     if (hostname is None and schemaname is None) and len(public_apps):
         self.logging('Migrating "master" for %d apps:' % len(public_apps))
         schema_store.reset_path()
         for app in public_apps:
             self.logging('\t- ' + app)
             if len(args) > 0:
                 args[0] = app
             else:
                 args.append(app)
             super(Command, self).handle(*args, **options)
     
     schemas = get_schemas(hostname=hostname, schemaname=schemaname, allschemas=allschemas)
     isolated_apps = [app for app in get_isolated_apps(appName=appName) if migratable(app)]
     
     for schema in schemas:
         self.logging('\n')
         with schema:
             self.logging('Migrating "%s" for %d apps:' % (schema, len(isolated_apps)))
             for app in isolated_apps:
                 with RunWithApps([app]):
                     if len(args) > 0:
                         args[0] = app
                     else:
                         args.append(app)
                 
                     super(Command, self).handle(*args, **options)
                     self.logging('\t- %s (Migrated)' % app)
                     
     if hostname is None and schemaname is None and len(schemas):
         migrations = []
         with schemas[0]:
             for migration in MigrationHistory.objects.all():
                 migrations.append(migration)
         MigrationHistory.objects.all().delete()
         for migration in migrations:
             migration.save()