/lib/galaxy/webapps/community/model/migrate/versions/0005_drop_tool_related_tables.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 193 lines · 157 code · 12 blank · 24 comment · 42 complexity · 7bbecc7a72f857804919ed953161c37a MD5 · raw file

  1. """
  2. Drops the tool, tool_category_association, event, tool_event_association, tool_rating_association,
  3. tool_tag_association and tool_annotation_association tables since they are no longer used in the
  4. next-gen tool shed.
  5. """
  6. from sqlalchemy import *
  7. from sqlalchemy.orm import *
  8. from sqlalchemy.exc import *
  9. from migrate import *
  10. from migrate.changeset import *
  11. import datetime
  12. now = datetime.datetime.utcnow
  13. import sys, logging
  14. log = logging.getLogger( __name__ )
  15. log.setLevel( logging.DEBUG )
  16. handler = logging.StreamHandler( sys.stdout )
  17. format = "%(name)s %(levelname)s %(asctime)s %(message)s"
  18. formatter = logging.Formatter( format )
  19. handler.setFormatter( formatter )
  20. log.addHandler( handler )
  21. # Need our custom types, but don't import anything else from model
  22. from galaxy.model.custom_types import *
  23. metadata = MetaData( migrate_engine )
  24. db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
  25. def upgrade():
  26. print __doc__
  27. # Load existing tables
  28. metadata.reflect()
  29. # Load and then drop the tool_category_association table
  30. try:
  31. ToolCategoryAssociation_table = Table( "tool_category_association", metadata, autoload=True )
  32. except NoSuchTableError:
  33. log.debug( "Failed loading table tool_category_association" )
  34. try:
  35. ToolCategoryAssociation_table.drop()
  36. except Exception, e:
  37. log.debug( "Dropping tool_category_association table failed: %s" % str( e ) )
  38. # Load and then drop the tool_event_association table
  39. try:
  40. ToolEventAssociation_table = Table( "tool_event_association", metadata, autoload=True )
  41. except NoSuchTableError:
  42. log.debug( "Failed loading table tool_event_association" )
  43. try:
  44. ToolEventAssociation_table.drop()
  45. except Exception, e:
  46. log.debug( "Dropping tool_event_association table failed: %s" % str( e ) )
  47. # Load and then drop the tool_rating_association table
  48. try:
  49. ToolRatingAssociation_table = Table( "tool_rating_association", metadata, autoload=True )
  50. except NoSuchTableError:
  51. log.debug( "Failed loading table tool_rating_association" )
  52. try:
  53. ToolRatingAssociation_table.drop()
  54. except Exception, e:
  55. log.debug( "Dropping tool_rating_association table failed: %s" % str( e ) )
  56. # Load and then drop the tool_tag_association table
  57. try:
  58. ToolTagAssociation_table = Table( "tool_tag_association", metadata, autoload=True )
  59. except NoSuchTableError:
  60. log.debug( "Failed loading table tool_tag_association" )
  61. try:
  62. ToolTagAssociation_table.drop()
  63. except Exception, e:
  64. log.debug( "Dropping tool_tag_association table failed: %s" % str( e ) )
  65. # Load and then drop the tool_annotation_association table
  66. try:
  67. ToolAnnotationAssociation_table = Table( "tool_annotation_association", metadata, autoload=True )
  68. except NoSuchTableError:
  69. log.debug( "Failed loading table tool_annotation_association" )
  70. try:
  71. ToolAnnotationAssociation_table.drop()
  72. except Exception, e:
  73. log.debug( "Dropping tool_annotation_association table failed: %s" % str( e ) )
  74. # Load and then drop the event table
  75. try:
  76. Event_table = Table( "event", metadata, autoload=True )
  77. except NoSuchTableError:
  78. log.debug( "Failed loading table event" )
  79. try:
  80. Event_table.drop()
  81. except Exception, e:
  82. log.debug( "Dropping event table failed: %s" % str( e ) )
  83. # Load and then drop the tool table
  84. try:
  85. Tool_table = Table( "tool", metadata, autoload=True )
  86. except NoSuchTableError:
  87. log.debug( "Failed loading table tool" )
  88. try:
  89. Tool_table.drop()
  90. except Exception, e:
  91. log.debug( "Dropping tool table failed: %s" % str( e ) )
  92. def downgrade():
  93. # Load existing tables
  94. metadata.reflect()
  95. # We've lost all of our data, so downgrading is useless. However, we'll
  96. # at least re-create the dropped tables.
  97. Event_table = Table( 'event', metadata,
  98. Column( "id", Integer, primary_key=True ),
  99. Column( "create_time", DateTime, default=now ),
  100. Column( "update_time", DateTime, default=now, onupdate=now ),
  101. Column( "state", TrimmedString( 255 ), index=True ),
  102. Column( "comment", TEXT ) )
  103. Tool_table = Table( "tool", metadata,
  104. Column( "id", Integer, primary_key=True ),
  105. Column( "guid", TrimmedString( 255 ), index=True, unique=True ),
  106. Column( "tool_id", TrimmedString( 255 ), index=True ),
  107. Column( "create_time", DateTime, default=now ),
  108. Column( "update_time", DateTime, default=now, onupdate=now ),
  109. Column( "newer_version_id", Integer, ForeignKey( "tool.id" ), nullable=True ),
  110. Column( "name", TrimmedString( 255 ), index=True ),
  111. Column( "description" , TEXT ),
  112. Column( "user_description" , TEXT ),
  113. Column( "version", TrimmedString( 255 ) ),
  114. Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
  115. Column( "external_filename" , TEXT ),
  116. Column( "deleted", Boolean, index=True, default=False ),
  117. Column( "suite", Boolean, default=False, index=True ) )
  118. ToolCategoryAssociation_table = Table( "tool_category_association", metadata,
  119. Column( "id", Integer, primary_key=True ),
  120. Column( "tool_id", Integer, ForeignKey( "tool.id" ), index=True ),
  121. Column( "category_id", Integer, ForeignKey( "category.id" ), index=True ) )
  122. ToolEventAssociation_table = Table( "tool_event_association", metadata,
  123. Column( "id", Integer, primary_key=True ),
  124. Column( "tool_id", Integer, ForeignKey( "tool.id" ), index=True ),
  125. Column( "event_id", Integer, ForeignKey( "event.id" ), index=True ) )
  126. ToolRatingAssociation_table = Table( "tool_rating_association", metadata,
  127. Column( "id", Integer, primary_key=True ),
  128. Column( "create_time", DateTime, default=now ),
  129. Column( "update_time", DateTime, default=now, onupdate=now ),
  130. Column( "tool_id", Integer, ForeignKey( "tool.id" ), index=True ),
  131. Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
  132. Column( "rating", Integer, index=True ),
  133. Column( "comment", TEXT ) )
  134. ToolTagAssociation_table = Table( "tool_tag_association", metadata,
  135. Column( "id", Integer, primary_key=True ),
  136. Column( "tool_id", Integer, ForeignKey( "tool.id" ), index=True ),
  137. Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
  138. Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
  139. Column( "user_tname", TrimmedString(255), index=True),
  140. Column( "value", TrimmedString(255), index=True),
  141. Column( "user_value", TrimmedString(255), index=True) )
  142. ToolAnnotationAssociation_table = Table( "tool_annotation_association", metadata,
  143. Column( "id", Integer, primary_key=True ),
  144. Column( "tool_id", Integer, ForeignKey( "tool.id" ), index=True ),
  145. Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
  146. Column( "annotation", TEXT, index=True) )
  147. # Create the event table
  148. try:
  149. Event_table.create()
  150. except Exception, e:
  151. log.debug( "Creating event table failed: %s" % str( e ) )
  152. # Create the tool table
  153. try:
  154. Tool_table.create()
  155. except Exception, e:
  156. log.debug( "Creating tool table failed: %s" % str( e ) )
  157. # Create the tool_category_association table
  158. try:
  159. ToolCategoryAssociation_table.create()
  160. except Exception, e:
  161. log.debug( "Creating tool_category_association table failed: %s" % str( e ) )
  162. # Create the tool_event_association table
  163. try:
  164. ToolEventAssociation_table.create()
  165. except Exception, e:
  166. log.debug( "Creating tool_event_association table failed: %s" % str( e ) )
  167. # Create the tool_rating_association table
  168. try:
  169. ToolRatingAssociation_table.create()
  170. except Exception, e:
  171. log.debug( "Creating tool_rating_association table failed: %s" % str( e ) )
  172. # Create the tool_tag_association table
  173. try:
  174. ToolTagAssociation_table.create()
  175. except Exception, e:
  176. log.debug( "Creating tool_tag_association table failed: %s" % str( e ) )
  177. # Create the tool_annotation_association table
  178. try:
  179. ToolAnnotationAssociation_table.create()
  180. except Exception, e:
  181. log.debug( "Creating tool_annotation_association table failed: %s" % str( e ) )