1 | """ |
---|
2 | This migration script provides support for (a) ordering tags by recency and |
---|
3 | (b) tagging pages. This script deletes all existing tags. |
---|
4 | """ |
---|
5 | |
---|
6 | from sqlalchemy import * |
---|
7 | from sqlalchemy.orm import * |
---|
8 | from sqlalchemy.exc import * |
---|
9 | from migrate import * |
---|
10 | import migrate.changeset |
---|
11 | |
---|
12 | import datetime |
---|
13 | now = datetime.datetime.utcnow |
---|
14 | |
---|
15 | # Need our custom types, but don't import anything else from model |
---|
16 | from galaxy.model.custom_types import * |
---|
17 | |
---|
18 | import logging |
---|
19 | log = logging.getLogger( __name__ ) |
---|
20 | |
---|
21 | metadata = MetaData( migrate_engine ) |
---|
22 | |
---|
23 | def 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 | |
---|
28 | HistoryTagAssociation_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 | |
---|
36 | DatasetTagAssociation_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 | |
---|
44 | HistoryDatasetAssociationTagAssociation_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 | |
---|
52 | PageTagAssociation_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 | |
---|
60 | def 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 | |
---|
106 | def 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 ) ) |
---|