Refactoring work to add in Workflow UUIDs

This commit is contained in:
Kyle Ellrott
2014-08-20 14:10:43 -07:00
parent a48c6410dc
commit 47f0f822f0
6 changed files with 88 additions and 2 deletions
+11 -1
View File
@@ -2974,6 +2974,7 @@ class StoredWorkflow( object, Dictifiable):
tag_str += ":" + tag.user_value
tags_str_list.append( tag_str )
rval['tags'] = tags_str_list
rval['latest_workflow_uuid'] = ( lambda uuid: str( uuid ) if self.latest_workflow.uuid else None )( self.latest_workflow.uuid )
return rval
@@ -2982,12 +2983,16 @@ class Workflow( object, Dictifiable ):
dict_collection_visible_keys = ( 'name', 'has_cycles', 'has_errors' )
dict_element_visible_keys = ( 'name', 'has_cycles', 'has_errors' )
def __init__( self ):
def __init__( self, uuid=None ):
self.user = None
self.name = None
self.has_cycles = None
self.has_errors = None
self.steps = []
if uuid is None:
self.uuid = uuid4()
else:
self.uuid = UUID(str(uuid))
def has_outputs_defined(self):
"""
@@ -2998,6 +3003,11 @@ class Workflow( object, Dictifiable ):
return True
return False
def to_dict( self, view='collection', value_mapper=None):
rval = super( Workflow, self ).to_dict( view=view, value_mapper = value_mapper )
rval['uuid'] = ( lambda uuid: str( uuid ) if uuid else None )( self.uuid )
return rval
class WorkflowStep( object ):
+2 -1
View File
@@ -690,7 +690,8 @@ model.Workflow.table = Table( "workflow", metadata,
Column( "stored_workflow_id", Integer, ForeignKey( "stored_workflow.id" ), index=True, nullable=False ),
Column( "name", TEXT ),
Column( "has_cycles", Boolean ),
Column( "has_errors", Boolean )
Column( "has_errors", Boolean ),
Column( "uuid", UUIDType, nullable=True )
)
model.WorkflowStep.table = Table( "workflow_step", metadata,
@@ -0,0 +1,55 @@
"""
Add UUIDs to workflows
"""
from sqlalchemy import *
from sqlalchemy.orm import *
from migrate import *
from migrate.changeset import *
from galaxy.model.custom_types import UUIDType, TrimmedString
import logging
log = logging.getLogger( __name__ )
metadata = MetaData()
"""
Because both workflow and job requests can be determined
based the a fixed data structure, their IDs are based on
hashing the data structure
"""
workflow_uuid_column = Column( "uuid", UUIDType, nullable=True )
def display_migration_details():
print "This migration script adds a UUID column to workflows"
def upgrade(migrate_engine):
print __doc__
metadata.bind = migrate_engine
metadata.reflect()
# Add the uuid colum to the workflow table
try:
workflow_table = Table( "workflow", metadata, autoload=True )
workflow_uuid_column.create( workflow_table )
assert workflow_uuid_column is workflow_table.c.uuid
except Exception, e:
print str(e)
log.error( "Adding column 'uuid' to workflow table failed: %s" % str( e ) )
return
def downgrade(migrate_engine):
metadata.bind = migrate_engine
metadata.reflect()
# Drop the workflow table's uuid column.
try:
workflow_table = Table( "workflow", metadata, autoload=True )
workflow_uuid = workflow_table.c.uuid
workflow_uuid.drop()
except Exception, e:
log.debug( "Dropping 'uuid' column from workflow table failed: %s" % ( str( e ) ) )
+2
View File
@@ -1791,6 +1791,8 @@ class UsesStoredWorkflowMixin( SharableItemSecurityMixin, UsesAnnotations ):
data['format-version'] = "0.1"
data['name'] = workflow.name
data['annotation'] = annotation_str
if workflow.uuid is not None:
data['uuid'] = str(workflow.uuid)
data['steps'] = {}
# For each step, rebuild the form and encode the state
for step in workflow.steps:
@@ -4,6 +4,7 @@ API operations for Workflows
from __future__ import absolute_import
import uuid
import logging
from sqlalchemy import desc, or_
from galaxy import exceptions, util
@@ -437,6 +438,17 @@ class WorkflowsAPIController(BaseAPIController, UsesStoredWorkflowMixin, UsesHis
query = trans.sa_session.query( trans.app.model.StoredWorkflow )
stored_workflow = query.get( workflow_id )
except Exception:
try:
#see if they have passed in the UUID for a workflow that is attached to a stored workflow
workflow_uuid = uuid.UUID(workflow_id)
stored_workflow = trans.sa_session.query(trans.app.model.StoredWorkflow).filter( and_(
trans.app.model.StoredWorkflow.latest_workflow_id == trans.app.model.Workflow.id,
trans.app.model.Workflow.uuid == workflow_uuid
)).first()
if stored_workflow is None:
raise exceptions.ObjectNotFound( "Workflow not found: %s" % workflow_id )
except:
pass #let the outer raise exception happen
raise exceptions.ObjectNotFound( "No such workflow found - invalid workflow identifier." )
if stored_workflow is None:
raise exceptions.ObjectNotFound( "No such workflow found." )
@@ -33,6 +33,12 @@ def main():
row.uuid = uuid.uuid4()
print "Setting dataset:", row.id, " UUID to ", row.uuid
model.context.flush()
for row in model.context.query( model.Workflow ):
if row.uuid is None:
row.uuid = uuid.uuid4()
print "Setting Workflow:", row.id, " UUID to ", row.uuid
model.context.flush()
if __name__ == "__main__":