/lib/galaxy/model/migrate/versions/0015_tagging.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 116 lines · 89 code · 10 blank · 17 comment · 17 complexity · 61cb74edf87aa8c164b05d906ddc777c MD5 · raw file

  1. """
  2. This migration script adds the tables necessary to support tagging of histories,
  3. datasets, and history-dataset associations (user views of datasets).
  4. If using mysql, this script will display the following error, which is corrected in the next
  5. migration script:
  6. history_dataset_association_tag_association table failed: (OperationalError)
  7. (1059, "Identifier name 'ix_history_dataset_association_tag_association_history_dataset_association_id'
  8. is too long)
  9. """
  10. from sqlalchemy import *
  11. from migrate import *
  12. import datetime
  13. now = datetime.datetime.utcnow
  14. # Need our custom types, but don't import anything else from model
  15. from galaxy.model.custom_types import *
  16. import logging
  17. log = logging.getLogger( __name__ )
  18. metadata = MetaData()
  19. def display_migration_details():
  20. print ""
  21. print "This migration script adds the tables necessary to support tagging of histories,"
  22. print "datasets, and history-dataset associations (user views of datasets)."
  23. print ""
  24. print "If using mysql, this script will display the following error, which is "
  25. print "corrected in the next migration script:"
  26. print "history_dataset_association_tag_association table failed: "
  27. print "(OperationalError) (1059, 'Identifier name "
  28. print "'ix_history_dataset_association_tag_association_history_dataset_association_id'"
  29. print "is too long)"
  30. # New tables to support tagging of histories, datasets, and history-dataset associations.
  31. Tag_table = Table( "tag", metadata,
  32. Column( "id", Integer, primary_key=True ),
  33. Column( "type", Integer ),
  34. Column( "parent_id", Integer, ForeignKey( "tag.id" ) ),
  35. Column( "name", TrimmedString(255) ),
  36. UniqueConstraint( "name" ) )
  37. HistoryTagAssociation_table = Table( "history_tag_association", metadata,
  38. Column( "history_id", Integer, ForeignKey( "history.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. DatasetTagAssociation_table = Table( "dataset_tag_association", metadata,
  44. Column( "dataset_id", Integer, ForeignKey( "dataset.id" ), index=True ),
  45. Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
  46. Column( "user_tname", TrimmedString(255), index=True),
  47. Column( "value", TrimmedString(255), index=True),
  48. Column( "user_value", TrimmedString(255), index=True) )
  49. HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata,
  50. Column( "history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id" ), index=True ),
  51. Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
  52. Column( "user_tname", TrimmedString(255), index=True),
  53. Column( "value", TrimmedString(255), index=True),
  54. Column( "user_value", TrimmedString(255), index=True) )
  55. def upgrade(migrate_engine):
  56. metadata.bind = migrate_engine
  57. display_migration_details()
  58. metadata.reflect()
  59. try:
  60. Tag_table.create()
  61. except Exception, e:
  62. print str(e)
  63. log.debug( "Creating tag table failed: %s" % str( e ) )
  64. try:
  65. HistoryTagAssociation_table.create()
  66. except Exception, e:
  67. print str(e)
  68. log.debug( "Creating history_tag_association table failed: %s" % str( e ) )
  69. try:
  70. DatasetTagAssociation_table.create()
  71. except Exception, e:
  72. print str(e)
  73. log.debug( "Creating dataset_tag_association table failed: %s" % str( e ) )
  74. try:
  75. HistoryDatasetAssociationTagAssociation_table.create()
  76. except Exception, e:
  77. print str(e)
  78. log.debug( "Creating history_dataset_association_tag_association table failed: %s" % str( e ) )
  79. def downgrade(migrate_engine):
  80. metadata.bind = migrate_engine
  81. metadata.reflect()
  82. try:
  83. Tag_table.drop()
  84. except Exception, e:
  85. print str(e)
  86. log.debug( "Dropping tag table failed: %s" % str( e ) )
  87. try:
  88. HistoryTagAssociation_table.drop()
  89. except Exception, e:
  90. print str(e)
  91. log.debug( "Dropping history_tag_association table failed: %s" % str( e ) )
  92. try:
  93. DatasetTagAssociation_table.drop()
  94. except Exception, e:
  95. print str(e)
  96. log.debug( "Dropping dataset_tag_association table failed: %s" % str( e ) )
  97. try:
  98. HistoryDatasetAssociationTagAssociation_table.drop()
  99. except Exception, e:
  100. print str(e)
  101. log.debug( "Dropping history_dataset_association_tag_association table failed: %s" % str( e ) )