/lib/galaxy/model/migrate/versions/0013_change_lib_item_templates_to_forms.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 256 lines · 238 code · 1 blank · 17 comment · 3 complexity · 632e258903a4b64b5d1c13ca59ec65e3 MD5 · raw file

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