PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0057_request_notify.py

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