PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ialbert/galaxy-genetrack
Python | 61 lines | 53 code | 4 blank | 4 comment | 14 complexity | 8e5d648ef0e89d090147a78986ce7b11 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( migrate_engine )
15db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, transactional=False ) )
16User_table = Table( "galaxy_user", metadata, autoload=True )
17HistoryDatasetAssociation_table = Table( "history_dataset_association", metadata, autoload=True )
18
19def boolean_false():
20   if migrate_engine.name == 'postgres' or migrate_engine.name == 'mysql':
21       return False
22   elif migrate_engine.name == 'sqlite':
23       return 0
24   else:
25       raise Exception( 'Unable to convert data for unknown database type: %s' % db )
26              
27def upgrade():
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():
61    pass