root/galaxy-central/lib/galaxy/model/migrate/versions/0012_user_address.py

リビジョン 2, 4.1 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
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 migrate import *
11from migrate.changeset import *
12import datetime
13now = datetime.datetime.utcnow
14import sys, logging
15# Need our custom types, but don't import anything else from model
16from galaxy.model.custom_types import *
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( migrate_engine )
27db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
28
29def 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
37UserAddress_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
54def 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
94def downgrade():
95    pass
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。