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

/lib/galaxy/webapps/community/model/migrate/versions/0007_add_long_description_times_downloaded_columns.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 66 lines | 50 code | 8 blank | 8 comment | 0 complexity | 1dbb4197e8b926ea5bd2ffa773d0bbc7 MD5 | raw file
 1"""
 2Migration script to add the long_description and times_downloaded columns to the repository table.
 3"""
 4
 5from sqlalchemy import *
 6from sqlalchemy.orm import *
 7from migrate import *
 8from migrate.changeset import *
 9
10# Need our custom types, but don't import anything else from model
11from galaxy.model.custom_types import *
12
13import sys, logging
14log = logging.getLogger( __name__ )
15log.setLevel(logging.DEBUG)
16handler = logging.StreamHandler( sys.stdout )
17format = "%(name)s %(levelname)s %(asctime)s %(message)s"
18formatter = logging.Formatter( format )
19handler.setFormatter( formatter )
20log.addHandler( handler )
21
22metadata = MetaData( migrate_engine )
23db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
24
25def upgrade():
26    print __doc__
27    metadata.reflect()
28    # Create and initialize imported column in job table.
29    Repository_table = Table( "repository", metadata, autoload=True )
30    c = Column( "long_description" , TEXT )
31    try:
32        # Create
33        c.create( Repository_table )
34        assert c is Repository_table.c.long_description
35    except Exception, e:
36        print "Adding long_description column to the repository table failed: %s" % str( e )
37        log.debug( "Adding long_description column to the repository table failed: %s" % str( e ) )
38
39    c = Column( "times_downloaded" , Integer )
40    try:
41        # Create
42        c.create( Repository_table )
43        assert c is Repository_table.c.times_downloaded
44    except Exception, e:
45        print "Adding times_downloaded column to the repository table failed: %s" % str( e )
46        log.debug( "Adding times_downloaded column to the repository table failed: %s" % str( e ) )
47
48    cmd = "UPDATE repository SET long_description = ''"
49    db_session.execute( cmd )
50    cmd = "UPDATE repository SET times_downloaded = 0"
51    db_session.execute( cmd )
52    
53def downgrade():
54    metadata.reflect()
55    # Drop email_alerts column from repository table.
56    Repository_table = Table( "repository", metadata, autoload=True )
57    try:
58        Repository_table.c.long_description.drop()
59    except Exception, e:
60        print "Dropping column long_description from the repository table failed: %s" % str( e )
61        log.debug( "Dropping column long_description from the repository table failed: %s" % str( e ) )
62    try:
63        Repository_table.c.times_downloaded.drop()
64    except Exception, e:
65        print "Dropping column times_downloaded from the repository table failed: %s" % str( e )
66        log.debug( "Dropping column times_downloaded from the repository table failed: %s" % str( e ) )