| 1 | from sqlalchemy import * | 
|---|
| 2 | from sqlalchemy.orm import * | 
|---|
| 3 | from migrate import * | 
|---|
| 4 | import sys, logging | 
|---|
| 5 |  | 
|---|
| 6 | log = logging.getLogger( __name__ ) | 
|---|
| 7 | log.setLevel(logging.DEBUG) | 
|---|
| 8 | handler = logging.StreamHandler( sys.stdout ) | 
|---|
| 9 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" | 
|---|
| 10 | formatter = logging.Formatter( format ) | 
|---|
| 11 | handler.setFormatter( formatter ) | 
|---|
| 12 | log.addHandler( handler ) | 
|---|
| 13 |  | 
|---|
| 14 | metadata = MetaData( migrate_engine ) | 
|---|
| 15 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) | 
|---|
| 16 | User_table = Table( "galaxy_user", metadata, autoload=True ) | 
|---|
| 17 | HistoryDatasetAssociation_table = Table( "history_dataset_association", metadata, autoload=True ) | 
|---|
| 18 |  | 
|---|
| 19 | def 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 |                | 
|---|
| 27 | def 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 ) ) ) | 
|---|
| 60 | def downgrade(): | 
|---|
| 61 |     pass | 
|---|