Syncs the database by calling its schema sync method.  If
        no specific schema has been set for this database, then
        the global database schema will be used.  If the dryRun
        flag is specified, then all the resulting commands will
        just be logged to the current logger and not actually 
        executed on the database.
        
        :note       From version 0.6.0 on, this method now accepts a mutable
                    keyword dictionary of values.  You can supply any member 
                    value for either the <orb.LookupOptions> or
                    <orb.Context>, 'options' for
                    an instance of the <orb.Context>
        
        :return     <bool> success

def sync(self, models=None, **context):
        """
        Syncs the database by calling its schema sync method.  If
        no specific schema has been set for this database, then
        the global database schema will be used.  If the dryRun
        flag is specified, then all the resulting commands will
        just be logged to the current logger and not actually 
        executed on the database.
        
        :note       From version 0.6.0 on, this method now accepts a mutable
                    keyword dictionary of values.  You can supply any member 
                    value for either the <orb.LookupOptions> or
                    <orb.Context>, 'options' for
                    an instance of the <orb.Context>
        
        :return     <bool> success
        """
        context = orb.Context(**context)

        # collect the information for this database
        conn = self.__connection
        all_models = orb.system.models(orb.Model).values()
        all_models.sort(cmp=lambda x,y: cmp(x.schema(), y.schema()))

        tables = [model for model in all_models if issubclass(model, orb.Table) and
                  not model.schema().testFlags(orb.Schema.Flags.Abstract) and
                  (not models or model.schema().name() in models)]
        views = [model for model in all_models if issubclass(model, orb.View) and
                  not model.schema().testFlags(orb.Schema.Flags.Abstract) and
                  (not models or model.schema().name() in models)]

        # initialize the database
        event = orb.events.SyncEvent(context=context)
        self.__connection.onSync(event)

        namespaces = set()
        info = conn.schemaInfo(context)

        # create new models
        for model in tables:
            if model.schema().dbname() not in info:
                namespace = model.schema().namespace()
                if namespace and namespace not in namespaces:
                    conn.addNamespace(namespace, context)
                    namespaces.add(namespace)

                conn.createModel(model, context, includeReferences=False, owner=self.username())

        # update after any newly created tables get generated
        info = conn.schemaInfo(context)

        for model in tables:
            try:
                model_info = info[model.schema().dbname()]
            except KeyError:
                continue
            else:
                # collect the missing columns and indexes
                add = defaultdict(list)
                for col in model.schema().columns(recurse=False).values():
                    if col.field() not in (model_info['fields'] or []) and not col.testFlag(col.Flags.Virtual):
                        add['fields'].append(col)

                for index in model.schema().indexes(recurse=False).values():
                    if index.dbname() not in (model_info['indexes'] or []):
                        add['indexes'].append(index)

                # alter the model with the new indexes
                if add['fields'] or add['indexes']:
                    conn.alterModel(model, context, add=add, owner=self.username())

        for model in tables:
            # call the sync event
            event = orb.events.SyncEvent(model=model)
            if model.processEvent(event):
                model.onSync(event)

        # sync views last
        for view in views:
            conn.createModel(view, context)
            event = orb.events.SyncEvent(model=view)
            if view.processEvent(event):
                view.onSync(event)