PageRenderTime 60ms CodeModel.GetById 50ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0018_ordered_tags_and_page_tags.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 118 lines | 107 code | 6 blank | 5 comment | 1 complexity | 16b5665ced58671287a58df6cc07fa89 MD5 | raw file
  1"""
  2This migration script provides support for (a) ordering tags by recency and
  3(b) tagging pages. This script deletes all existing tags.
  4"""
  5
  6from sqlalchemy import *
  7from sqlalchemy.orm import *
  8from sqlalchemy.exc import *
  9from migrate import *
 10import migrate.changeset
 11
 12import datetime
 13now = datetime.datetime.utcnow
 14
 15# Need our custom types, but don't import anything else from model
 16from galaxy.model.custom_types import *
 17
 18import logging
 19log = logging.getLogger( __name__ )
 20
 21metadata = MetaData()
 22
 23def display_migration_details():
 24    print ""
 25    print "This migration script provides support for (a) ordering tags by recency and"
 26    print "(b) tagging pages. This script deletes all existing tags."
 27
 28HistoryTagAssociation_table = Table( "history_tag_association", metadata,
 29    Column( "id", Integer, primary_key=True ),
 30    Column( "history_id", Integer, ForeignKey( "history.id" ), index=True ),
 31    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
 32    Column( "user_tname", TrimmedString(255), index=True),
 33    Column( "value", TrimmedString(255), index=True),
 34    Column( "user_value", TrimmedString(255), index=True) )
 35
 36DatasetTagAssociation_table = Table( "dataset_tag_association", metadata,
 37    Column( "id", Integer, primary_key=True ),
 38    Column( "dataset_id", Integer, ForeignKey( "dataset.id" ), index=True ),
 39    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
 40    Column( "user_tname", TrimmedString(255), index=True),
 41    Column( "value", TrimmedString(255), index=True),
 42    Column( "user_value", TrimmedString(255), index=True) )
 43
 44HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata,
 45    Column( "id", Integer, primary_key=True ),
 46    Column( "history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id" ), index=True ),
 47    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
 48    Column( "user_tname", TrimmedString(255), index=True),
 49    Column( "value", TrimmedString(255), index=True),
 50    Column( "user_value", TrimmedString(255), index=True) )
 51
 52PageTagAssociation_table = Table( "page_tag_association", metadata,
 53    Column( "id", Integer, primary_key=True ),
 54    Column( "page_id", Integer, ForeignKey( "page.id" ), index=True ),
 55    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
 56    Column( "user_tname", TrimmedString(255), index=True),
 57    Column( "value", TrimmedString(255), index=True),
 58    Column( "user_value", TrimmedString(255), index=True) )
 59
 60def upgrade(migrate_engine):
 61    metadata.bind = migrate_engine
 62    display_migration_details()
 63    metadata.reflect()
 64
 65    #
 66    # Recreate tables.
 67    #
 68    try:
 69        HistoryTagAssociation_table.drop()
 70        HistoryTagAssociation_table.create()
 71    except Exception, e:
 72        print "Recreating history_tag_association table failed: %s" % str( e )
 73        log.debug( "Recreating history_tag_association table failed: %s" % str( e ) )
 74
 75    try:
 76        DatasetTagAssociation_table.drop()
 77        DatasetTagAssociation_table.create()
 78    except Exception, e:
 79        print str(e)
 80        log.debug( "Recreating dataset_tag_association table failed: %s" % str( e ) )
 81
 82    try:
 83        HistoryDatasetAssociationTagAssociation_table.drop()
 84        HistoryDatasetAssociationTagAssociation_table.create()
 85    except OperationalError, e:
 86        # Handle error that results from and index name that is too long; this occurs
 87        # in MySQL.
 88        if str(e).find("CREATE INDEX") != -1:
 89            # Manually create index.
 90            i = Index( "ix_hda_ta_history_dataset_association_id", HistoryDatasetAssociationTagAssociation_table.c.history_dataset_association_id )
 91            try:
 92                i.create()
 93            except Exception, e:
 94                print str(e)
 95                log.debug( "Adding index 'ix_hda_ta_history_dataset_association_id' to table 'history_dataset_association_tag_association' table failed: %s" % str( e ) )
 96    except Exception, e:
 97        print str(e)
 98        log.debug( "Recreating history_dataset_association_tag_association table failed: %s" % str( e ) )
 99
100    # Create page_tag_association table.
101    try:
102        PageTagAssociation_table.create()
103    except Exception, e:
104        print str(e)
105        log.debug( "Creating page_tag_association table failed: %s" % str( e ) )
106
107def downgrade(migrate_engine):
108    metadata.bind = migrate_engine
109    metadata.reflect()
110
111    # No need to downgrade other tagging tables. They work fine with verision 16 code.
112
113    # Drop page_tag_association table.
114    try:
115        PageTagAssociation_table.drop()
116    except Exception, e:
117        print str(e)
118        log.debug( "Dropping page_tag_association table failed: %s" % str( e ) )