1 | """ |
---|
2 | This migration script adds the request_event table and |
---|
3 | removes the state field in the request table |
---|
4 | """ |
---|
5 | from sqlalchemy import * |
---|
6 | from sqlalchemy.orm import * |
---|
7 | from sqlalchemy.exc import * |
---|
8 | from migrate import * |
---|
9 | from migrate.changeset import * |
---|
10 | |
---|
11 | import datetime |
---|
12 | now = datetime.datetime.utcnow |
---|
13 | |
---|
14 | import sys, logging |
---|
15 | log = logging.getLogger( __name__ ) |
---|
16 | log.setLevel(logging.DEBUG) |
---|
17 | handler = logging.StreamHandler( sys.stdout ) |
---|
18 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" |
---|
19 | formatter = logging.Formatter( format ) |
---|
20 | handler.setFormatter( formatter ) |
---|
21 | log.addHandler( handler ) |
---|
22 | |
---|
23 | # Need our custom types, but don't import anything else from model |
---|
24 | from galaxy.model.custom_types import * |
---|
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 migration script adds the request_event table and" |
---|
32 | print "removes the state field in the request table" |
---|
33 | print "========================================" |
---|
34 | |
---|
35 | def 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 | |
---|
43 | def 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 | |
---|
52 | RequestEvent_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 | |
---|
60 | def 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 | |
---|
94 | def downgrade(): |
---|
95 | pass |
---|