/lib/galaxy/model/migrate/versions/0012_user_address.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 95 lines · 74 code · 7 blank · 14 comment · 11 complexity · 8e21f9e4f905bcef17e946b414940be1 MD5 · raw file

  1. """
  2. This script adds a new user_address table that is currently only used with sample requests, where
  3. a user can select from a list of his addresses to associate with the request. This script also
  4. drops the request.submitted column which was boolean and replaces it with a request.state column
  5. which is a string, allowing for more flexibility with request states.
  6. """
  7. from sqlalchemy import *
  8. from sqlalchemy.orm import *
  9. from sqlalchemy.exc import *
  10. from galaxy.model.custom_types import *
  11. from migrate import *
  12. from migrate.changeset import *
  13. import datetime
  14. now = datetime.datetime.utcnow
  15. import sys, logging
  16. # Need our custom types, but don't import anything else from model
  17. log = logging.getLogger( __name__ )
  18. log.setLevel(logging.DEBUG)
  19. handler = logging.StreamHandler( sys.stdout )
  20. format = "%(name)s %(levelname)s %(asctime)s %(message)s"
  21. formatter = logging.Formatter( format )
  22. handler.setFormatter( formatter )
  23. log.addHandler( handler )
  24. metadata = MetaData()
  25. def display_migration_details():
  26. print "========================================"
  27. print "This script adds a new user_address table that is currently only used with sample requests, where"
  28. print "a user can select from a list of his addresses to associate with the request. This script also"
  29. print "drops the request.submitted column which was boolean and replaces it with a request.state column"
  30. print "which is a string, allowing for more flexibility with request states."
  31. print "========================================"
  32. UserAddress_table = Table( "user_address", metadata,
  33. Column( "id", Integer, primary_key=True),
  34. Column( "create_time", DateTime, default=now ),
  35. Column( "update_time", DateTime, default=now, onupdate=now ),
  36. Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
  37. Column( "desc", TEXT),
  38. Column( "name", TrimmedString( 255 ), nullable=False),
  39. Column( "institution", TrimmedString( 255 )),
  40. Column( "address", TrimmedString( 255 ), nullable=False),
  41. Column( "city", TrimmedString( 255 ), nullable=False),
  42. Column( "state", TrimmedString( 255 ), nullable=False),
  43. Column( "postal_code", TrimmedString( 255 ), nullable=False),
  44. Column( "country", TrimmedString( 255 ), nullable=False),
  45. Column( "phone", TrimmedString( 255 )),
  46. Column( "deleted", Boolean, index=True, default=False ),
  47. Column( "purged", Boolean, index=True, default=False ) )
  48. def upgrade(migrate_engine):
  49. #raise Exception
  50. metadata.bind = migrate_engine
  51. display_migration_details()
  52. # Load existing tables
  53. metadata.reflect()
  54. # Add all of the new tables above
  55. try:
  56. UserAddress_table.create()
  57. except Exception, e:
  58. log.debug( "Creating user_address table failed: %s" % str( e ) )
  59. # Add 1 column to the request_type table
  60. try:
  61. RequestType_table = Table( "request_type", metadata, autoload=True )
  62. except NoSuchTableError:
  63. RequestType_table = None
  64. log.debug( "Failed loading table request_type" )
  65. if RequestType_table is not None:
  66. try:
  67. col = Column( "deleted", Boolean, index=True, default=False )
  68. col.create( RequestType_table, index_name='ix_request_type_deleted')
  69. assert col is RequestType_table.c.deleted
  70. except Exception, e:
  71. log.debug( "Adding column 'deleted' to request_type table failed: %s" % ( str( e ) ) )
  72. # Delete the submitted column
  73. # This fails for sqlite, so skip the drop -- no conflicts in the future
  74. try:
  75. Request_table = Table( "request", metadata, autoload=True )
  76. except NoSuchTableError:
  77. Request_table = None
  78. log.debug( "Failed loading table request" )
  79. if Request_table is not None:
  80. if migrate_engine.name != 'sqlite':
  81. #DBTODO drop from table doesn't work in sqlite w/ sqlalchemy-migrate .6+
  82. Request_table.c.submitted.drop()
  83. col = Column( "state", TrimmedString( 255 ), index=True )
  84. col.create( Request_table, index_name='ix_request_state')
  85. assert col is Request_table.c.state
  86. def downgrade(migrate_engine):
  87. metadata.bind = migrate_engine
  88. pass