root/galaxy-central/lib/galaxy/model/migrate/versions/0057_request_notify.py @ 2

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

import galaxy-central

行番号 
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( migrate_engine )
22db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
23
24
25def 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
66def downgrade():
67    pass
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。