PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0018_ordered_tags_and_page_tags.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 118 lines | 107 code | 6 blank | 5 comment | 1 complexity | 16b5665ced58671287a58df6cc07fa89 MD5 | raw file
  1. """
  2. This migration script provides support for (a) ordering tags by recency and
  3. (b) tagging pages. This script deletes all existing tags.
  4. """
  5. from sqlalchemy import *
  6. from sqlalchemy.orm import *
  7. from sqlalchemy.exc import *
  8. from migrate import *
  9. import migrate.changeset
  10. import datetime
  11. now = datetime.datetime.utcnow
  12. # Need our custom types, but don't import anything else from model
  13. from galaxy.model.custom_types import *
  14. import logging
  15. log = logging.getLogger( __name__ )
  16. metadata = MetaData()
  17. def display_migration_details():
  18. print ""
  19. print "This migration script provides support for (a) ordering tags by recency and"
  20. print "(b) tagging pages. This script deletes all existing tags."
  21. HistoryTagAssociation_table = Table( "history_tag_association", metadata,
  22. Column( "id", Integer, primary_key=True ),
  23. Column( "history_id", Integer, ForeignKey( "history.id" ), index=True ),
  24. Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
  25. Column( "user_tname", TrimmedString(255), index=True),
  26. Column( "value", TrimmedString(255), index=True),
  27. Column( "user_value", TrimmedString(255), index=True) )
  28. DatasetTagAssociation_table = Table( "dataset_tag_association", metadata,
  29. Column( "id", Integer, primary_key=True ),
  30. Column( "dataset_id", Integer, ForeignKey( "dataset.id" ), index=True ),
  31. Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
  32. Column( "user_tname", TrimmedString(255), index=True),
  33. Column( "value", TrimmedString(255), index=True),
  34. Column( "user_value", TrimmedString(255), index=True) )
  35. HistoryDatasetAssociationTagAssociation_table = Table( "history_dataset_association_tag_association", metadata,
  36. Column( "id", Integer, primary_key=True ),
  37. Column( "history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id" ), index=True ),
  38. Column( "tag_id", Integer, ForeignKey( "tag.id" ), index=True ),
  39. Column( "user_tname", TrimmedString(255), index=True),
  40. Column( "value", TrimmedString(255), index=True),
  41. Column( "user_value", TrimmedString(255), index=True) )
  42. PageTagAssociation_table = Table( "page_tag_association", metadata,
  43. Column( "id", Integer, primary_key=True ),
  44. Column( "page_id", Integer, ForeignKey( "page.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. def upgrade(migrate_engine):
  50. metadata.bind = migrate_engine
  51. display_migration_details()
  52. metadata.reflect()
  53. #
  54. # Recreate tables.
  55. #
  56. try:
  57. HistoryTagAssociation_table.drop()
  58. HistoryTagAssociation_table.create()
  59. except Exception, e:
  60. print "Recreating history_tag_association table failed: %s" % str( e )
  61. log.debug( "Recreating history_tag_association table failed: %s" % str( e ) )
  62. try:
  63. DatasetTagAssociation_table.drop()
  64. DatasetTagAssociation_table.create()
  65. except Exception, e:
  66. print str(e)
  67. log.debug( "Recreating dataset_tag_association table failed: %s" % str( e ) )
  68. try:
  69. HistoryDatasetAssociationTagAssociation_table.drop()
  70. HistoryDatasetAssociationTagAssociation_table.create()
  71. except OperationalError, e:
  72. # Handle error that results from and index name that is too long; this occurs
  73. # in MySQL.
  74. if str(e).find("CREATE INDEX") != -1:
  75. # Manually create index.
  76. i = Index( "ix_hda_ta_history_dataset_association_id", HistoryDatasetAssociationTagAssociation_table.c.history_dataset_association_id )
  77. try:
  78. i.create()
  79. except Exception, e:
  80. print str(e)
  81. log.debug( "Adding index 'ix_hda_ta_history_dataset_association_id' to table 'history_dataset_association_tag_association' table failed: %s" % str( e ) )
  82. except Exception, e:
  83. print str(e)
  84. log.debug( "Recreating history_dataset_association_tag_association table failed: %s" % str( e ) )
  85. # Create page_tag_association table.
  86. try:
  87. PageTagAssociation_table.create()
  88. except Exception, e:
  89. print str(e)
  90. log.debug( "Creating page_tag_association table failed: %s" % str( e ) )
  91. def downgrade(migrate_engine):
  92. metadata.bind = migrate_engine
  93. metadata.reflect()
  94. # No need to downgrade other tagging tables. They work fine with verision 16 code.
  95. # Drop page_tag_association table.
  96. try:
  97. PageTagAssociation_table.drop()
  98. except Exception, e:
  99. print str(e)
  100. log.debug( "Dropping page_tag_association table failed: %s" % str( e ) )