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

/lib/galaxy/webapps/community/model/migrate/versions/0002_add_tool_suite_column.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 50 lines | 38 code | 5 blank | 7 comment | 2 complexity | fcdb2223816645adace045540c1bff80 MD5 | raw file
 1"""
 2Migration script to add the suite column to the tool table.
 3"""
 4
 5from sqlalchemy import *
 6from sqlalchemy.orm import *
 7from migrate import *
 8from migrate.changeset import *
 9
10import sys, logging
11log = logging.getLogger( __name__ )
12log.setLevel(logging.DEBUG)
13handler = logging.StreamHandler( sys.stdout )
14format = "%(name)s %(levelname)s %(asctime)s %(message)s"
15formatter = logging.Formatter( format )
16handler.setFormatter( formatter )
17log.addHandler( handler )
18
19metadata = MetaData( migrate_engine )
20db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
21
22def upgrade():
23    print __doc__
24    metadata.reflect()
25    # Create and initialize imported column in job table.
26    Tool_table = Table( "tool", metadata, autoload=True )
27    c = Column( "suite", Boolean, default=False, index=True )
28    try:
29        # Create
30        c.create( Tool_table )
31        assert c is Tool_table.c.suite
32        # Initialize.
33        if migrate_engine.name == 'mysql' or migrate_engine.name == 'sqlite': 
34            default_false = "0"
35        elif migrate_engine.name == 'postgres':
36            default_false = "false"
37        db_session.execute( "UPDATE tool SET suite=%s" % default_false )
38    except Exception, e:
39        print "Adding suite column to the tool table failed: %s" % str( e )
40        log.debug( "Adding suite column to the tool table failed: %s" % str( e ) )
41    
42def downgrade():
43    metadata.reflect()
44    # Drop suite column from tool table.
45    Tool_table = Table( "tool", metadata, autoload=True )
46    try:
47        Tool_table.c.suite.drop()
48    except Exception, e:
49        print "Dropping column suite from the tool table failed: %s" % str( e )
50        log.debug( "Dropping column suite from the tool table failed: %s" % str( e ) )