1 | """ |
---|
2 | This migration script eliminates all of the tables that were used for the 1st version of the |
---|
3 | library templates where template fields and contents were each stored as a separate table row |
---|
4 | in various library item tables. All of these tables are dropped in this script, eliminating all |
---|
5 | existing template data. A total of 14 existing tables are dropped. |
---|
6 | |
---|
7 | We're now basing library templates on forms, so field contents are |
---|
8 | stored as a jsonified list in the form_values table. This script introduces the following 3 |
---|
9 | new association tables: |
---|
10 | 1) library_info_association |
---|
11 | 2) library_folder_info_association |
---|
12 | 3) library_dataset_dataset_info_association |
---|
13 | |
---|
14 | If using mysql, this script will throw an (OperationalError) exception due to a long index name on |
---|
15 | the library_dataset_dataset_info_association table, which is OK because the script creates an index |
---|
16 | with a shortened name. |
---|
17 | """ |
---|
18 | from sqlalchemy import * |
---|
19 | from sqlalchemy.orm import * |
---|
20 | from sqlalchemy.exc import * |
---|
21 | from migrate import * |
---|
22 | from migrate.changeset import * |
---|
23 | import sys, logging |
---|
24 | |
---|
25 | log = logging.getLogger( __name__ ) |
---|
26 | log.setLevel(logging.DEBUG) |
---|
27 | handler = logging.StreamHandler( sys.stdout ) |
---|
28 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" |
---|
29 | formatter = logging.Formatter( format ) |
---|
30 | handler.setFormatter( formatter ) |
---|
31 | log.addHandler( handler ) |
---|
32 | |
---|
33 | metadata = MetaData( migrate_engine ) |
---|
34 | |
---|
35 | def display_migration_details(): |
---|
36 | print "========================================" |
---|
37 | print "This migration script eliminates all of the tables that were used for the 1st version of the" |
---|
38 | print "library templates where template fields and contents were each stored as a separate table row" |
---|
39 | print "in various library item tables. All of these tables are dropped in this script, eliminating all" |
---|
40 | print "existing template data. A total of 14 existing tables are dropped." |
---|
41 | print "" |
---|
42 | print "We're now basing library templates on Galaxy forms, so field contents are stored as a jsonified" |
---|
43 | print "list in the form_values table. This script introduces the following 3 new association tables:" |
---|
44 | print "1) library_info_association" |
---|
45 | print "2) library_folder_info_association" |
---|
46 | print "3) library_dataset_dataset_info_association" |
---|
47 | print "" |
---|
48 | print "If using mysql, this script will throw an (OperationalError) exception due to a long index name" |
---|
49 | print "on the library_dataset_dataset_info_association table, which is OK because the script creates" |
---|
50 | print "an index with a shortened name." |
---|
51 | print "========================================" |
---|
52 | |
---|
53 | if migrate_engine.name == 'postgres': |
---|
54 | # http://blog.pythonisito.com/2008/01/cascading-drop-table-with-sqlalchemy.html |
---|
55 | from sqlalchemy.databases import postgres |
---|
56 | class PGCascadeSchemaDropper(postgres.PGSchemaDropper): |
---|
57 | def visit_table(self, table): |
---|
58 | for column in table.columns: |
---|
59 | if column.default is not None: |
---|
60 | self.traverse_single(column.default) |
---|
61 | self.append("\nDROP TABLE " + |
---|
62 | self.preparer.format_table(table) + |
---|
63 | " CASCADE") |
---|
64 | self.execute() |
---|
65 | postgres.dialect.schemadropper = PGCascadeSchemaDropper |
---|
66 | |
---|
67 | LibraryInfoAssociation_table = Table( 'library_info_association', metadata, |
---|
68 | Column( "id", Integer, primary_key=True), |
---|
69 | Column( "library_id", Integer, ForeignKey( "library.id" ), index=True ), |
---|
70 | Column( "form_definition_id", Integer, ForeignKey( "form_definition.id" ), index=True ), |
---|
71 | Column( "form_values_id", Integer, ForeignKey( "form_values.id" ), index=True ) ) |
---|
72 | |
---|
73 | LibraryFolderInfoAssociation_table = Table( 'library_folder_info_association', metadata, |
---|
74 | Column( "id", Integer, primary_key=True), |
---|
75 | Column( "library_folder_id", Integer, ForeignKey( "library_folder.id" ), nullable=True, index=True ), |
---|
76 | Column( "form_definition_id", Integer, ForeignKey( "form_definition.id" ), index=True ), |
---|
77 | Column( "form_values_id", Integer, ForeignKey( "form_values.id" ), index=True ) ) |
---|
78 | |
---|
79 | LibraryDatasetDatasetInfoAssociation_table = Table( 'library_dataset_dataset_info_association', metadata, |
---|
80 | Column( "id", Integer, primary_key=True), |
---|
81 | Column( "library_dataset_dataset_association_id", Integer, ForeignKey( "library_dataset_dataset_association.id" ), nullable=True, index=True ), |
---|
82 | Column( "form_definition_id", Integer, ForeignKey( "form_definition.id" ), index=True ), |
---|
83 | Column( "form_values_id", Integer, ForeignKey( "form_values.id" ), index=True ) ) |
---|
84 | |
---|
85 | def upgrade(): |
---|
86 | display_migration_details() |
---|
87 | # Load existing tables |
---|
88 | metadata.reflect() |
---|
89 | # Drop all of the original library_item_info tables |
---|
90 | # NOTE: all existing library item into template data is eliminated here via table drops |
---|
91 | try: |
---|
92 | LibraryItemInfoPermissions_table = Table( "library_item_info_permissions", metadata, autoload=True ) |
---|
93 | except NoSuchTableError: |
---|
94 | LibraryItemInfoPermissions_table = None |
---|
95 | log.debug( "Failed loading table library_item_info_permissions" ) |
---|
96 | try: |
---|
97 | LibraryItemInfoPermissions_table.drop() |
---|
98 | except Exception, e: |
---|
99 | log.debug( "Dropping library_item_info_permissions table failed: %s" % str( e ) ) |
---|
100 | |
---|
101 | try: |
---|
102 | LibraryItemInfoTemplatePermissions_table = Table( "library_item_info_template_permissions", metadata, autoload=True ) |
---|
103 | except NoSuchTableError: |
---|
104 | LibraryItemInfoTemplatePermissions_table = None |
---|
105 | log.debug( "Failed loading table library_item_info_template_permissions" ) |
---|
106 | try: |
---|
107 | LibraryItemInfoTemplatePermissions_table.drop() |
---|
108 | except Exception, e: |
---|
109 | log.debug( "Dropping library_item_info_template_permissions table failed: %s" % str( e ) ) |
---|
110 | |
---|
111 | try: |
---|
112 | LibraryItemInfoElement_table = Table( "library_item_info_element", metadata, autoload=True ) |
---|
113 | except NoSuchTableError: |
---|
114 | LibraryItemInfoElement_table = None |
---|
115 | log.debug( "Failed loading table library_item_info_element" ) |
---|
116 | try: |
---|
117 | LibraryItemInfoElement_table.drop() |
---|
118 | except Exception, e: |
---|
119 | log.debug( "Dropping library_item_info_element table failed: %s" % str( e ) ) |
---|
120 | |
---|
121 | try: |
---|
122 | LibraryItemInfoTemplateElement_table = Table( "library_item_info_template_element", metadata, autoload=True ) |
---|
123 | except NoSuchTableError: |
---|
124 | LibraryItemInfoTemplateElement_table = None |
---|
125 | log.debug( "Failed loading table library_item_info_template_element" ) |
---|
126 | try: |
---|
127 | LibraryItemInfoTemplateElement_table.drop() |
---|
128 | except Exception, e: |
---|
129 | log.debug( "Dropping library_item_info_template_element table failed: %s" % str( e ) ) |
---|
130 | |
---|
131 | try: |
---|
132 | LibraryInfoTemplateAssociation_table = Table( "library_info_template_association", metadata, autoload=True ) |
---|
133 | except NoSuchTableError: |
---|
134 | LibraryInfoTemplateAssociation_table = None |
---|
135 | log.debug( "Failed loading table library_info_template_association" ) |
---|
136 | try: |
---|
137 | LibraryInfoTemplateAssociation_table.drop() |
---|
138 | except Exception, e: |
---|
139 | log.debug( "Dropping library_info_template_association table failed: %s" % str( e ) ) |
---|
140 | |
---|
141 | try: |
---|
142 | LibraryFolderInfoTemplateAssociation_table = Table( "library_folder_info_template_association", metadata, autoload=True ) |
---|
143 | except NoSuchTableError: |
---|
144 | LibraryFolderInfoTemplateAssociation_table = None |
---|
145 | log.debug( "Failed loading table library_folder_info_template_association" ) |
---|
146 | try: |
---|
147 | LibraryFolderInfoTemplateAssociation_table.drop() |
---|
148 | except Exception, e: |
---|
149 | log.debug( "Dropping library_folder_info_template_association table failed: %s" % str( e ) ) |
---|
150 | |
---|
151 | try: |
---|
152 | LibraryDatasetInfoTemplateAssociation_table = Table( "library_dataset_info_template_association", metadata, autoload=True ) |
---|
153 | except NoSuchTableError: |
---|
154 | LibraryDatasetInfoTemplateAssociation_table = None |
---|
155 | log.debug( "Failed loading table library_dataset_info_template_association" ) |
---|
156 | try: |
---|
157 | LibraryDatasetInfoTemplateAssociation_table.drop() |
---|
158 | except Exception, e: |
---|
159 | log.debug( "Dropping library_dataset_info_template_association table failed: %s" % str( e ) ) |
---|
160 | |
---|
161 | try: |
---|
162 | LibraryDatasetDatasetInfoTemplateAssociation_table = Table( "library_dataset_dataset_info_template_association", metadata, autoload=True ) |
---|
163 | except NoSuchTableError: |
---|
164 | LibraryDatasetDatasetInfoTemplateAssociation_table = None |
---|
165 | log.debug( "Failed loading table library_dataset_dataset_info_template_association" ) |
---|
166 | try: |
---|
167 | LibraryDatasetDatasetInfoTemplateAssociation_table.drop() |
---|
168 | except Exception, e: |
---|
169 | log.debug( "Dropping library_dataset_dataset_info_template_association table failed: %s" % str( e ) ) |
---|
170 | |
---|
171 | try: |
---|
172 | LibraryInfoAssociation_table = Table( "library_info_association", metadata, autoload=True ) |
---|
173 | except NoSuchTableError: |
---|
174 | LibraryInfoAssociation_table = None |
---|
175 | log.debug( "Failed loading table library_info_association" ) |
---|
176 | try: |
---|
177 | LibraryInfoAssociation_table.drop() |
---|
178 | except Exception, e: |
---|
179 | log.debug( "Dropping library_info_association table failed: %s" % str( e ) ) |
---|
180 | |
---|
181 | try: |
---|
182 | LibraryFolderInfoAssociation_table = Table( "library_folder_info_association", metadata, autoload=True ) |
---|
183 | except NoSuchTableError: |
---|
184 | LibraryFolderInfoAssociation_table = None |
---|
185 | log.debug( "Failed loading table library_folder_info_association" ) |
---|
186 | try: |
---|
187 | LibraryFolderInfoAssociation_table.drop() |
---|
188 | except Exception, e: |
---|
189 | log.debug( "Dropping library_folder_info_association table failed: %s" % str( e ) ) |
---|
190 | |
---|
191 | try: |
---|
192 | LibraryDatasetInfoAssociation_table = Table( "library_dataset_info_association", metadata, autoload=True ) |
---|
193 | except NoSuchTableError: |
---|
194 | LibraryDatasetInfoAssociation_table = None |
---|
195 | log.debug( "Failed loading table library_dataset_info_association" ) |
---|
196 | try: |
---|
197 | LibraryDatasetInfoAssociation_table.drop() |
---|
198 | except Exception, e: |
---|
199 | log.debug( "Dropping library_dataset_info_association table failed: %s" % str( e ) ) |
---|
200 | |
---|
201 | try: |
---|
202 | LibraryDatasetDatasetInfoAssociation_table = Table( "library_dataset_dataset_info_association", metadata, autoload=True ) |
---|
203 | except NoSuchTableError: |
---|
204 | LibraryDatasetDatasetInfoAssociation_table = None |
---|
205 | log.debug( "Failed loading table library_dataset_dataset_info_association" ) |
---|
206 | try: |
---|
207 | LibraryDatasetDatasetInfoAssociation_table.drop() |
---|
208 | except Exception, e: |
---|
209 | log.debug( "Dropping library_dataset_dataset_info_association table failed: %s" % str( e ) ) |
---|
210 | |
---|
211 | try: |
---|
212 | LibraryItemInfo_table = Table( "library_item_info", metadata, autoload=True ) |
---|
213 | except NoSuchTableError: |
---|
214 | LibraryItemInfo_table = None |
---|
215 | log.debug( "Failed loading table library_item_info" ) |
---|
216 | try: |
---|
217 | LibraryItemInfo_table.drop() |
---|
218 | except Exception, e: |
---|
219 | log.debug( "Dropping library_item_info table failed: %s" % str( e ) ) |
---|
220 | |
---|
221 | try: |
---|
222 | LibraryItemInfoTemplate_table = Table( "library_item_info_template", metadata, autoload=True ) |
---|
223 | except NoSuchTableError: |
---|
224 | LibraryItemInfoTemplate_table = None |
---|
225 | log.debug( "Failed loading table library_item_info_template" ) |
---|
226 | try: |
---|
227 | LibraryItemInfoTemplate_table.drop() |
---|
228 | except Exception, e: |
---|
229 | log.debug( "Dropping library_item_info_template table failed: %s" % str( e ) ) |
---|
230 | |
---|
231 | # Create all new tables above |
---|
232 | try: |
---|
233 | LibraryInfoAssociation_table.create() |
---|
234 | except Exception, e: |
---|
235 | log.debug( "Creating library_info_association table failed: %s" % str( e ) ) |
---|
236 | try: |
---|
237 | LibraryFolderInfoAssociation_table.create() |
---|
238 | except Exception, e: |
---|
239 | log.debug( "Creating library_folder_info_association table failed: %s" % str( e ) ) |
---|
240 | try: |
---|
241 | LibraryDatasetDatasetInfoAssociation_table.create() |
---|
242 | except Exception, e: |
---|
243 | log.debug( "Creating library_dataset_dataset_info_association table failed: %s" % str( e ) ) |
---|
244 | # Fix index on LibraryDatasetDatasetInfoAssociation_table for mysql |
---|
245 | if migrate_engine.name == 'mysql': |
---|
246 | # Load existing tables |
---|
247 | metadata.reflect() |
---|
248 | i = Index( "ix_lddaia_ldda_id", LibraryDatasetDatasetInfoAssociation_table.c.library_dataset_dataset_association_id ) |
---|
249 | try: |
---|
250 | i.create() |
---|
251 | except Exception, e: |
---|
252 | log.debug( "Adding index 'ix_lddaia_ldda_id' to table 'library_dataset_dataset_info_association' table failed: %s" % str( e ) ) |
---|
253 | |
---|
254 | def downgrade(): |
---|
255 | log.debug( "Downgrade is not possible." ) |
---|