/lib/galaxy/model/migrate/check.py

https://bitbucket.org/ialbert/galaxy-genetrack · Python · 112 lines · 68 code · 9 blank · 35 comment · 16 complexity · 9f714557cb76322abdbe2b82b66802ad MD5 · raw file

  1. import os.path, logging
  2. from galaxy import eggs
  3. import pkg_resources
  4. pkg_resources.require( "sqlalchemy-migrate" )
  5. from migrate.versioning import repository, schema
  6. from sqlalchemy import *
  7. from sqlalchemy.exceptions import NoSuchTableError
  8. log = logging.getLogger( __name__ )
  9. # path relative to galaxy
  10. migrate_repository_directory = os.path.dirname( __file__ ).replace( os.getcwd() + os.path.sep, '', 1 )
  11. migrate_repository = repository.Repository( migrate_repository_directory )
  12. dialect_to_egg = {
  13. "sqlite" : "pysqlite>=2",
  14. "postgres" : "psycopg2",
  15. "mysql" : "MySQL_python"
  16. }
  17. def create_or_verify_database( url, engine_options={} ):
  18. """
  19. Check that the database is use-able, possibly creating it if empty (this is
  20. the only time we automatically create tables, otherwise we force the
  21. user to do it using the management script so they can create backups).
  22. 1) Empty database --> initialize with latest version and return
  23. 2) Database older than migration support --> fail and require manual update
  24. 3) Database at state where migrate support introduced --> add version control information but make no changes (might still require manual update)
  25. 4) Database versioned but out of date --> fail with informative message, user must run "sh manage_db.sh upgrade"
  26. """
  27. dialect = ( url.split( ':', 1 ) )[0]
  28. try:
  29. egg = dialect_to_egg[dialect]
  30. try:
  31. pkg_resources.require( egg )
  32. log.debug( "%s egg successfully loaded for %s dialect" % ( egg, dialect ) )
  33. except:
  34. # If the module is in the path elsewhere (i.e. non-egg), it'll still load.
  35. log.warning( "%s egg not found, but an attempt will be made to use %s anyway" % ( egg, dialect ) )
  36. except KeyError:
  37. # Let this go, it could possibly work with db's we don't support
  38. log.error( "database_connection contains an unknown SQLAlchemy database dialect: %s" % dialect )
  39. # Create engine and metadata
  40. engine = create_engine( url, **engine_options )
  41. meta = MetaData( bind=engine )
  42. # Try to load dataset table
  43. try:
  44. dataset_table = Table( "dataset", meta, autoload=True )
  45. except NoSuchTableError:
  46. # No 'dataset' table means a completely uninitialized database, which
  47. # is fine, init the database in a versioned state
  48. log.info( "No database, initializing" )
  49. # Database might or might not be versioned
  50. try:
  51. # Declare the database to be under a repository's version control
  52. db_schema = schema.ControlledSchema.create( engine, migrate_repository )
  53. except:
  54. # The database is already under version control
  55. db_schema = schema.ControlledSchema( engine, migrate_repository )
  56. # Apply all scripts to get to current version
  57. migrate_to_current_version( engine, db_schema )
  58. return
  59. try:
  60. hda_table = Table( "history_dataset_association", meta, autoload=True )
  61. except NoSuchTableError:
  62. raise Exception( "Your database is older than hg revision 1464:c7acaa1bb88f and will need to be updated manually" )
  63. # There is a 'history_dataset_association' table, so we (hopefully) have
  64. # version 1 of the database, but without the migrate_version table. This
  65. # happens if the user has a build from right before migration was added.
  66. # Verify that this is true, if it is any older they'll have to update
  67. # manually
  68. if 'copied_from_history_dataset_association_id' not in hda_table.c:
  69. # The 'copied_from_history_dataset_association_id' column was added in
  70. # rev 1464:c7acaa1bb88f. This is the oldest revision we currently do
  71. # automated versioning for, so stop here
  72. raise Exception( "Your database is older than hg revision 1464:c7acaa1bb88f and will need to be updated manually" )
  73. # At revision 1464:c7acaa1bb88f or greater (database version 1), make sure
  74. # that the db has version information. This is the trickiest case -- we
  75. # have a database but no version control, and are assuming it is a certain
  76. # version. If the user has postion version 1 changes this could cause
  77. # problems
  78. try:
  79. version_table = Table( "migrate_version", meta, autoload=True )
  80. except NoSuchTableError:
  81. # The database exists but is not yet under migrate version control, so init with version 1
  82. log.info( "Adding version control to existing database" )
  83. try:
  84. metadata_file_table = Table( "metadata_file", meta, autoload=True )
  85. schema.ControlledSchema.create( engine, migrate_repository, version=2 )
  86. except NoSuchTableError:
  87. schema.ControlledSchema.create( engine, migrate_repository, version=1 )
  88. # Verify that the code and the DB are in sync
  89. db_schema = schema.ControlledSchema( engine, migrate_repository )
  90. if migrate_repository.versions.latest != db_schema.version:
  91. raise Exception( "Your database has version '%d' but this code expects version '%d'. Please backup your database and then migrate the schema by running 'sh manage_db.sh upgrade'."
  92. % ( db_schema.version, migrate_repository.versions.latest ) )
  93. else:
  94. log.info( "At database version %d" % db_schema.version )
  95. def migrate_to_current_version( engine, schema ):
  96. # Changes to get to current version
  97. changeset = schema.changeset( None )
  98. for ver, change in changeset:
  99. nextver = ver + changeset.step
  100. log.info( 'Migrating %s -> %s... ' % ( ver, nextver ) )
  101. schema.runchange( ver, change, changeset.step )