/lib/galaxy/model/migrate/versions/0071_add_history_and_workflow_to_sample.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 46 lines · 45 code · 0 blank · 1 comment · 0 complexity · f68fca673dbd9a179936d6cab6b4092a MD5 · raw file

  1. """
  2. Migration script to add 'workflow' and 'history' columns for a sample.
  3. """
  4. from sqlalchemy import *
  5. from sqlalchemy.orm import *
  6. from migrate import *
  7. from migrate.changeset import *
  8. from galaxy.model.custom_types import *
  9. import logging
  10. log = logging.getLogger( __name__ )
  11. metadata = MetaData()
  12. def upgrade(migrate_engine):
  13. metadata.bind = migrate_engine
  14. print __doc__
  15. metadata.reflect()
  16. try:
  17. Sample_table = Table( "sample", metadata, autoload=True )
  18. c1 = Column( "workflow", JSONType, nullable=True )
  19. c2 = Column( "history_id", Integer, ForeignKey( "history.id" ), nullable=True)
  20. c1.create( Sample_table )
  21. c2.create( Sample_table )
  22. assert c1 is Sample_table.c.workflow
  23. assert c2 is Sample_table.c.history_id
  24. except Exception, e:
  25. print "Adding history and workflow columns to sample table failed: %s" % str( e )
  26. log.debug( "Adding history and workflow columns to sample table failed: %s" % str( e ) )
  27. def downgrade(migrate_engine):
  28. metadata.bind = migrate_engine
  29. metadata.reflect()
  30. try:
  31. Sample_table = Table( "sample", metadata, autoload=True )
  32. Sample_table.c.workflow.drop()
  33. except Exception, e:
  34. print "Dropping workflow column from sample table failed: %s" % str( e )
  35. log.debug( "Dropping workflow column from sample table failed: %s" % str( e ) )
  36. try:
  37. Sample_table = Table( "sample", metadata, autoload=True )
  38. Sample_table.c.history_id.drop()
  39. except Exception, e:
  40. print "Dropping history column from sample table failed: %s" % str( e )
  41. log.debug( "Dropping history column from sample table failed: %s" % str( e ) )