PageRenderTime 36ms CodeModel.GetById 16ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0011_v0010_mysql_index_fix.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 65 lines | 63 code | 0 blank | 2 comment | 2 complexity | 88ea06fff8a258ae174503cad0e6aa0a MD5 | raw file
 1"""
 2This script fixes a problem introduced in 0010_hda_display_at_atuhz_table.py.  MySQL has a
 3name length limit and thus the index "ix_hdadaa_history_dataset_association_id" has to be
 4manually created.
 5"""
 6from sqlalchemy import *
 7from sqlalchemy.orm import *
 8from sqlalchemy.exc import *
 9from migrate import *
10from migrate.changeset import *
11
12import datetime
13now = datetime.datetime.utcnow
14
15import sys, logging
16log = logging.getLogger( __name__ )
17log.setLevel(logging.DEBUG)
18handler = logging.StreamHandler( sys.stdout )
19format = "%(name)s %(levelname)s %(asctime)s %(message)s"
20formatter = logging.Formatter( format )
21handler.setFormatter( formatter )
22log.addHandler( handler )
23
24# Need our custom types, but don't import anything else from model
25from galaxy.model.custom_types import *
26
27metadata = MetaData()
28
29def display_migration_details():
30    print "========================================"
31    print "This script fixes a problem introduced in the previous migration script ( 9->10 ).  MySQL"
32    print "has a name length limit and thus the index 'ix_hdadaa_history_dataset_association_id' has"
33    print "to be manually created."
34    print "========================================"
35
36HistoryDatasetAssociationDisplayAtAuthorization_table = Table( "history_dataset_association_display_at_authorization", metadata,
37    Column( "id", Integer, primary_key=True ),
38    Column( "create_time", DateTime, default=now ),
39    Column( "update_time", DateTime, index=True, default=now, onupdate=now ),
40    Column( "history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id" ), index=True ),
41    Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
42    Column( "site", TrimmedString( 255 ) ) )
43
44def upgrade(migrate_engine):
45    metadata.bind = migrate_engine
46    display_migration_details()
47    if migrate_engine.name == 'mysql':
48        # Load existing tables
49        metadata.reflect()
50        i = Index( "ix_hdadaa_history_dataset_association_id", HistoryDatasetAssociationDisplayAtAuthorization_table.c.history_dataset_association_id )
51        try:
52            i.create()
53        except Exception, e:
54            log.debug( "Adding index 'ix_hdadaa_history_dataset_association_id' to table 'history_dataset_association_display_at_authorization' table failed: %s" % str( e ) )
55
56def downgrade(migrate_engine):
57    metadata.bind = migrate_engine
58    if migrate_engine.name == 'mysql':
59        # Load existing tables
60        metadata.reflect()
61        i = Index( "ix_hdadaa_history_dataset_association_id", HistoryDatasetAssociationDisplayAtAuthorization_table.c.history_dataset_association_id )
62        try:
63            i.drop()
64        except Exception, e:
65            log.debug( "Removing index 'ix_hdadaa_history_dataset_association_id' from table 'history_dataset_association_display_at_authorization' table failed: %s" % str( e ) )