PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0006_change_qual_datatype.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 60 lines | 58 code | 0 blank | 2 comment | 0 complexity | a7ce98a167ab3caaee879f6bf8850fdc MD5 | raw file
 1"""
 2This migration script changes certain values in the history_dataset_association.extension
 3column, specifically 'qual' is chaged to be 'qual454'.
 4"""
 5from sqlalchemy import *
 6from sqlalchemy.orm import *
 7from migrate import *
 8import sys, logging
 9
10log = logging.getLogger( __name__ )
11log.setLevel(logging.DEBUG)
12handler = logging.StreamHandler( sys.stdout )
13format = "%(name)s %(levelname)s %(asctime)s %(message)s"
14formatter = logging.Formatter( format )
15handler.setFormatter( formatter )
16log.addHandler( handler )
17
18metadata = MetaData()
19
20def display_migration_details():
21    print "========================================"
22    print "This migration script changes certain values in the history_dataset_association.extension"
23    print "column, specifically 'qual' is chaged to be 'qual454'."
24    print "========================================"
25
26
27def upgrade(migrate_engine):
28    display_migration_details()
29    metadata.bind = migrate_engine
30    db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
31    HistoryDatasetAssociation_table = Table( "history_dataset_association", metadata, autoload=True )
32    # Load existing tables
33    metadata.reflect()
34    # Add 2 indexes to the galaxy_user table
35    i = Index( 'ix_hda_extension', HistoryDatasetAssociation_table.c.extension )
36    try:
37        i.create()
38    except Exception, e:
39        log.debug( "Adding index 'ix_hda_extension' to history_dataset_association table failed: %s" % ( str( e ) ) )
40
41    # Set the default data in the galaxy_user table, but only for null values
42    cmd = "UPDATE history_dataset_association SET extension = 'qual454' WHERE extension = 'qual' and peek like \'>%%\'"
43    try:
44        db_session.execute( cmd )
45    except Exception, e:
46        log.debug( "Resetting extension qual to qual454 in history_dataset_association failed: %s" % ( str( e ) ) )
47    cmd = "UPDATE history_dataset_association SET extension = 'qualsolexa' WHERE extension = 'qual' and peek not like \'>%%\'"
48    try:
49        db_session.execute( cmd )
50    except Exception, e:
51        log.debug( "Resetting extension qual to qualsolexa in history_dataset_association failed: %s" % ( str( e ) ) )
52    # Add 1 index to the history_dataset_association table
53    try:
54        i.drop()
55    except Exception, e:
56        log.debug( "Dropping index 'ix_hda_extension' to history_dataset_association table failed: %s" % ( str( e ) ) )
57
58def downgrade(migrate_engine):
59    metadata.bind = migrate_engine
60    pass