/lib/galaxy/webapps/community/model/migrate/versions/0007_add_long_description_times_downloaded_columns.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 66 lines · 50 code · 8 blank · 8 comment · 8 complexity · 1dbb4197e8b926ea5bd2ffa773d0bbc7 MD5 · raw file

  1. """
  2. Migration script to add the long_description and times_downloaded columns to the repository table.
  3. """
  4. from sqlalchemy import *
  5. from sqlalchemy.orm import *
  6. from migrate import *
  7. from migrate.changeset import *
  8. # Need our custom types, but don't import anything else from model
  9. from galaxy.model.custom_types import *
  10. import sys, logging
  11. log = logging.getLogger( __name__ )
  12. log.setLevel(logging.DEBUG)
  13. handler = logging.StreamHandler( sys.stdout )
  14. format = "%(name)s %(levelname)s %(asctime)s %(message)s"
  15. formatter = logging.Formatter( format )
  16. handler.setFormatter( formatter )
  17. log.addHandler( handler )
  18. metadata = MetaData( migrate_engine )
  19. db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
  20. def upgrade():
  21. print __doc__
  22. metadata.reflect()
  23. # Create and initialize imported column in job table.
  24. Repository_table = Table( "repository", metadata, autoload=True )
  25. c = Column( "long_description" , TEXT )
  26. try:
  27. # Create
  28. c.create( Repository_table )
  29. assert c is Repository_table.c.long_description
  30. except Exception, e:
  31. print "Adding long_description column to the repository table failed: %s" % str( e )
  32. log.debug( "Adding long_description column to the repository table failed: %s" % str( e ) )
  33. c = Column( "times_downloaded" , Integer )
  34. try:
  35. # Create
  36. c.create( Repository_table )
  37. assert c is Repository_table.c.times_downloaded
  38. except Exception, e:
  39. print "Adding times_downloaded column to the repository table failed: %s" % str( e )
  40. log.debug( "Adding times_downloaded column to the repository table failed: %s" % str( e ) )
  41. cmd = "UPDATE repository SET long_description = ''"
  42. db_session.execute( cmd )
  43. cmd = "UPDATE repository SET times_downloaded = 0"
  44. db_session.execute( cmd )
  45. def downgrade():
  46. metadata.reflect()
  47. # Drop email_alerts column from repository table.
  48. Repository_table = Table( "repository", metadata, autoload=True )
  49. try:
  50. Repository_table.c.long_description.drop()
  51. except Exception, e:
  52. print "Dropping column long_description from the repository table failed: %s" % str( e )
  53. log.debug( "Dropping column long_description from the repository table failed: %s" % str( e ) )
  54. try:
  55. Repository_table.c.times_downloaded.drop()
  56. except Exception, e:
  57. print "Dropping column times_downloaded from the repository table failed: %s" % str( e )
  58. log.debug( "Dropping column times_downloaded from the repository table failed: %s" % str( e ) )