1 | """ |
---|
2 | Migration script to add dbkey column for visualization. |
---|
3 | """ |
---|
4 | |
---|
5 | from sqlalchemy import * |
---|
6 | from sqlalchemy.orm import * |
---|
7 | from migrate import * |
---|
8 | from migrate.changeset import * |
---|
9 | from galaxy.util.json import from_json_string |
---|
10 | |
---|
11 | import logging |
---|
12 | log = logging.getLogger( __name__ ) |
---|
13 | |
---|
14 | metadata = MetaData( migrate_engine ) |
---|
15 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) |
---|
16 | |
---|
17 | def upgrade(): |
---|
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, index=True ) |
---|
27 | y = Column( "dbkey", TEXT, index=True ) |
---|
28 | x.create( Visualization_table ) |
---|
29 | y.create( Visualization_revision_table ) |
---|
30 | assert x is Visualization_table.c.dbkey |
---|
31 | assert y is Visualization_revision_table.c.dbkey |
---|
32 | |
---|
33 | try: |
---|
34 | i = Index( "ix_visualization_dbkey", Visualization_table.c.dbkey ) |
---|
35 | i.create() |
---|
36 | except: |
---|
37 | pass |
---|
38 | |
---|
39 | try: |
---|
40 | i = Index( "ix_visualization_revision_dbkey", Visualization_revision_table.c.dbkey ) |
---|
41 | i.create() |
---|
42 | except: |
---|
43 | pass |
---|
44 | |
---|
45 | all_viz = db_session.execute( "SELECT visualization.id as viz_id, visualization_revision.id as viz_rev_id, visualization_revision.config FROM visualization_revision \ |
---|
46 | LEFT JOIN visualization ON visualization.id=visualization_revision.visualization_id" ) |
---|
47 | for viz in all_viz: |
---|
48 | viz_id = viz['viz_id'] |
---|
49 | viz_rev_id = viz['viz_rev_id'] |
---|
50 | if viz[Visualization_revision_table.c.config]: |
---|
51 | dbkey = from_json_string(viz[Visualization_revision_table.c.config]).get('dbkey', "").replace("'", "\\'") |
---|
52 | db_session.execute("UPDATE visualization_revision SET dbkey='%s' WHERE id=%s" % (dbkey, viz_rev_id)) |
---|
53 | db_session.execute("UPDATE visualization SET dbkey='%s' WHERE id=%s" % (dbkey, viz_id)) |
---|
54 | |
---|
55 | def downgrade(): |
---|
56 | metadata.reflect() |
---|
57 | |
---|
58 | Visualization_table = Table( "visualization", metadata, autoload=True ) |
---|
59 | Visualization_revision_table = Table( "visualization_revision", metadata, autoload=True ) |
---|
60 | |
---|
61 | Visualization_table.c.dbkey.drop() |
---|
62 | Visualization_revision_table.c.dbkey.drop() |
---|