1 | """ |
---|
2 | Migration script to modify the 'notify' field in the 'request' table from a boolean |
---|
3 | to a JSONType |
---|
4 | """ |
---|
5 | |
---|
6 | from sqlalchemy import * |
---|
7 | from sqlalchemy.orm import * |
---|
8 | from migrate import * |
---|
9 | from migrate.changeset import * |
---|
10 | from sqlalchemy.exc import * |
---|
11 | |
---|
12 | from galaxy.model.custom_types import * |
---|
13 | from galaxy.util.json import from_json_string, to_json_string |
---|
14 | |
---|
15 | import datetime |
---|
16 | now = datetime.datetime.utcnow |
---|
17 | |
---|
18 | import logging |
---|
19 | log = logging.getLogger( __name__ ) |
---|
20 | |
---|
21 | metadata = MetaData( migrate_engine ) |
---|
22 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) |
---|
23 | |
---|
24 | |
---|
25 | def upgrade(): |
---|
26 | print __doc__ |
---|
27 | metadata.reflect() |
---|
28 | try: |
---|
29 | Request_table = Table( "request", metadata, autoload=True ) |
---|
30 | except NoSuchTableError, e: |
---|
31 | Request_table = None |
---|
32 | log.debug( "Failed loading table 'request'" ) |
---|
33 | |
---|
34 | |
---|
35 | if Request_table: |
---|
36 | # create the column again as JSONType |
---|
37 | try: |
---|
38 | col = Column( "notification", JSONType() ) |
---|
39 | col.create( Request_table ) |
---|
40 | assert col is Request_table.c.notification |
---|
41 | except Exception, e: |
---|
42 | log.debug( "Creating column 'notification' in the 'request' table failed: %s" % ( str( e ) ) ) |
---|
43 | |
---|
44 | cmd = "SELECT id, user_id, notify FROM request" |
---|
45 | result = db_session.execute( cmd ) |
---|
46 | for r in result: |
---|
47 | id = int(r[0]) |
---|
48 | notify_old = r[1] |
---|
49 | notify_new = dict(email=[], sample_states=[], body='', subject='') |
---|
50 | cmd = "update request set notification='%s' where id=%i" % (to_json_string(notify_new), id) |
---|
51 | db_session.execute( cmd ) |
---|
52 | |
---|
53 | cmd = "SELECT id, notification FROM request" |
---|
54 | result = db_session.execute( cmd ) |
---|
55 | for r in result: |
---|
56 | rr = from_json_string(str(r[1])) |
---|
57 | |
---|
58 | # remove the 'notify' column |
---|
59 | try: |
---|
60 | Request_table.c.notify.drop() |
---|
61 | except Exception, e: |
---|
62 | log.debug( "Deleting column 'notify' from the 'request' table failed: %s" % ( str( e ) ) ) |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | def downgrade(): |
---|
67 | pass |
---|