PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0004_indexes_and_defaults.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 62 lines | 55 code | 3 blank | 4 comment | 2 complexity | 9d3c7063813ae4dc39d228ead5bce575 MD5 | raw file
 1from sqlalchemy import *
 2from sqlalchemy.orm import *
 3from migrate import *
 4import sys, logging
 5
 6log = logging.getLogger( __name__ )
 7log.setLevel(logging.DEBUG)
 8handler = logging.StreamHandler( sys.stdout )
 9format = "%(name)s %(levelname)s %(asctime)s %(message)s"
10formatter = logging.Formatter( format )
11handler.setFormatter( formatter )
12log.addHandler( handler )
13
14metadata = MetaData()
15
16def upgrade(migrate_engine):
17    db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
18    metadata.bind = migrate_engine
19    User_table = Table( "galaxy_user", metadata, autoload=True )
20    HistoryDatasetAssociation_table = Table( "history_dataset_association", metadata, autoload=True )
21    def boolean_false():
22       if migrate_engine.name == 'postgresql' or migrate_engine.name == 'mysql':
23           return False
24       elif migrate_engine.name == 'sqlite':
25           return 0
26       else:
27           raise Exception( 'Unable to convert data for unknown database type: %s' % migrate_engine.name)
28    # Load existing tables
29    metadata.reflect()
30    # Add 2 indexes to the galaxy_user table
31    i = Index( 'ix_galaxy_user_deleted', User_table.c.deleted )
32    try:
33        i.create()
34    except Exception, e:
35        log.debug( "Adding index 'ix_galaxy_user_deleted' to galaxy_user table failed: %s" % ( str( e ) ) )
36    i = Index( 'ix_galaxy_user_purged', User_table.c.purged )
37    try:
38        i.create()
39    except Exception, e:
40        log.debug( "Adding index 'ix_galaxy_user_purged' to galaxy_user table failed: %s" % ( str( e ) ) )
41    # Set the default data in the galaxy_user table, but only for null values
42    cmd = "UPDATE galaxy_user SET deleted = %s WHERE deleted is null"
43    cmd = cmd % boolean_false()
44    try:
45        db_session.execute( cmd )
46    except Exception, e:
47        log.debug( "Setting default data for galaxy_user.deleted column failed: %s" % ( str( e ) ) )
48    cmd = "UPDATE galaxy_user SET purged = %s WHERE purged is null"
49    cmd = cmd % boolean_false()
50    try:
51        db_session.execute( cmd )
52    except Exception, e:
53        log.debug( "Setting default data for galaxy_user.purged column failed: %s" % ( str( e ) ) )
54    # Add 1 index to the history_dataset_association table
55    i = Index( 'ix_hda_copied_from_library_dataset_dataset_association_id', HistoryDatasetAssociation_table.c.copied_from_library_dataset_dataset_association_id )
56    try:
57        i.create()
58    except Exception, e:
59        log.debug( "Adding index 'ix_hda_copied_from_library_dataset_dataset_association_id' to history_dataset_association table failed: %s" % ( str( e ) ) )
60def downgrade(migrate_engine):
61    metadata.bind = migrate_engine
62    pass