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
Example #2
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()