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

/lib/galaxy/model/migrate/versions/0033_published_cols_for_histories_and_workflows.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 105 lines | 74 code | 15 blank | 16 comment | 3 complexity | 78499b7dc1551d404c23d98bcf58aff8 MD5 | raw file
  1"""
  2Migration script to add necessary columns for distinguishing between viewing/importing and publishing histories, \
  3workflows, and pages. Script adds published column to histories and workflows and importable column to pages.
  4"""
  5
  6from sqlalchemy import *
  7from sqlalchemy.orm import *
  8from migrate import *
  9from migrate.changeset import *
 10
 11import logging
 12log = logging.getLogger( __name__ )
 13
 14metadata = MetaData()
 15
 16def upgrade(migrate_engine):
 17    metadata.bind = migrate_engine
 18    print __doc__
 19    metadata.reflect()
 20
 21    # Create published column in history table.
 22    History_table = Table( "history", metadata, autoload=True )
 23    c = Column( "published", Boolean, index=True )
 24    try:
 25        c.create( History_table, index_name='ix_history_published')
 26        assert c is History_table.c.published
 27    except Exception, e:
 28        print "Adding published column to history table failed: %s" % str( e )
 29        log.debug( "Adding published column to history table failed: %s" % str( e ) )
 30
 31
 32    if migrate_engine.name != 'sqlite':
 33        # Create index for published column in history table.
 34        try:
 35            i = Index( "ix_history_published", History_table.c.published )
 36            i.create()
 37        except:
 38            # Mysql doesn't have a named index, but alter should work
 39            History_table.c.published.alter( unique=False )
 40
 41    # Create published column in stored workflows table.
 42    StoredWorkflow_table = Table( "stored_workflow", metadata, autoload=True )
 43    c = Column( "published", Boolean, index=True )
 44    try:
 45        c.create( StoredWorkflow_table, index_name='ix_stored_workflow_published')
 46        assert c is StoredWorkflow_table.c.published
 47    except Exception, e:
 48        print "Adding published column to stored_workflow table failed: %s" % str( e )
 49        log.debug( "Adding published column to stored_workflow table failed: %s" % str( e ) )
 50
 51    if migrate_engine.name != 'sqlite':
 52        # Create index for published column in stored workflows table.
 53        try:
 54            i = Index( "ix_stored_workflow_published", StoredWorkflow_table.c.published )
 55            i.create()
 56        except:
 57            # Mysql doesn't have a named index, but alter should work
 58            StoredWorkflow_table.c.published.alter( unique=False )
 59
 60    # Create importable column in page table.
 61    Page_table = Table( "page", metadata, autoload=True )
 62    c = Column( "importable", Boolean, index=True )
 63    try:
 64        c.create( Page_table, index_name='ix_page_importable')
 65        assert c is Page_table.c.importable
 66    except Exception, e:
 67        print "Adding importable column to page table failed: %s" % str( e )
 68        log.debug( "Adding importable column to page table failed: %s" % str( e ) )
 69
 70    if migrate_engine.name != 'sqlite':
 71        # Create index for importable column in page table.
 72        try:
 73            i = Index( "ix_page_importable", Page_table.c.importable )
 74            i.create()
 75        except:
 76            # Mysql doesn't have a named index, but alter should work
 77            Page_table.c.importable.alter( unique=False )
 78
 79def downgrade(migrate_engine):
 80    metadata.bind = migrate_engine
 81    metadata.reflect()
 82
 83    # Drop published column from history table.
 84    History_table = Table( "history", metadata, autoload=True )
 85    try:
 86        History_table.c.published.drop()
 87    except Exception, e:
 88        print "Dropping column published from history table failed: %s" % str( e )
 89        log.debug( "Dropping column published from history table failed: %s" % str( e ) )
 90
 91    # Drop published column from stored_workflow table.
 92    StoredWorkflow_table = Table( "stored_workflow", metadata, autoload=True )
 93    try:
 94        StoredWorkflow_table.c.published.drop()
 95    except Exception, e:
 96        print "Dropping column published from stored_workflow table failed: %s" % str( e )
 97        log.debug( "Dropping column published from stored_workflow table failed: %s" % str( e ) )
 98
 99    # Drop importable column from page table.
100    Page_table = Table( "page", metadata, autoload=True )
101    try:
102        Page_table.c.importable.drop()
103    except Exception, e:
104        print "Dropping column importable from page table failed: %s" % str( e )
105        log.debug( "Dropping column importable from page table failed: %s" % str( e ) )