/lib/galaxy/model/migrate/versions/0057_request_notify.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 68 lines · 67 code · 0 blank · 1 comment · 0 complexity · 7e0e79bb26aa3a5d6869e62e9e262f09 MD5 · raw file

  1. """
  2. Migration script to modify the 'notify' field in the 'request' table from a boolean
  3. to a JSONType
  4. """
  5. from sqlalchemy import *
  6. from sqlalchemy.orm import *
  7. from migrate import *
  8. from migrate.changeset import *
  9. from sqlalchemy.exc import *
  10. from galaxy.model.custom_types import *
  11. from galaxy.util.json import from_json_string, to_json_string
  12. import datetime
  13. now = datetime.datetime.utcnow
  14. import logging
  15. log = logging.getLogger( __name__ )
  16. metadata = MetaData()
  17. def upgrade(migrate_engine):
  18. metadata.bind = migrate_engine
  19. print __doc__
  20. metadata.reflect()
  21. try:
  22. Request_table = Table( "request", metadata, autoload=True )
  23. except NoSuchTableError, e:
  24. Request_table = None
  25. log.debug( "Failed loading table 'request'" )
  26. if Request_table is not None:
  27. # create the column again as JSONType
  28. try:
  29. col = Column( "notification", JSONType() )
  30. col.create( Request_table )
  31. assert col is Request_table.c.notification
  32. except Exception, e:
  33. log.debug( "Creating column 'notification' in the 'request' table failed: %s" % ( str( e ) ) )
  34. cmd = "SELECT id, user_id, notify FROM request"
  35. result = migrate_engine.execute( cmd )
  36. for r in result:
  37. id = int(r[0])
  38. notify_old = r[1]
  39. notify_new = dict(email=[], sample_states=[], body='', subject='')
  40. cmd = "update request set notification='%s' where id=%i" % (to_json_string(notify_new), id)
  41. migrate_engine.execute( cmd )
  42. cmd = "SELECT id, notification FROM request"
  43. result = migrate_engine.execute( cmd )
  44. for r in result:
  45. rr = from_json_string(str(r[1]))
  46. # remove the 'notify' column for non-sqlite databases.
  47. if migrate_engine.name != 'sqlite':
  48. try:
  49. Request_table.c.notify.drop()
  50. except Exception, e:
  51. log.debug( "Deleting column 'notify' from the 'request' table failed: %s" % ( str( e ) ) )
  52. def downgrade(migrate_engine):
  53. metadata.bind = migrate_engine
  54. pass