| 1 | """ | 
|---|
| 2 | This migration script creates the new history_user_share_association table, and adds | 
|---|
| 3 | a new boolean type column to the history table.  This provides support for sharing | 
|---|
| 4 | histories in the same way that workflows are shared. | 
|---|
| 5 | """ | 
|---|
| 6 | from sqlalchemy import * | 
|---|
| 7 | from sqlalchemy.orm import * | 
|---|
| 8 | from migrate import * | 
|---|
| 9 | from migrate.changeset import * | 
|---|
| 10 | import sys, logging | 
|---|
| 11 |  | 
|---|
| 12 | log = logging.getLogger( __name__ ) | 
|---|
| 13 | log.setLevel(logging.DEBUG) | 
|---|
| 14 | handler = logging.StreamHandler( sys.stdout ) | 
|---|
| 15 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" | 
|---|
| 16 | formatter = logging.Formatter( format ) | 
|---|
| 17 | handler.setFormatter( formatter ) | 
|---|
| 18 | log.addHandler( handler ) | 
|---|
| 19 |  | 
|---|
| 20 | metadata = MetaData( migrate_engine ) | 
|---|
| 21 |  | 
|---|
| 22 | def display_migration_details(): | 
|---|
| 23 |     print "========================================" | 
|---|
| 24 |     print "This migration script creates the new history_user_share_association table, and adds" | 
|---|
| 25 |     print "a new boolean type column to the history table.  This provides support for sharing" | 
|---|
| 26 |     print "histories in the same way that workflows are shared." | 
|---|
| 27 |     print "========================================" | 
|---|
| 28 |  | 
|---|
| 29 | HistoryUserShareAssociation_table = Table( "history_user_share_association", metadata, | 
|---|
| 30 |     Column( "id", Integer, primary_key=True ), | 
|---|
| 31 |     Column( "history_id", Integer, ForeignKey( "history.id" ), index=True ), | 
|---|
| 32 |     Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ) | 
|---|
| 33 |     ) | 
|---|
| 34 |  | 
|---|
| 35 | def upgrade(): | 
|---|
| 36 |     display_migration_details() | 
|---|
| 37 |     # Load existing tables | 
|---|
| 38 |     metadata.reflect() | 
|---|
| 39 |     # Create the history_user_share_association table | 
|---|
| 40 |     try: | 
|---|
| 41 |         HistoryUserShareAssociation_table.create() | 
|---|
| 42 |     except Exception, e: | 
|---|
| 43 |         log.debug( "Creating history_user_share_association table failed: %s" % str( e ) ) | 
|---|
| 44 |     # Add 1 column to the history table | 
|---|
| 45 |     try: | 
|---|
| 46 |         History_table = Table( "history", metadata, autoload=True ) | 
|---|
| 47 |     except NoSuchTableError: | 
|---|
| 48 |         History_table = None | 
|---|
| 49 |         log.debug( "Failed loading table history" ) | 
|---|
| 50 |     if History_table: | 
|---|
| 51 |         try: | 
|---|
| 52 |             col = Column( 'importable', Boolean, index=True, default=False ) | 
|---|
| 53 |             col.create( History_table ) | 
|---|
| 54 |             assert col is History_table.c.importable | 
|---|
| 55 |         except Exception, e: | 
|---|
| 56 |             log.debug( "Adding column 'importable' to history table failed: %s" % ( str( e ) ) ) | 
|---|
| 57 |  | 
|---|
| 58 | def downgrade(): | 
|---|
| 59 |     # Load existing tables | 
|---|
| 60 |     metadata.reflect() | 
|---|
| 61 |     # Drop 1 column from the history table | 
|---|
| 62 |     try: | 
|---|
| 63 |         History_table = Table( "history", metadata, autoload=True ) | 
|---|
| 64 |     except NoSuchTableError: | 
|---|
| 65 |         History_table = None | 
|---|
| 66 |         log.debug( "Failed loading table history" ) | 
|---|
| 67 |     if History_table: | 
|---|
| 68 |         try: | 
|---|
| 69 |             col = History_table.c.importable | 
|---|
| 70 |             col.drop() | 
|---|
| 71 |         except Exception, e: | 
|---|
| 72 |             log.debug( "Dropping column 'importable' from history table failed: %s" % ( str( e ) ) ) | 
|---|
| 73 |     # Drop the history_user_share_association table | 
|---|
| 74 |     try: | 
|---|
| 75 |         HistoryUserShareAssociation_table.drop() | 
|---|
| 76 |     except Exception, e: | 
|---|
| 77 |         log.debug( "Dropping history_user_share_association table failed: %s" % str( e ) ) | 
|---|