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

/lib/galaxy/model/migrate/versions/0054_visualization_dbkey.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 56 lines | 43 code | 8 blank | 5 comment | 3 complexity | 32c2b11141eb69fd43a8bbe4d61072a7 MD5 | raw file
 1"""
 2Migration script to add dbkey column for visualization.
 3"""
 4
 5from sqlalchemy import *
 6from sqlalchemy.orm import *
 7from migrate import *
 8from migrate.changeset import *
 9from galaxy.util.json import from_json_string
10
11import logging
12log = logging.getLogger( __name__ )
13
14metadata = MetaData()
15
16def upgrade(migrate_engine):
17    metadata.bind = migrate_engine
18
19    print __doc__
20    metadata.reflect()
21
22    Visualization_table = Table( "visualization", metadata, autoload=True )
23    Visualization_revision_table = Table( "visualization_revision", metadata, autoload=True )
24
25    # Create dbkey columns.
26    x = Column( "dbkey", TEXT )
27    y = Column( "dbkey", TEXT )
28    x.create( Visualization_table )
29    y.create( Visualization_revision_table )
30    # Manually create indexes for compatability w/ mysql_length.
31    xi = Index( "ix_visualization_dbkey", Visualization_table.c.dbkey, mysql_length = 200)
32    xi.create()
33    yi = Index( "ix_visualization_revision_dbkey", Visualization_revision_table.c.dbkey, mysql_length = 200)
34    yi.create()
35    assert x is Visualization_table.c.dbkey
36    assert y is Visualization_revision_table.c.dbkey
37
38    all_viz = migrate_engine.execute( "SELECT visualization.id as viz_id, visualization_revision.id as viz_rev_id, visualization_revision.config FROM visualization_revision \
39                    LEFT JOIN visualization ON visualization.id=visualization_revision.visualization_id" )
40    for viz in all_viz:
41        viz_id = viz['viz_id']
42        viz_rev_id = viz['viz_rev_id']
43        if viz[Visualization_revision_table.c.config]:
44            dbkey = from_json_string(viz[Visualization_revision_table.c.config]).get('dbkey', "").replace("'", "\\'")
45            migrate_engine.execute("UPDATE visualization_revision SET dbkey='%s' WHERE id=%s" % (dbkey, viz_rev_id))
46            migrate_engine.execute("UPDATE visualization SET dbkey='%s' WHERE id=%s" % (dbkey, viz_id))
47
48def downgrade(migrate_engine):
49    metadata.bind = migrate_engine
50    metadata.reflect()
51
52    Visualization_table = Table( "visualization", metadata, autoload=True )
53    Visualization_revision_table = Table( "visualization_revision", metadata, autoload=True )
54
55    Visualization_table.c.dbkey.drop()
56    Visualization_revision_table.c.dbkey.drop()