root/galaxy-central/lib/galaxy/model/migrate/versions/0018_ordered_tags_and_page_tags.py @ 2

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

import galaxy-central

行番号 
1"""
2This migration script provides support for (a) ordering tags by recency and
3(b) tagging pages. This script deletes all existing tags.
4"""
5
6from sqlalchemy import *
7from sqlalchemy.orm import *
8from sqlalchemy.exc import *
9from migrate import *
10import migrate.changeset
11
12import datetime
13now = datetime.datetime.utcnow
14
15# Need our custom types, but don't import anything else from model
16from galaxy.model.custom_types import *
17
18import logging
19log = logging.getLogger( __name__ )
20
21metadata = MetaData( migrate_engine )
22
23def display_migration_details():
24    print ""
25    print "This migration script provides support for (a) ordering tags by recency and"
26    print "(b) tagging pages. This script deletes all existing tags."
27
28HistoryTagAssociation_table = Table( "history_tag_association", metadata,
29    Column( "id", Integer, primary_key=True ),
30    Column( "history_id", Integer, ForeignKey( "history.id" ), index=True ),
31    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
32    Column( "user_tname", TrimmedString(255), index=True),
33    Column( "value", TrimmedString(255), index=True),
34    Column( "user_value", TrimmedString(255), index=True) )
35
36DatasetTagAssociation_table = Table( "dataset_tag_association", metadata,
37    Column( "id", Integer, primary_key=True ),
38    Column( "dataset_id", Integer, ForeignKey( "dataset.id" ), index=True ),
39    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
40    Column( "user_tname", TrimmedString(255), index=True),
41    Column( "value", TrimmedString(255), index=True),
42    Column( "user_value", TrimmedString(255), index=True) )
43
44HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata,
45    Column( "id", Integer, primary_key=True ),
46    Column( "history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id" ), index=True ),
47    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
48    Column( "user_tname", TrimmedString(255), index=True),
49    Column( "value", TrimmedString(255), index=True),
50    Column( "user_value", TrimmedString(255), index=True) )
51
52PageTagAssociation_table = Table( "page_tag_association", metadata,
53    Column( "id", Integer, primary_key=True ),
54    Column( "page_id", Integer, ForeignKey( "page.id" ), index=True ),
55    Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
56    Column( "user_tname", TrimmedString(255), index=True),
57    Column( "value", TrimmedString(255), index=True),
58    Column( "user_value", TrimmedString(255), index=True) )
59
60def upgrade():
61    display_migration_details()
62    metadata.reflect()
63
64    #
65    # Recreate tables.
66    #
67    try:
68        HistoryTagAssociation_table.drop()
69        HistoryTagAssociation_table.create()
70    except Exception, e:
71        print "Recreating history_tag_association table failed: %s" % str( e )
72        log.debug( "Recreating history_tag_association table failed: %s" % str( e ) )
73
74    try:
75        DatasetTagAssociation_table.drop()
76        DatasetTagAssociation_table.create()
77    except Exception, e:
78        print str(e)
79        log.debug( "Recreating dataset_tag_association table failed: %s" % str( e ) )
80
81    try:
82        HistoryDatasetAssociationTagAssociation_table.drop()
83        HistoryDatasetAssociationTagAssociation_table.create()
84    except OperationalError, e:
85        # Handle error that results from and index name that is too long; this occurs
86        # in MySQL.
87        if str(e).find("CREATE INDEX") != -1:
88            # Manually create index.
89            i = Index( "ix_hda_ta_history_dataset_association_id", HistoryDatasetAssociationTagAssociation_table.c.history_dataset_association_id )
90            try:
91                i.create()
92            except Exception, e:
93                print str(e)
94                log.debug( "Adding index 'ix_hda_ta_history_dataset_association_id' to table 'history_dataset_association_tag_association' table failed: %s" % str( e ) )
95    except Exception, e:
96        print str(e)
97        log.debug( "Recreating history_dataset_association_tag_association table failed: %s" % str( e ) )
98
99    # Create page_tag_association table.
100    try:
101        PageTagAssociation_table.create()
102    except Exception, e:
103        print str(e)
104        log.debug( "Creating page_tag_association table failed: %s" % str( e ) )
105
106def downgrade():
107    metadata.reflect()
108
109    # No need to downgrade other tagging tables. They work fine with verision 16 code.
110
111    # Drop page_tag_association table.
112    try:
113        PageTagAssociation_table.drop()
114    except Exception, e:
115        print str(e)
116        log.debug( "Dropping page_tag_association table failed: %s" % str( e ) )
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。