PageRenderTime 30ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0015_tagging.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 116 lines | 97 code | 3 blank | 16 comment | 3 complexity | 61cb74edf87aa8c164b05d906ddc777c MD5 | raw file
  1"""
  2This migration script adds the tables necessary to support tagging of histories,
  3datasets, and history-dataset associations (user views of datasets).
  4
  5If using mysql, this script will display the following error, which is corrected in the next
  6migration script:
  7
  8history_dataset_association_tag_association table failed:  (OperationalError)
  9(1059, "Identifier name 'ix_history_dataset_association_tag_association_history_dataset_association_id'
 10is too long)
 11"""
 12
 13from sqlalchemy import *
 14from migrate import *
 15
 16import datetime
 17now = datetime.datetime.utcnow
 18
 19# Need our custom types, but don't import anything else from model
 20from galaxy.model.custom_types import *
 21
 22import logging
 23log = logging.getLogger( __name__ )
 24
 25metadata = MetaData()
 26
 27def display_migration_details():
 28    print ""
 29    print "This migration script adds the tables necessary to support tagging of histories,"
 30    print "datasets, and history-dataset associations (user views of datasets)."
 31    print ""
 32    print "If using mysql, this script will display the following error, which is "
 33    print "corrected in the next migration script:"
 34    print "history_dataset_association_tag_association table failed:  "
 35    print "(OperationalError) (1059, 'Identifier name "
 36    print "'ix_history_dataset_association_tag_association_history_dataset_association_id'"
 37    print "is too long)"
 38
 39
 40# New tables to support tagging of histories, datasets, and history-dataset associations.
 41Tag_table = Table( "tag", metadata,
 42    Column( "id", Integer, primary_key=True ),
 43    Column( "type", Integer ),
 44    Column( "parent_id", Integer, ForeignKey( "tag.id" ) ),
 45    Column( "name", TrimmedString(255) ),
 46    UniqueConstraint( "name" ) )
 47
 48HistoryTagAssociation_table = Table( "history_tag_association", metadata,
 49    Column( "history_id", Integer, ForeignKey( "history.id" ), index=True ),
 50    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
 51    Column( "user_tname", TrimmedString(255), index=True),
 52    Column( "value", TrimmedString(255), index=True),
 53    Column( "user_value", TrimmedString(255), index=True) )
 54
 55DatasetTagAssociation_table = Table( "dataset_tag_association", metadata,
 56    Column( "dataset_id", Integer, ForeignKey( "dataset.id" ), index=True ),
 57    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
 58    Column( "user_tname", TrimmedString(255), index=True),
 59    Column( "value", TrimmedString(255), index=True),
 60    Column( "user_value", TrimmedString(255), index=True) )
 61
 62HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata,
 63    Column( "history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id" ), index=True ),
 64    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
 65    Column( "user_tname", TrimmedString(255), index=True),
 66    Column( "value", TrimmedString(255), index=True),
 67    Column( "user_value", TrimmedString(255), index=True) )
 68
 69def upgrade(migrate_engine):
 70    metadata.bind = migrate_engine
 71    display_migration_details()
 72    metadata.reflect()
 73    try:
 74        Tag_table.create()
 75    except Exception, e:
 76        print str(e)
 77        log.debug( "Creating tag table failed: %s" % str( e ) )
 78    try:
 79        HistoryTagAssociation_table.create()
 80    except Exception, e:
 81        print str(e)
 82        log.debug( "Creating history_tag_association table failed: %s" % str( e ) )
 83    try:
 84        DatasetTagAssociation_table.create()
 85    except Exception, e:
 86        print str(e)
 87        log.debug( "Creating dataset_tag_association table failed: %s" % str( e ) )
 88    try:
 89        HistoryDatasetAssociationTagAssociation_table.create()
 90    except Exception, e:
 91        print str(e)
 92        log.debug( "Creating history_dataset_association_tag_association table failed: %s" % str( e ) )
 93
 94def downgrade(migrate_engine):
 95    metadata.bind = migrate_engine
 96    metadata.reflect()
 97    try:
 98        Tag_table.drop()
 99    except Exception, e:
100        print str(e)
101        log.debug( "Dropping tag table failed: %s" % str( e ) )
102    try:
103        HistoryTagAssociation_table.drop()
104    except Exception, e:
105        print str(e)
106        log.debug( "Dropping history_tag_association table failed: %s" % str( e ) )
107    try:
108        DatasetTagAssociation_table.drop()
109    except Exception, e:
110        print str(e)
111        log.debug( "Dropping dataset_tag_association table failed: %s" % str( e ) )
112    try:
113        HistoryDatasetAssociationTagAssociation_table.drop()
114    except Exception, e:
115        print str(e)
116        log.debug( "Dropping history_dataset_association_tag_association table failed: %s" % str( e ) )