55import io .ebean .migration .MigrationException ;
66import io .ebean .migration .MigrationResource ;
77
8+ import java .io .IOException ;
89import java .sql .Connection ;
910import java .sql .SQLException ;
1011import java .util .Collections ;
@@ -21,18 +22,20 @@ public class MigrationEngine {
2122 static final System .Logger log = AppLog .getLogger ("io.ebean.migration" );
2223
2324 private final MigrationConfig migrationConfig ;
25+ private final boolean checkStateOnly ;
2426
2527 /**
2628 * Create with the MigrationConfig.
2729 */
28- public MigrationEngine (MigrationConfig migrationConfig ) {
30+ public MigrationEngine (MigrationConfig migrationConfig , boolean checkStateOnly ) {
2931 this .migrationConfig = migrationConfig ;
32+ this .checkStateOnly = checkStateOnly ;
3033 }
3134
3235 /**
3336 * Run the migrations if there are any that need running.
3437 */
35- public List <MigrationResource > run (Connection connection , boolean checkStateOnly ) {
38+ public List <MigrationResource > run (Connection connection ) {
3639 try {
3740 LocalMigrationResources resources = new LocalMigrationResources (migrationConfig );
3841 if (!resources .readResources () && !resources .readInitResources ()) {
@@ -42,18 +45,13 @@ public List<MigrationResource> run(Connection connection, boolean checkStateOnly
4245
4346 long startMs = System .currentTimeMillis ();
4447 connection .setAutoCommit (false );
45- MigrationPlatform platform = derivePlatformName (migrationConfig , connection );
46- new MigrationSchema (migrationConfig , connection ).createAndSetIfNeeded ();
47-
48- MigrationTable table = new MigrationTable (migrationConfig , connection , checkStateOnly , platform );
49- table .createIfNeededAndLock ();
48+ MigrationTable table = initialiseMigrationTable (connection );
5049 try {
51- List <LocalMigrationResource > migrations = resources .versions ();
52- List <MigrationResource > result = runMigrations (migrations , table , checkStateOnly );
50+ List <MigrationResource > result = runMigrations (resources .versions (), table , checkStateOnly );
5351 connection .commit ();
5452 if (!checkStateOnly ) {
5553 long commitMs = System .currentTimeMillis ();
56- log .log (INFO , "DB migrations completed in {0}ms - executed:{1} totalMigrations:{2}" , (commitMs - startMs ), table .count (), migrations .size ());
54+ log .log (INFO , "DB migrations completed in {0}ms - executed:{1} totalMigrations:{2}" , (commitMs - startMs ), table .count (), table .size ());
5755 int countNonTransactional = table .runNonTransactional ();
5856 if (countNonTransactional > 0 ) {
5957 log .log (INFO , "Non-transactional DB migrations completed in {0}ms - executed:{1}" , (System .currentTimeMillis () - commitMs ), countNonTransactional );
@@ -70,13 +68,22 @@ public List<MigrationResource> run(Connection connection, boolean checkStateOnly
7068
7169 } catch (Exception e ) {
7270 rollback (connection );
73- throw new RuntimeException ( e );
71+ throw new MigrationException ( "Error running DB migrations" , e );
7472
7573 } finally {
7674 close (connection );
7775 }
7876 }
7977
78+ private MigrationTable initialiseMigrationTable (Connection connection ) throws SQLException , IOException {
79+ MigrationPlatform platform = derivePlatformName (migrationConfig , connection );
80+ new MigrationSchema (migrationConfig , connection ).createAndSetIfNeeded ();
81+
82+ final MigrationTable table = new MigrationTable (migrationConfig , connection , checkStateOnly , platform );
83+ table .createIfNeededAndLock ();
84+ return table ;
85+ }
86+
8087 /**
8188 * Run all the migrations as needed.
8289 */
0 commit comments