PageRenderTime 30ms CodeModel.GetById 19ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0012_user_address.py

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