/lib/galaxy/model/migrate/versions/0060_history_archive_import.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 73 lines · 48 code · 14 blank · 11 comment · 11 complexity · aab7c98ef72a52e790eb8b383fc20615 MD5 · raw file

  1. """
  2. Migration script to create column and table for importing histories from
  3. file archives.
  4. """
  5. from sqlalchemy import *
  6. from sqlalchemy.orm import *
  7. from migrate import *
  8. from migrate.changeset import *
  9. import logging
  10. log = logging.getLogger( __name__ )
  11. metadata = MetaData()
  12. # Columns to add.
  13. importing_col = Column( "importing", Boolean, index=True, default=False )
  14. ldda_parent_col = Column( "ldda_parent_id", Integer, ForeignKey( "library_dataset_dataset_association.id" ), index=True )
  15. # Table to add.
  16. JobImportHistoryArchive_table = Table( "job_import_history_archive", metadata,
  17. Column( "id", Integer, primary_key=True ),
  18. Column( "job_id", Integer, ForeignKey( "job.id" ), index=True ),
  19. Column( "history_id", Integer, ForeignKey( "history.id" ), index=True ),
  20. Column( "archive_dir", TEXT )
  21. )
  22. def upgrade(migrate_engine):
  23. metadata.bind = migrate_engine
  24. print __doc__
  25. metadata.reflect()
  26. # Add column to history table and initialize.
  27. try:
  28. History_table = Table( "history", metadata, autoload=True )
  29. importing_col.create( History_table, index_name="ix_history_importing")
  30. assert importing_col is History_table.c.importing
  31. # Initialize column to false.
  32. if migrate_engine.name == 'mysql' or migrate_engine.name == 'sqlite':
  33. default_false = "0"
  34. elif migrate_engine.name in ['postgres', 'postgresql']:
  35. default_false = "false"
  36. migrate_engine.execute( "UPDATE history SET importing=%s" % default_false )
  37. except Exception, e:
  38. print str(e)
  39. log.debug( "Adding column 'importing' to history table failed: %s" % str( e ) )
  40. # Create job_import_history_archive table.
  41. try:
  42. JobImportHistoryArchive_table.create()
  43. except Exception, e:
  44. log.debug( "Creating job_import_history_archive table failed: %s" % str( e ) )
  45. def downgrade(migrate_engine):
  46. metadata.bind = migrate_engine
  47. metadata.reflect()
  48. # Drop 'importing' column from history table.
  49. try:
  50. History_table = Table( "history", metadata, autoload=True )
  51. importing_col = History_table.c.importing
  52. importing_col.drop()
  53. except Exception, e:
  54. log.debug( "Dropping column 'importing' from history table failed: %s" % ( str( e ) ) )
  55. # Drop job_import_history_archive table.
  56. try:
  57. JobImportHistoryArchive_table.drop()
  58. except Exception, e:
  59. log.debug( "Dropping job_import_history_archive table failed: %s" % str( e ) )