public void migrate ( Log log , String encoding ) { createSchemaVersionTable ( ) ; final Collection < Migration > pendingMigrations = getPendingMigrations ( ) ; if ( pendingMigrations . isEmpty ( ) ) { log . info ( "No new migrations are found" ) ; return ; } log . info ( "Migrating database, applying " + pendingMigrations . size ( ) + " migration(s)" ) ; Migration currentMigration = null ; try { connection ( ) . setAutoCommit ( false ) ; for ( Migration migration : pendingMigrations ) { currentMigration = migration ; log . info ( "Running migration " + currentMigration . getName ( ) ) ; long start = System . currentTimeMillis ( ) ; currentMigration . migrate ( encoding ) ; versionStrategy . recordMigration ( currentMigration . getVersion ( ) , new Date ( start ) , ( start - System . currentTimeMillis ( ) ) ) ; connection ( ) . commit ( ) ; } } catch ( Exception e ) { try { connection ( ) . rollback ( ) ; } catch ( Exception ex ) { throw new MigrationException ( e ) ; } assert currentMigration != null ; throw new MigrationException ( "Migration for version " + currentMigration . getVersion ( ) + " failed, rolling back and terminating migration." , e ) ; } log . info ( "Migrated database" ) ; }
Migrates the database to the latest version enabling migrations if necessary .