/lib/galaxy/model/migrate/versions/0031_community_and_workflow_tags.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 182 lines · 178 code · 0 blank · 4 comment · 0 complexity · 09e9c397a6b6217b0dda8f591c4a86a4 MD5 · raw file

  1. """
  2. Migration script to (a) add and populate necessary columns for doing community tagging of histories, datasets, and pages and \
  3. (b) add table for doing individual and community tagging of workflows.
  4. SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so this script will generate error messages when run against \
  5. SQLite; however, script does execute successfully against SQLite.
  6. """
  7. from sqlalchemy import *
  8. from sqlalchemy.orm import *
  9. from migrate import *
  10. from migrate.changeset import *
  11. import logging
  12. log = logging.getLogger( __name__ )
  13. metadata = MetaData()
  14. StoredWorkflowTagAssociation_table = Table( "stored_workflow_tag_association", metadata,
  15. Column( "id", Integer, primary_key=True ),
  16. Column( "stored_workflow_id", Integer, ForeignKey( "stored_workflow.id" ), index=True ),
  17. Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
  18. Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
  19. Column( "user_tname", Unicode(255), index=True),
  20. Column( "value", Unicode(255), index=True),
  21. Column( "user_value", Unicode(255), index=True) )
  22. WorkflowTagAssociation_table = Table( "workflow_tag_association", metadata,
  23. Column( "id", Integer, primary_key=True ),
  24. Column( "workflow_id", Integer, ForeignKey( "workflow.id" ), index=True ),
  25. Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
  26. Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
  27. Column( "user_tname", Unicode(255), index=True),
  28. Column( "value", Unicode(255), index=True),
  29. Column( "user_value", Unicode(255), index=True) )
  30. def upgrade(migrate_engine):
  31. metadata.bind = migrate_engine
  32. print __doc__
  33. metadata.reflect()
  34. # Create user_id column in history_tag_association table.
  35. HistoryTagAssociation_table = Table( "history_tag_association", metadata, autoload=True )
  36. if migrate_engine.name != 'sqlite':
  37. c = Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True )
  38. try:
  39. c.create( HistoryTagAssociation_table, index_name='ix_history_tag_association_user_id')
  40. assert c is HistoryTagAssociation_table.c.user_id
  41. except Exception, e:
  42. # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
  43. print str(e)
  44. log.debug( "Adding user_id column to history_tag_association table failed: %s" % str( e ) )
  45. else:
  46. c = Column( "user_id", Integer)
  47. try:
  48. c.create( HistoryTagAssociation_table)
  49. assert c is HistoryTagAssociation_table.c.user_id
  50. except Exception, e:
  51. # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
  52. print str(e)
  53. log.debug( "Adding user_id column to history_tag_association table failed: %s" % str( e ) )
  54. # Populate column so that user_id is the id of the user who owns the history (and, up to now, was the only person able to tag the history).
  55. if c is HistoryTagAssociation_table.c.user_id:
  56. migrate_engine.execute(
  57. "UPDATE history_tag_association SET user_id=( SELECT user_id FROM history WHERE history_tag_association.history_id = history.id )"
  58. )
  59. if migrate_engine.name != 'sqlite':
  60. # Create user_id column in history_dataset_association_tag_association table.
  61. HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata, autoload=True )
  62. c = Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True )
  63. try:
  64. c.create( HistoryDatasetAssociationTagAssociation_table, index_name='ix_history_dataset_association_tag_association_user_id')
  65. assert c is HistoryDatasetAssociationTagAssociation_table.c.user_id
  66. except Exception, e:
  67. # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
  68. print str(e)
  69. log.debug( "Adding user_id column to history_dataset_association_tag_association table failed: %s" % str( e ) )
  70. else:
  71. #In sqlite, we can no longer quietly fail to add foreign key.
  72. # Create user_id column in history_dataset_association_tag_association table.
  73. HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata, autoload=True )
  74. c = Column( "user_id", Integer)
  75. try:
  76. c.create( HistoryDatasetAssociationTagAssociation_table)
  77. assert c is HistoryDatasetAssociationTagAssociation_table.c.user_id
  78. except Exception, e:
  79. # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
  80. print str(e)
  81. log.debug( "Adding user_id column to history_dataset_association_tag_association table failed: %s" % str( e ) )
  82. # Populate column so that user_id is the id of the user who owns the history_dataset_association (and, up to now, was the only person able to tag the page).
  83. if c is HistoryDatasetAssociationTagAssociation_table.c.user_id:
  84. migrate_engine.execute(
  85. "UPDATE history_dataset_association_tag_association SET user_id=( SELECT history.user_id FROM history, history_dataset_association WHERE history_dataset_association.history_id = history.id AND history_dataset_association.id = history_dataset_association_tag_association.history_dataset_association_id)"
  86. )
  87. if migrate_engine.name != 'sqlite':
  88. # Create user_id column in page_tag_association table.
  89. PageTagAssociation_table = Table( "page_tag_association", metadata, autoload=True )
  90. c = Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True )
  91. try:
  92. c.create( PageTagAssociation_table, index_name='ix_page_tag_association_user_id')
  93. assert c is PageTagAssociation_table.c.user_id
  94. except Exception, e:
  95. # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
  96. print str(e)
  97. log.debug( "Adding user_id column to page_tag_association table failed: %s" % str( e ) )
  98. else:
  99. # Create user_id column in page_tag_association table.
  100. PageTagAssociation_table = Table( "page_tag_association", metadata, autoload=True )
  101. c = Column( "user_id", Integer )
  102. try:
  103. c.create( PageTagAssociation_table )
  104. assert c is PageTagAssociation_table.c.user_id
  105. except Exception, e:
  106. # SQLite does not support 'ALTER TABLE ADD FOREIGN KEY', so catch exception if it arises.
  107. print str(e)
  108. log.debug( "Adding user_id column to page_tag_association table failed: %s" % str( e ) )
  109. # Populate column so that user_id is the id of the user who owns the page (and, up to now, was the only person able to tag the page).
  110. if c is PageTagAssociation_table.c.user_id:
  111. migrate_engine.execute(
  112. "UPDATE page_tag_association SET user_id=( SELECT user_id FROM page WHERE page_tag_association.page_id = page.id )"
  113. )
  114. # Create stored_workflow_tag_association table.
  115. try:
  116. StoredWorkflowTagAssociation_table.create()
  117. except Exception, e:
  118. print str(e)
  119. log.debug( "Creating stored_workflow_tag_association table failed: %s" % str( e ) )
  120. # Create workflow_tag_association table.
  121. try:
  122. WorkflowTagAssociation_table.create()
  123. except Exception, e:
  124. print str(e)
  125. log.debug( "Creating workflow_tag_association table failed: %s" % str( e ) )
  126. def downgrade(migrate_engine):
  127. metadata.bind = migrate_engine
  128. metadata.reflect()
  129. # Drop user_id column from history_tag_association table.
  130. HistoryTagAssociation_table = Table( "history_tag_association", metadata, autoload=True )
  131. try:
  132. HistoryTagAssociation_table.c.user_id.drop()
  133. except Exception, e:
  134. print str(e)
  135. log.debug( "Dropping column user_id from history_tag_association table failed: %s" % str( e ) )
  136. # Drop user_id column from history_dataset_association_tag_association table.
  137. HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata, autoload=True )
  138. try:
  139. HistoryDatasetAssociationTagAssociation_table.c.user_id.drop()
  140. except Exception, e:
  141. print str(e)
  142. log.debug( "Dropping column user_id from history_dataset_association_tag_association table failed: %s" % str( e ) )
  143. # Drop user_id column from page_tag_association table.
  144. PageTagAssociation_table = Table( "page_tag_association", metadata, autoload=True )
  145. try:
  146. PageTagAssociation_table.c.user_id.drop()
  147. except Exception, e:
  148. print str(e)
  149. log.debug( "Dropping column user_id from page_tag_association table failed: %s" % str( e ) )
  150. # Drop stored_workflow_tag_association table.
  151. try:
  152. StoredWorkflowTagAssociation_table.drop()
  153. except Exception, e:
  154. print str(e)
  155. log.debug( "Dropping stored_workflow_tag_association table failed: %s" % str( e ) )
  156. # Drop workflow_tag_association table.
  157. try:
  158. WorkflowTagAssociation_table.drop()
  159. except Exception, e:
  160. print str(e)
  161. log.debug( "Dropping workflow_tag_association table failed: %s" % str( e ) )