'Using the list of tool_version_dicts retrieved from the Tool Shed (one per changeset
revision up to the currently installed changeset revision), create the parent / child
pairs of tool versions.  Each dictionary contains { tool id : parent tool id } pairs.
This function is called only from Galaxy.'

def handle_tool_versions(self, tool_version_dicts, tool_shed_repository):

    context = self.app.install_model.context
    for tool_version_dict in tool_version_dicts:
        for (tool_guid, parent_id) in tool_version_dict.items():
            tool_version_using_tool_guid = self.get_tool_version(tool_guid)
            tool_version_using_parent_id = self.get_tool_version(parent_id)
            if (not tool_version_using_tool_guid):
                tool_version_using_tool_guid = self.app.install_model.ToolVersion(tool_id=tool_guid, tool_shed_repository=tool_shed_repository)
                context.add(tool_version_using_tool_guid)
                context.flush()
            if (not tool_version_using_parent_id):
                tool_version_using_parent_id = self.app.install_model.ToolVersion(tool_id=parent_id, tool_shed_repository=tool_shed_repository)
                context.add(tool_version_using_parent_id)
                context.flush()
            context.query(self.app.install_model.ToolVersionAssociation).filter(or_(and_((self.app.install_model.ToolVersionAssociation.table.c.parent_id == tool_version_using_parent_id.id), (self.app.install_model.ToolVersionAssociation.table.c.tool_id != tool_version_using_tool_guid.id)), and_((self.app.install_model.ToolVersionAssociation.table.c.parent_id != tool_version_using_parent_id.id), (self.app.install_model.ToolVersionAssociation.table.c.tool_id == tool_version_using_tool_guid.id)))).delete()
            context.flush()
            tool_version_association = self.get_tool_version_association(tool_version_using_parent_id, tool_version_using_tool_guid)
            if (not tool_version_association):
                tool_version_association = self.app.install_model.ToolVersionAssociation(tool_id=tool_version_using_tool_guid.id, parent_id=tool_version_using_parent_id.id)
                context.add(tool_version_association)
                context.flush()
