| 1 | """ |
|---|
| 2 | This script adds a new user_address table that is currently only used with sample requests, where |
|---|
| 3 | a user can select from a list of his addresses to associate with the request. This script also |
|---|
| 4 | drops the request.submitted column which was boolean and replaces it with a request.state column |
|---|
| 5 | which is a string, allowing for more flexibility with request states. |
|---|
| 6 | """ |
|---|
| 7 | from sqlalchemy import * |
|---|
| 8 | from sqlalchemy.orm import * |
|---|
| 9 | from sqlalchemy.exc import * |
|---|
| 10 | from migrate import * |
|---|
| 11 | from migrate.changeset import * |
|---|
| 12 | import datetime |
|---|
| 13 | now = datetime.datetime.utcnow |
|---|
| 14 | import sys, logging |
|---|
| 15 | # Need our custom types, but don't import anything else from model |
|---|
| 16 | from galaxy.model.custom_types import * |
|---|
| 17 | |
|---|
| 18 | log = logging.getLogger( __name__ ) |
|---|
| 19 | log.setLevel(logging.DEBUG) |
|---|
| 20 | handler = logging.StreamHandler( sys.stdout ) |
|---|
| 21 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" |
|---|
| 22 | formatter = logging.Formatter( format ) |
|---|
| 23 | handler.setFormatter( formatter ) |
|---|
| 24 | log.addHandler( handler ) |
|---|
| 25 | |
|---|
| 26 | metadata = MetaData( migrate_engine ) |
|---|
| 27 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) |
|---|
| 28 | |
|---|
| 29 | def display_migration_details(): |
|---|
| 30 | print "========================================" |
|---|
| 31 | print "This script adds a new user_address table that is currently only used with sample requests, where" |
|---|
| 32 | print "a user can select from a list of his addresses to associate with the request. This script also" |
|---|
| 33 | print "drops the request.submitted column which was boolean and replaces it with a request.state column" |
|---|
| 34 | print "which is a string, allowing for more flexibility with request states." |
|---|
| 35 | print "========================================" |
|---|
| 36 | |
|---|
| 37 | UserAddress_table = Table( "user_address", metadata, |
|---|
| 38 | Column( "id", Integer, primary_key=True), |
|---|
| 39 | Column( "create_time", DateTime, default=now ), |
|---|
| 40 | Column( "update_time", DateTime, default=now, onupdate=now ), |
|---|
| 41 | Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ), |
|---|
| 42 | Column( "desc", TEXT), |
|---|
| 43 | Column( "name", TrimmedString( 255 ), nullable=False), |
|---|
| 44 | Column( "institution", TrimmedString( 255 )), |
|---|
| 45 | Column( "address", TrimmedString( 255 ), nullable=False), |
|---|
| 46 | Column( "city", TrimmedString( 255 ), nullable=False), |
|---|
| 47 | Column( "state", TrimmedString( 255 ), nullable=False), |
|---|
| 48 | Column( "postal_code", TrimmedString( 255 ), nullable=False), |
|---|
| 49 | Column( "country", TrimmedString( 255 ), nullable=False), |
|---|
| 50 | Column( "phone", TrimmedString( 255 )), |
|---|
| 51 | Column( "deleted", Boolean, index=True, default=False ), |
|---|
| 52 | Column( "purged", Boolean, index=True, default=False ) ) |
|---|
| 53 | |
|---|
| 54 | def upgrade(): |
|---|
| 55 | display_migration_details() |
|---|
| 56 | # Load existing tables |
|---|
| 57 | metadata.reflect() |
|---|
| 58 | # Add all of the new tables above |
|---|
| 59 | try: |
|---|
| 60 | UserAddress_table.create() |
|---|
| 61 | except Exception, e: |
|---|
| 62 | log.debug( "Creating user_address table failed: %s" % str( e ) ) |
|---|
| 63 | # Add 1 column to the request_type table |
|---|
| 64 | try: |
|---|
| 65 | RequestType_table = Table( "request_type", metadata, autoload=True ) |
|---|
| 66 | except NoSuchTableError: |
|---|
| 67 | RequestType_table = None |
|---|
| 68 | log.debug( "Failed loading table request_type" ) |
|---|
| 69 | if RequestType_table: |
|---|
| 70 | try: |
|---|
| 71 | col = Column( "deleted", Boolean, index=True, default=False ) |
|---|
| 72 | col.create( RequestType_table ) |
|---|
| 73 | assert col is RequestType_table.c.deleted |
|---|
| 74 | except Exception, e: |
|---|
| 75 | log.debug( "Adding column 'deleted' to request_type table failed: %s" % ( str( e ) ) ) |
|---|
| 76 | # Delete the submitted column |
|---|
| 77 | try: |
|---|
| 78 | Request_table = Table( "request", metadata, autoload=True ) |
|---|
| 79 | except NoSuchTableError: |
|---|
| 80 | Request_table = None |
|---|
| 81 | log.debug( "Failed loading table request" ) |
|---|
| 82 | if Request_table: |
|---|
| 83 | try: |
|---|
| 84 | Request_table.c.submitted.drop() |
|---|
| 85 | except Exception, e: |
|---|
| 86 | log.debug( "Deleting column 'submitted' to request table failed: %s" % ( str( e ) ) ) |
|---|
| 87 | try: |
|---|
| 88 | col = Column( "state", TrimmedString( 255 ), index=True ) |
|---|
| 89 | col.create( Request_table ) |
|---|
| 90 | assert col is Request_table.c.state |
|---|
| 91 | except Exception, e: |
|---|
| 92 | log.debug( "Adding column 'state' to request table failed: %s" % ( str( e ) ) ) |
|---|
| 93 | |
|---|
| 94 | def downgrade(): |
|---|
| 95 | pass |
|---|