/lib/galaxy/model/migrate/versions/0064_add_run_and_sample_run_association_tables.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 68 lines · 55 code · 9 blank · 4 comment · 12 complexity · f4a9139b5fab0c9a0d6d450aac30eca1 MD5 · raw file

  1. """
  2. Migration script to add the run and sample_run_association tables.
  3. """
  4. from sqlalchemy import *
  5. from migrate import *
  6. from migrate.changeset import *
  7. from galaxy.model.custom_types import *
  8. import datetime
  9. now = datetime.datetime.utcnow
  10. import logging
  11. log = logging.getLogger( __name__ )
  12. metadata = MetaData()
  13. Run_table = Table( "run", metadata,
  14. Column( "id", Integer, primary_key=True ),
  15. Column( "create_time", DateTime, default=now ),
  16. Column( "update_time", DateTime, default=now, onupdate=now ),
  17. Column( "form_definition_id", Integer, ForeignKey( "form_definition.id" ), index=True ),
  18. Column( "form_values_id", Integer, ForeignKey( "form_values.id" ), index=True ),
  19. Column( "deleted", Boolean, index=True, default=False ) )
  20. RequestTypeRunAssociation_table = Table( "request_type_run_association", metadata,
  21. Column( "id", Integer, primary_key=True ),
  22. Column( "request_type_id", Integer, ForeignKey( "request_type.id" ), index=True, nullable=False ),
  23. Column( "run_id", Integer, ForeignKey( "run.id" ), index=True, nullable=False ) )
  24. SampleRunAssociation_table = Table( "sample_run_association", metadata,
  25. Column( "id", Integer, primary_key=True ),
  26. Column( "sample_id", Integer, ForeignKey( "sample.id" ), index=True, nullable=False ),
  27. Column( "run_id", Integer, ForeignKey( "run.id" ), index=True, nullable=False ) )
  28. def upgrade(migrate_engine):
  29. metadata.bind = migrate_engine
  30. print __doc__
  31. metadata.reflect()
  32. try:
  33. Run_table.create()
  34. except Exception, e:
  35. log.debug( "Creating Run_table table failed: %s" % str( e ) )
  36. try:
  37. RequestTypeRunAssociation_table.create()
  38. except Exception, e:
  39. log.debug( "Creating RequestTypeRunAssociation table failed: %s" % str( e ) )
  40. try:
  41. SampleRunAssociation_table.create()
  42. except Exception, e:
  43. log.debug( "Creating SampleRunAssociation table failed: %s" % str( e ) )
  44. def downgrade(migrate_engine):
  45. metadata.bind = migrate_engine
  46. # Load existing tables
  47. metadata.reflect()
  48. try:
  49. SampleRunAssociation_table.drop()
  50. except Exception, e:
  51. log.debug( "Dropping SampleRunAssociation table failed: %s" % str( e ) )
  52. try:
  53. RequestTypeRunAssociation_table.drop()
  54. except Exception, e:
  55. log.debug( "Dropping RequestTypeRunAssociation table failed: %s" % str( e ) )
  56. try:
  57. Run_table.drop()
  58. except Exception, e:
  59. log.debug( "Dropping Run_table table failed: %s" % str( e ) )