PageRenderTime 32ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0033_published_cols_for_histories_and_workflows.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 105 lines | 74 code | 15 blank | 16 comment | 18 complexity | 78499b7dc1551d404c23d98bcf58aff8 MD5 | raw file
  1. """
  2. Migration script to add necessary columns for distinguishing between viewing/importing and publishing histories, \
  3. workflows, and pages. Script adds published column to histories and workflows and importable column to pages.
  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. def upgrade(migrate_engine):
  13. metadata.bind = migrate_engine
  14. print __doc__
  15. metadata.reflect()
  16. # Create published column in history table.
  17. History_table = Table( "history", metadata, autoload=True )
  18. c = Column( "published", Boolean, index=True )
  19. try:
  20. c.create( History_table, index_name='ix_history_published')
  21. assert c is History_table.c.published
  22. except Exception, e:
  23. print "Adding published column to history table failed: %s" % str( e )
  24. log.debug( "Adding published column to history table failed: %s" % str( e ) )
  25. if migrate_engine.name != 'sqlite':
  26. # Create index for published column in history table.
  27. try:
  28. i = Index( "ix_history_published", History_table.c.published )
  29. i.create()
  30. except:
  31. # Mysql doesn't have a named index, but alter should work
  32. History_table.c.published.alter( unique=False )
  33. # Create published column in stored workflows table.
  34. StoredWorkflow_table = Table( "stored_workflow", metadata, autoload=True )
  35. c = Column( "published", Boolean, index=True )
  36. try:
  37. c.create( StoredWorkflow_table, index_name='ix_stored_workflow_published')
  38. assert c is StoredWorkflow_table.c.published
  39. except Exception, e:
  40. print "Adding published column to stored_workflow table failed: %s" % str( e )
  41. log.debug( "Adding published column to stored_workflow table failed: %s" % str( e ) )
  42. if migrate_engine.name != 'sqlite':
  43. # Create index for published column in stored workflows table.
  44. try:
  45. i = Index( "ix_stored_workflow_published", StoredWorkflow_table.c.published )
  46. i.create()
  47. except:
  48. # Mysql doesn't have a named index, but alter should work
  49. StoredWorkflow_table.c.published.alter( unique=False )
  50. # Create importable column in page table.
  51. Page_table = Table( "page", metadata, autoload=True )
  52. c = Column( "importable", Boolean, index=True )
  53. try:
  54. c.create( Page_table, index_name='ix_page_importable')
  55. assert c is Page_table.c.importable
  56. except Exception, e:
  57. print "Adding importable column to page table failed: %s" % str( e )
  58. log.debug( "Adding importable column to page table failed: %s" % str( e ) )
  59. if migrate_engine.name != 'sqlite':
  60. # Create index for importable column in page table.
  61. try:
  62. i = Index( "ix_page_importable", Page_table.c.importable )
  63. i.create()
  64. except:
  65. # Mysql doesn't have a named index, but alter should work
  66. Page_table.c.importable.alter( unique=False )
  67. def downgrade(migrate_engine):
  68. metadata.bind = migrate_engine
  69. metadata.reflect()
  70. # Drop published column from history table.
  71. History_table = Table( "history", metadata, autoload=True )
  72. try:
  73. History_table.c.published.drop()
  74. except Exception, e:
  75. print "Dropping column published from history table failed: %s" % str( e )
  76. log.debug( "Dropping column published from history table failed: %s" % str( e ) )
  77. # Drop published column from stored_workflow table.
  78. StoredWorkflow_table = Table( "stored_workflow", metadata, autoload=True )
  79. try:
  80. StoredWorkflow_table.c.published.drop()
  81. except Exception, e:
  82. print "Dropping column published from stored_workflow table failed: %s" % str( e )
  83. log.debug( "Dropping column published from stored_workflow table failed: %s" % str( e ) )
  84. # Drop importable column from page table.
  85. Page_table = Table( "page", metadata, autoload=True )
  86. try:
  87. Page_table.c.importable.drop()
  88. except Exception, e:
  89. print "Dropping column importable from page table failed: %s" % str( e )
  90. log.debug( "Dropping column importable from page table failed: %s" % str( e ) )