/lib/galaxy/model/migrate/versions/0004_indexes_and_defaults.py

https://bitbucket.org/ialbert/galaxy-genetrack · Python · 61 lines · 53 code · 4 blank · 4 comment · 14 complexity · 8e5d648ef0e89d090147a78986ce7b11 MD5 · raw file

  1. from sqlalchemy import *
  2. from sqlalchemy.orm import *
  3. from migrate import *
  4. import sys, logging
  5. log = logging.getLogger( __name__ )
  6. log.setLevel(logging.DEBUG)
  7. handler = logging.StreamHandler( sys.stdout )
  8. format = "%(name)s %(levelname)s %(asctime)s %(message)s"
  9. formatter = logging.Formatter( format )
  10. handler.setFormatter( formatter )
  11. log.addHandler( handler )
  12. metadata = MetaData( migrate_engine )
  13. db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, transactional=False ) )
  14. User_table = Table( "galaxy_user", metadata, autoload=True )
  15. HistoryDatasetAssociation_table = Table( "history_dataset_association", metadata, autoload=True )
  16. def boolean_false():
  17. if migrate_engine.name == 'postgres' or migrate_engine.name == 'mysql':
  18. return False
  19. elif migrate_engine.name == 'sqlite':
  20. return 0
  21. else:
  22. raise Exception( 'Unable to convert data for unknown database type: %s' % db )
  23. def upgrade():
  24. # Load existing tables
  25. metadata.reflect()
  26. # Add 2 indexes to the galaxy_user table
  27. i = Index( 'ix_galaxy_user_deleted', User_table.c.deleted )
  28. try:
  29. i.create()
  30. except Exception, e:
  31. log.debug( "Adding index 'ix_galaxy_user_deleted' to galaxy_user table failed: %s" % ( str( e ) ) )
  32. i = Index( 'ix_galaxy_user_purged', User_table.c.purged )
  33. try:
  34. i.create()
  35. except Exception, e:
  36. log.debug( "Adding index 'ix_galaxy_user_purged' to galaxy_user table failed: %s" % ( str( e ) ) )
  37. # Set the default data in the galaxy_user table, but only for null values
  38. cmd = "UPDATE galaxy_user SET deleted = %s WHERE deleted is null"
  39. cmd = cmd % boolean_false()
  40. try:
  41. db_session.execute( cmd )
  42. except Exception, e:
  43. log.debug( "Setting default data for galaxy_user.deleted column failed: %s" % ( str( e ) ) )
  44. cmd = "UPDATE galaxy_user SET purged = %s WHERE purged is null"
  45. cmd = cmd % boolean_false()
  46. try:
  47. db_session.execute( cmd )
  48. except Exception, e:
  49. log.debug( "Setting default data for galaxy_user.purged column failed: %s" % ( str( e ) ) )
  50. # Add 1 index to the history_dataset_association table
  51. i = Index( 'ix_hda_copied_from_library_dataset_dataset_association_id', HistoryDatasetAssociation_table.c.copied_from_library_dataset_dataset_association_id )
  52. try:
  53. i.create()
  54. except Exception, e:
  55. log.debug( "Adding index 'ix_hda_copied_from_library_dataset_dataset_association_id' to history_dataset_association table failed: %s" % ( str( e ) ) )
  56. def downgrade():
  57. pass