Skip to content

Commit e7cfaee

Browse files
committed
Make runner package internal
- Do not export runner package - Move logic in MigrationRunner into MigrationEngine - Make most types in runner package final and package protected
1 parent da4d2ea commit e7cfaee

17 files changed

+207
-174
lines changed
Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.ebean.migration;
22

33
import io.avaje.applog.AppLog;
4-
import io.ebean.migration.runner.*;
4+
import io.ebean.migration.runner.MigrationEngine;
55

66
import javax.sql.DataSource;
77
import java.sql.Connection;
@@ -19,8 +19,6 @@ public class MigrationRunner {
1919

2020
protected final MigrationConfig migrationConfig;
2121

22-
protected List<MigrationResource> checkMigrations;
23-
2422
public MigrationRunner(MigrationConfig migrationConfig) {
2523
this.migrationConfig = migrationConfig;
2624
}
@@ -43,8 +41,7 @@ public List<MigrationResource> checkState(DataSource dataSource) {
4341
* Return the migrations that would be applied if the migration is run.
4442
*/
4543
public List<MigrationResource> checkState(Connection connection) {
46-
run(connection, true);
47-
return checkMigrations;
44+
return run(connection, true);
4845
}
4946

5047
/**
@@ -85,118 +82,8 @@ private Connection getConnection(DataSource dataSource) {
8582
/**
8683
* Run the migrations if there are any that need running.
8784
*/
88-
protected void run(Connection connection, boolean checkStateOnly) {
89-
try {
90-
LocalMigrationResources resources = new LocalMigrationResources(migrationConfig);
91-
if (!resources.readResources() && !resources.readInitResources()) {
92-
log.log(DEBUG, "no migrations to check");
93-
return;
94-
}
95-
96-
long startMs = System.currentTimeMillis();
97-
connection.setAutoCommit(false);
98-
MigrationPlatform platform = derivePlatformName(migrationConfig, connection);
99-
new MigrationSchema(migrationConfig, connection).createAndSetIfNeeded();
100-
101-
MigrationTable table = new MigrationTable(migrationConfig, connection, checkStateOnly, platform);
102-
table.createIfNeededAndLock();
103-
try {
104-
List<LocalMigrationResource> migrations = resources.versions();
105-
runMigrations(migrations, table, checkStateOnly);
106-
connection.commit();
107-
if (!checkStateOnly) {
108-
long commitMs = System.currentTimeMillis();
109-
log.log(INFO, "DB migrations completed in {0}ms - executed:{1} totalMigrations:{2}", (commitMs - startMs), table.count(), migrations.size());
110-
int countNonTransactional = table.runNonTransactional();
111-
if (countNonTransactional > 0) {
112-
log.log(INFO, "Non-transactional DB migrations completed in {0}ms - executed:{1}", (System.currentTimeMillis() - commitMs), countNonTransactional);
113-
}
114-
}
115-
} finally {
116-
table.unlockMigrationTable();
117-
}
118-
119-
} catch (MigrationException e) {
120-
rollback(connection);
121-
throw e;
122-
123-
} catch (Exception e) {
124-
rollback(connection);
125-
throw new RuntimeException(e);
126-
127-
} finally {
128-
close(connection);
129-
}
130-
}
131-
132-
/**
133-
* Run all the migrations as needed.
134-
*/
135-
private void runMigrations(List<LocalMigrationResource> localVersions, MigrationTable table, boolean checkStateMode) throws SQLException {
136-
// get the migrations in version order
137-
if (table.isEmpty()) {
138-
LocalMigrationResource initVersion = getInitVersion();
139-
if (initVersion != null) {
140-
// run using a dbinit script
141-
log.log(INFO, "dbinit migration version:{0} local migrations:{1} checkState:{2}", initVersion, localVersions.size(), checkStateMode);
142-
checkMigrations = table.runInit(initVersion, localVersions);
143-
return;
144-
}
145-
}
146-
checkMigrations = table.runAll(localVersions);
147-
}
148-
149-
/**
150-
* Return the last init migration.
151-
*/
152-
private LocalMigrationResource getInitVersion() {
153-
LocalMigrationResources initResources = new LocalMigrationResources(migrationConfig);
154-
if (initResources.readInitResources()) {
155-
List<LocalMigrationResource> initVersions = initResources.versions();
156-
if (!initVersions.isEmpty()) {
157-
return initVersions.get(initVersions.size() - 1);
158-
}
159-
}
160-
return null;
85+
private List<MigrationResource> run(Connection connection, boolean checkStateOnly) {
86+
return new MigrationEngine(migrationConfig).run(connection, checkStateOnly);
16187
}
16288

163-
/**
164-
* Return the platform deriving from connection if required.
165-
*/
166-
private MigrationPlatform derivePlatformName(MigrationConfig migrationConfig, Connection connection) {
167-
final String platform = migrationConfig.getPlatform();
168-
if (platform != null) {
169-
return DbNameUtil.platform(platform);
170-
}
171-
// determine the platform from the db connection
172-
String derivedPlatformName = DbNameUtil.normalise(connection);
173-
migrationConfig.setPlatform(derivedPlatformName);
174-
return DbNameUtil.platform(derivedPlatformName);
175-
}
176-
177-
/**
178-
* Close the connection logging if an error occurs.
179-
*/
180-
private void close(Connection connection) {
181-
try {
182-
if (connection != null) {
183-
connection.close();
184-
}
185-
} catch (SQLException e) {
186-
log.log(WARNING, "Error closing connection", e);
187-
}
188-
}
189-
190-
/**
191-
* Rollback the connection logging if an error occurs.
192-
*/
193-
private void rollback(Connection connection) {
194-
try {
195-
if (connection != null) {
196-
connection.rollback();
197-
}
198-
} catch (SQLException e) {
199-
log.log(WARNING, "Error on connection rollback", e);
200-
}
201-
}
20289
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Calculates the checksum for the given string content.
1111
*/
12-
class Checksum {
12+
final class Checksum {
1313

1414
/**
1515
* Returns the checksum of the string content.

ebean-migration/src/main/java/io/ebean/migration/DbNameUtil.java renamed to ebean-migration/src/main/java/io/ebean/migration/runner/DbNameUtil.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
package io.ebean.migration;
2-
3-
import io.ebean.migration.runner.MigrationPlatform;
1+
package io.ebean.migration.runner;
42

53
import java.sql.*;
64

@@ -61,7 +59,7 @@ private static String readPostgres(Connection connection) {
6159
}
6260
}
6361
} catch (SQLException e) {
64-
MigrationRunner.log.log(WARNING, "Error running detection query on Postgres", e);
62+
MigrationEngine.log.log(WARNING, "Error running detection query on Postgres", e);
6563
}
6664
return POSTGRES;
6765
}

ebean-migration/src/main/java/io/ebean/migration/DbPlatformNames.java renamed to 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
@@ -1,4 +1,4 @@
1-
package io.ebean.migration;
1+
package io.ebean.migration.runner;
22

33
/**
44
* Known database platform names.

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* A DB migration resource (DDL or Jdbc)
88
*/
9-
public abstract class LocalMigrationResource implements MigrationResource {
9+
abstract class LocalMigrationResource implements MigrationResource {
1010

1111
protected final MigrationVersion version;
1212

@@ -17,34 +17,35 @@ public abstract class LocalMigrationResource implements MigrationResource {
1717
/**
1818
* Construct with version and resource.
1919
*/
20-
public LocalMigrationResource(MigrationVersion version, String location) {
20+
LocalMigrationResource(MigrationVersion version, String location) {
2121
this.version = version;
2222
this.location = location;
2323
this.type = version.type();
2424
}
2525

26+
@Override
2627
public String toString() {
2728
return version.toString();
2829
}
2930

3031
/**
3132
* Return true if the underlying version is "repeatable".
3233
*/
33-
public boolean isRepeatable() {
34+
boolean isRepeatable() {
3435
return version.isRepeatable();
3536
}
3637

3738
/**
3839
* Return true if the underlying version is "repeatable init".
3940
*/
40-
public boolean isRepeatableInit() {
41+
boolean isRepeatableInit() {
4142
return version.isRepeatableInit();
4243
}
4344

4445
/**
4546
* Return true if the underlying version is "repeatable last".
4647
*/
47-
public boolean isRepeatableLast() {
48+
boolean isRepeatableLast() {
4849
return version.isRepeatableLast();
4950
}
5051

@@ -87,7 +88,7 @@ public String type() {
8788
/**
8889
* Set the migration to be an Init migration.
8990
*/
90-
public void setInitType() {
91+
void setInitType() {
9192
this.type = MigrationVersion.BOOTINIT_TYPE;
9293
}
9394
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* Loads the DB migration resources and sorts them into execution order.
1818
*/
19-
public final class LocalMigrationResources {
19+
final class LocalMigrationResources {
2020

2121
private static final System.Logger log = MigrationSchema.log;
2222

@@ -28,7 +28,7 @@ public final class LocalMigrationResources {
2828
/**
2929
* Construct with configuration options.
3030
*/
31-
public LocalMigrationResources(MigrationConfig migrationConfig) {
31+
LocalMigrationResources(MigrationConfig migrationConfig) {
3232
this.migrationConfig = migrationConfig;
3333
this.classLoader = migrationConfig.getClassLoader();
3434
this.searchForJdbcMigrations = migrationConfig.getJdbcMigrationFactory() != null;
@@ -37,14 +37,14 @@ public LocalMigrationResources(MigrationConfig migrationConfig) {
3737
/**
3838
* Read the init migration resources (usually only 1) returning true if there are versions.
3939
*/
40-
public boolean readInitResources() {
40+
boolean readInitResources() {
4141
return readResourcesForPath(migrationConfig.getMigrationInitPath());
4242
}
4343

4444
/**
4545
* Read all the migration resources (SQL scripts) returning true if there are versions.
4646
*/
47-
public boolean readResources() {
47+
boolean readResources() {
4848
return readResourcesForPath(migrationConfig.getMigrationPath());
4949
}
5050

@@ -134,14 +134,14 @@ private LocalMigrationResource createScriptMigration(Resource resource, String f
134134
/**
135135
* Return the list of migration resources in version order.
136136
*/
137-
public List<LocalMigrationResource> versions() {
137+
List<LocalMigrationResource> versions() {
138138
return versions;
139139
}
140140

141141
/**
142142
* Filter used to find the migration scripts.
143143
*/
144-
private static class Match implements Predicate<String> {
144+
private static final class Match implements Predicate<String> {
145145

146146
private final boolean searchJdbc;
147147

@@ -158,7 +158,7 @@ public boolean test(String name) {
158158
/**
159159
* Filter to find JDBC migrations only.
160160
*/
161-
private static class JdbcOnly implements Predicate<String> {
161+
private static final class JdbcOnly implements Predicate<String> {
162162
@Override
163163
public boolean test(String name) {
164164
return name.endsWith(".class") && !name.contains("$");

0 commit comments

Comments
 (0)