root/galaxy-central/lib/galaxy/model/migrate/versions/0031_community_and_workflow_tags.py @ 2

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

import galaxy-central

行番号 
1"""
2Migration script to (a) add and populate necessary columns for doing community tagging of histories, datasets, and pages and \
3(b) add table for doing individual and community tagging of workflows.
4
5SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so this script will generate error messages when run against \
6SQLite; however, script does execute successfully against SQLite.
7"""
8
9from sqlalchemy import *
10from sqlalchemy.orm import *
11from migrate import *
12from migrate.changeset import *
13
14import logging
15log = logging.getLogger( __name__ )
16
17metadata = MetaData( migrate_engine )
18db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
19
20StoredWorkflowTagAssociation_table = Table( "stored_workflow_tag_association", metadata,
21    Column( "id", Integer, primary_key=True ),
22    Column( "stored_workflow_id", Integer, ForeignKey( "stored_workflow.id" ), index=True ),
23    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
24    Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
25    Column( "user_tname", Unicode(255), index=True),
26    Column( "value", Unicode(255), index=True),
27    Column( "user_value", Unicode(255), index=True) )
28   
29WorkflowTagAssociation_table = Table( "workflow_tag_association", metadata,
30    Column( "id", Integer, primary_key=True ),
31    Column( "workflow_id", Integer, ForeignKey( "workflow.id" ), index=True ),
32    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
33    Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
34    Column( "user_tname", Unicode(255), index=True),
35    Column( "value", Unicode(255), index=True),
36    Column( "user_value", Unicode(255), index=True) )
37
38def upgrade():
39    print __doc__
40    metadata.reflect()
41
42    # Create user_id column in history_tag_association table.
43    HistoryTagAssociation_table = Table( "history_tag_association", metadata, autoload=True )
44    c = Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True )
45    try:
46        c.create( HistoryTagAssociation_table )
47        assert c is HistoryTagAssociation_table.c.user_id
48    except Exception, e:
49        # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
50        print str(e)
51        log.debug( "Adding user_id column to history_tag_association table failed: %s" % str( e ) )
52
53    # Create user_id index for history_tag_association table.
54    try:
55        i = Index( "ix_history_tag_association_user_id", HistoryTagAssociation_table.c.user_id )
56        i.create()
57    except:
58        # Mysql doesn't have a named index, but alter should work
59        HistoryTagAssociation_table.c.user_id.alter( unique=False )
60       
61    # Populate column so that user_id is the id of the user who owns the history (and, up to now, was the only person able to tag the history).
62    if c is HistoryTagAssociation_table.c.user_id:
63        db_session.execute(
64            "UPDATE history_tag_association SET user_id=( SELECT user_id FROM history WHERE history_tag_association.history_id = history.id )"
65                            )
66
67    # Create user_id column in history_dataset_association_tag_association table.
68    HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata, autoload=True )
69    c = Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True )
70    try:
71        c.create( HistoryDatasetAssociationTagAssociation_table )
72        assert c is HistoryDatasetAssociationTagAssociation_table.c.user_id
73    except Exception, e:
74        # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
75        print str(e)
76        log.debug( "Adding user_id column to history_dataset_association_tag_association table failed: %s" % str( e ) )
77
78    # Create user_id index for history_dataset_association_tag_association table.
79    try:
80        i = Index( "ix_history_dataset_association_tag_association_user_id", HistoryDatasetAssociationTagAssociation_table.c.user_id )
81        i.create()
82    except:
83        # Mysql doesn't have a named index, but alter should work
84        HistoryDatasetAssociationTagAssociation_table.c.user_id.alter( unique=False )
85       
86    # Populate column so that user_id is the id of the user who owns the history_dataset_association (and, up to now, was the only person able to tag the page).
87    if c is HistoryDatasetAssociationTagAssociation_table.c.user_id:
88        db_session.execute(
89            "UPDATE history_dataset_association_tag_association SET user_id=( SELECT history.user_id FROM history, history_dataset_association WHERE history_dataset_association.history_id = history.id AND history_dataset_association.id = history_dataset_association_tag_association.history_dataset_association_id)"
90                            )
91
92    # Create user_id column in page_tag_association table.
93    PageTagAssociation_table = Table( "page_tag_association", metadata, autoload=True )
94    c = Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True )
95    try:
96        c.create( PageTagAssociation_table )
97        assert c is PageTagAssociation_table.c.user_id
98    except Exception, e:
99        # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
100        print str(e)
101        log.debug( "Adding user_id column to history_tag_association table failed: %s" % str( e ) )
102
103    # Create user_id index for page_tag_association table.
104    try:
105        i = Index( "ix_page_tag_association_user_id", PageTagAssociation_table.c.user_id )
106        i.create()
107    except:
108        # Mysql doesn't have a named index, but alter should work
109        PageTagAssociation_table.c.user_id.alter( unique=False )
110       
111    # Populate column so that user_id is the id of the user who owns the page (and, up to now, was the only person able to tag the page).
112    if c is PageTagAssociation_table.c.user_id:
113        db_session.execute(
114            "UPDATE page_tag_association SET user_id=( SELECT user_id FROM page WHERE page_tag_association.page_id = page.id )"
115                            )
116
117    # Create stored_workflow_tag_association table.
118    try:
119        StoredWorkflowTagAssociation_table.create()
120    except Exception, e:
121        print str(e)
122        log.debug( "Creating stored_workflow_tag_association table failed: %s" % str( e ) )
123       
124    # Create workflow_tag_association table.
125    try:
126        WorkflowTagAssociation_table.create()
127    except Exception, e:
128        print str(e)
129        log.debug( "Creating workflow_tag_association table failed: %s" % str( e ) )       
130
131def downgrade():
132    metadata.reflect()
133
134    # Drop user_id column from history_tag_association table.
135    HistoryTagAssociation_table = Table( "history_tag_association", metadata, autoload=True )
136    try:
137        HistoryTagAssociation_table.c.user_id.drop()
138    except Exception, e:
139        print str(e)
140        log.debug( "Dropping column user_id from history_tag_association table failed: %s" % str( e ) )
141       
142
143    # Drop user_id column from history_dataset_association_tag_association table.
144    HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata, autoload=True )
145    try:
146        HistoryDatasetAssociationTagAssociation_table.c.user_id.drop()
147    except Exception, e:
148        print str(e)
149        log.debug( "Dropping column user_id from history_dataset_association_tag_association table failed: %s" % str( e ) )
150
151    # Drop user_id column from page_tag_association table.
152    PageTagAssociation_table = Table( "page_tag_association", metadata, autoload=True )
153    try:
154        PageTagAssociation_table.c.user_id.drop()
155    except Exception, e:
156        print str(e)
157        log.debug( "Dropping column user_id from page_tag_association table failed: %s" % str( e ) )
158
159    # Drop stored_workflow_tag_association table.
160    try:
161        StoredWorkflowTagAssociation_table.drop()
162    except Exception, e:
163        print str(e)
164        log.debug( "Dropping stored_workflow_tag_association table failed: %s" % str( e ) )
165       
166    # Drop workflow_tag_association table.
167    try:
168        WorkflowTagAssociation_table.drop()
169    except Exception, e:
170        print str(e)
171        log.debug( "Dropping workflow_tag_association table failed: %s" % str( e ) )
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。