[2] | 1 | """ |
---|
| 2 | This migration script adds a new column to 2 tables: |
---|
| 3 | 1) a new boolean type column named 'submitted' to the 'request' table |
---|
| 4 | 2) a new string type column named 'bar_code' to the 'sample' table |
---|
| 5 | """ |
---|
| 6 | from sqlalchemy import * |
---|
| 7 | from sqlalchemy.orm import * |
---|
| 8 | from migrate import * |
---|
| 9 | from migrate.changeset import * |
---|
| 10 | import sys, logging |
---|
| 11 | from galaxy.model.custom_types import * |
---|
| 12 | from sqlalchemy.exc import * |
---|
| 13 | |
---|
| 14 | log = logging.getLogger( __name__ ) |
---|
| 15 | log.setLevel(logging.DEBUG) |
---|
| 16 | handler = logging.StreamHandler( sys.stdout ) |
---|
| 17 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" |
---|
| 18 | formatter = logging.Formatter( format ) |
---|
| 19 | handler.setFormatter( formatter ) |
---|
| 20 | log.addHandler( handler ) |
---|
| 21 | |
---|
| 22 | metadata = MetaData( migrate_engine ) |
---|
| 23 | |
---|
| 24 | def display_migration_details(): |
---|
| 25 | print "========================================" |
---|
| 26 | print "This migration script adds a new column to 2 tables:" |
---|
| 27 | print "1) a new boolean type column named 'submitted' to the 'request' table" |
---|
| 28 | print "2) a new string type column named 'bar_code' to the 'sample' table" |
---|
| 29 | print "========================================" |
---|
| 30 | |
---|
| 31 | def upgrade(): |
---|
| 32 | display_migration_details() |
---|
| 33 | # Load existing tables |
---|
| 34 | metadata.reflect() |
---|
| 35 | # Add 1 column to the request table |
---|
| 36 | try: |
---|
| 37 | Request_table = Table( "request", metadata, autoload=True ) |
---|
| 38 | except NoSuchTableError: |
---|
| 39 | Request_table = None |
---|
| 40 | log.debug( "Failed loading table request" ) |
---|
| 41 | if Request_table: |
---|
| 42 | try: |
---|
| 43 | col = Column( "submitted", Boolean, index=True, default=False ) |
---|
| 44 | col.create( Request_table ) |
---|
| 45 | assert col is Request_table.c.submitted |
---|
| 46 | except Exception, e: |
---|
| 47 | log.debug( "Adding column 'submitted' to request table failed: %s" % ( str( e ) ) ) |
---|
| 48 | # Add 1 column to the sample table |
---|
| 49 | try: |
---|
| 50 | Sample_table = Table( "sample", metadata, autoload=True ) |
---|
| 51 | except NoSuchTableError: |
---|
| 52 | Sample_table = None |
---|
| 53 | log.debug( "Failed loading table sample" ) |
---|
| 54 | if Sample_table: |
---|
| 55 | try: |
---|
| 56 | col = Column( "bar_code", TrimmedString( 255 ), index=True ) |
---|
| 57 | col.create( Sample_table ) |
---|
| 58 | assert col is Sample_table.c.bar_code |
---|
| 59 | except Exception, e: |
---|
| 60 | log.debug( "Adding column 'bar_code' to sample table failed: %s" % ( str( e ) ) ) |
---|
| 61 | |
---|
| 62 | def downgrade(): |
---|
| 63 | pass |
---|