Skip to content

Commit c2c4f9c

Browse files
committed
Refactor internals of MigrationEngine
1 parent e7cfaee commit c2c4f9c

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

ebean-migration/src/main/java/io/ebean/migration/MigrationRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private Connection getConnection(DataSource dataSource) {
8383
* Run the migrations if there are any that need running.
8484
*/
8585
private List<MigrationResource> run(Connection connection, boolean checkStateOnly) {
86-
return new MigrationEngine(migrationConfig).run(connection, checkStateOnly);
86+
return new MigrationEngine(migrationConfig, checkStateOnly).run(connection);
8787
}
8888

8989
}

ebean-migration/src/main/java/io/ebean/migration/runner/DbPlatformNames.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Known database platform names.
55
*/
6-
public interface DbPlatformNames {
6+
interface DbPlatformNames {
77

88
String SQLSERVER = "sqlserver";
99
String SQLITE = "sqlite";

ebean-migration/src/main/java/io/ebean/migration/runner/MigrationEngine.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.ebean.migration.MigrationException;
66
import io.ebean.migration.MigrationResource;
77

8+
import java.io.IOException;
89
import java.sql.Connection;
910
import java.sql.SQLException;
1011
import 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

Comments
 (0)