PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0011_v0010_mysql_index_fix.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 65 lines | 63 code | 0 blank | 2 comment | 1 complexity | 88ea06fff8a258ae174503cad0e6aa0a MD5 | raw file
  1. """
  2. This script fixes a problem introduced in 0010_hda_display_at_atuhz_table.py. MySQL has a
  3. name length limit and thus the index "ix_hdadaa_history_dataset_association_id" has to be
  4. manually created.
  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()
  24. def display_migration_details():
  25. print "========================================"
  26. print "This script fixes a problem introduced in the previous migration script ( 9->10 ). MySQL"
  27. print "has a name length limit and thus the index 'ix_hdadaa_history_dataset_association_id' has"
  28. print "to be manually created."
  29. print "========================================"
  30. HistoryDatasetAssociationDisplayAtAuthorization_table = Table( "history_dataset_association_display_at_authorization", metadata,
  31. Column( "id", Integer, primary_key=True ),
  32. Column( "create_time", DateTime, default=now ),
  33. Column( "update_time", DateTime, index=True, default=now, onupdate=now ),
  34. Column( "history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id" ), index=True ),
  35. Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
  36. Column( "site", TrimmedString( 255 ) ) )
  37. def upgrade(migrate_engine):
  38. metadata.bind = migrate_engine
  39. display_migration_details()
  40. if migrate_engine.name == 'mysql':
  41. # Load existing tables
  42. metadata.reflect()
  43. i = Index( "ix_hdadaa_history_dataset_association_id", HistoryDatasetAssociationDisplayAtAuthorization_table.c.history_dataset_association_id )
  44. try:
  45. i.create()
  46. except Exception, e:
  47. log.debug( "Adding index 'ix_hdadaa_history_dataset_association_id' to table 'history_dataset_association_display_at_authorization' table failed: %s" % str( e ) )
  48. def downgrade(migrate_engine):
  49. metadata.bind = migrate_engine
  50. if migrate_engine.name == 'mysql':
  51. # Load existing tables
  52. metadata.reflect()
  53. i = Index( "ix_hdadaa_history_dataset_association_id", HistoryDatasetAssociationDisplayAtAuthorization_table.c.history_dataset_association_id )
  54. try:
  55. i.drop()
  56. except Exception, e:
  57. log.debug( "Removing index 'ix_hdadaa_history_dataset_association_id' from table 'history_dataset_association_display_at_authorization' table failed: %s" % str( e ) )