root/galaxy-central/lib/galaxy/model/migrate/versions/0027_request_events.py

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

import galaxy-central

行番号 
1"""
2This migration script adds the request_event table and
3removes the state field in the request table
4"""
5from sqlalchemy import *
6from sqlalchemy.orm import *
7from sqlalchemy.exc import *
8from migrate import *
9from migrate.changeset import *
10
11import datetime
12now = datetime.datetime.utcnow
13
14import sys, logging
15log = logging.getLogger( __name__ )
16log.setLevel(logging.DEBUG)
17handler = logging.StreamHandler( sys.stdout )
18format = "%(name)s %(levelname)s %(asctime)s %(message)s"
19formatter = logging.Formatter( format )
20handler.setFormatter( formatter )
21log.addHandler( handler )
22
23# Need our custom types, but don't import anything else from model
24from galaxy.model.custom_types import *
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 migration script adds the request_event table and"
32    print "removes the state field in the request table"
33    print "========================================"
34   
35def localtimestamp():
36   if migrate_engine.name == 'postgres' or migrate_engine.name == 'mysql':
37       return "LOCALTIMESTAMP"
38   elif migrate_engine.name == 'sqlite':
39       return "current_date || ' ' || current_time"
40   else:
41       raise Exception( 'Unable to convert data for unknown database type: %s' % db )
42   
43def nextval( table, col='id' ):
44    if migrate_engine.name == 'postgres':
45        return "nextval('%s_%s_seq')" % ( table, col )
46    elif migrate_engine.name == 'mysql' or migrate_engine.name == 'sqlite':
47        return "null"
48    else:
49        raise Exception( 'Unable to convert data for unknown database type: %s' % migrate_engine.name )
50
51
52RequestEvent_table = Table('request_event', metadata,
53    Column( "id", Integer, primary_key=True),
54    Column( "create_time", DateTime, default=now ),
55    Column( "update_time", DateTime, default=now, onupdate=now ),
56    Column( "request_id", Integer, ForeignKey( "request.id" ), index=True ),
57    Column( "state", TrimmedString( 255 ),  index=True ),
58    Column( "comment", TEXT ) )
59
60def upgrade():
61    display_migration_details()
62    # Load existing tables
63    metadata.reflect()
64    # Add new request_event table
65    try:
66        RequestEvent_table.create()
67    except Exception, e:
68        log.debug( "Creating request_event table failed: %s" % str( e ) )
69    # move the current state of all existing requests to the request_event table
70    cmd = \
71        "INSERT INTO request_event " + \
72        "SELECT %s AS id," + \
73        "%s AS create_time," + \
74        "%s AS update_time," + \
75        "request.id AS request_id," + \
76        "request.state AS state," + \
77        "'%s' AS comment " + \
78        "FROM request;"
79    cmd = cmd % ( nextval('request_event'), localtimestamp(), localtimestamp(), 'Imported from request table')
80    db_session.execute( cmd )
81   
82    # Delete the state column
83    try:
84        Request_table = Table( "request", metadata, autoload=True )
85    except NoSuchTableError:
86        Request_table = None
87        log.debug( "Failed loading table request" )
88    if Request_table:
89        try:
90            Request_table.c.state.drop()
91        except Exception, e:
92            log.debug( "Deleting column 'state' to request table failed: %s" % ( str( e ) ) )   
93   
94def downgrade():
95    pass
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。