Skip to content

Commit f11ffc5

Browse files
committed
Refactor extract MigrationResource interface (that is now returned by checkState() methods)
This is effectively a breaking API change for code using the checkState() methods, these now return the MigrationResource interface rather than the abstract LocalMigrationResource (which we want to be internal)
1 parent 0f59839 commit f11ffc5

File tree

7 files changed

+127
-64
lines changed

7 files changed

+127
-64
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.ebean.migration;
2+
3+
/**
4+
* A DB migration resource - typically a resource containing SQL.
5+
*/
6+
public interface MigrationResource extends Comparable<MigrationResource> {
7+
8+
/**
9+
* Return the "key" that identifies the migration.
10+
*/
11+
String key();
12+
13+
/**
14+
* Return the migration comment.
15+
*/
16+
String comment();
17+
18+
/**
19+
* Deprecated migrate to comment().
20+
*/
21+
@Deprecated
22+
default String getComment() {
23+
return comment();
24+
}
25+
26+
/**
27+
* Return the content of the migration.
28+
*/
29+
String content();
30+
31+
/**
32+
* Deprecated migrate to content().
33+
*/
34+
@Deprecated
35+
default String getContent() {
36+
return content();
37+
}
38+
39+
/**
40+
* Default ordering by version.
41+
*/
42+
@Override
43+
int compareTo(MigrationResource other);
44+
45+
/**
46+
* Return the underlying migration version.
47+
*/
48+
MigrationVersion version();
49+
50+
/**
51+
* Deprecated migrate to version().
52+
*/
53+
@Deprecated
54+
default MigrationVersion getVersion() {
55+
return version();
56+
}
57+
58+
/**
59+
* Return the resource location.
60+
*/
61+
String location();
62+
63+
/**
64+
* Deprecated migrate to location().
65+
*/
66+
@Deprecated
67+
default String getLocation() {
68+
return location();
69+
}
70+
71+
/**
72+
* Return the type code ("R" or "V") for this migration.
73+
*/
74+
String type();
75+
76+
/**
77+
* Deprecated migrate to type().
78+
*/
79+
@Deprecated
80+
default String getType() {
81+
return type();
82+
}
83+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MigrationRunner {
1919

2020
protected final MigrationConfig migrationConfig;
2121

22-
protected List<LocalMigrationResource> checkMigrations;
22+
protected List<MigrationResource> checkMigrations;
2323

2424
public MigrationRunner(MigrationConfig migrationConfig) {
2525
this.migrationConfig = migrationConfig;
@@ -28,21 +28,21 @@ public MigrationRunner(MigrationConfig migrationConfig) {
2828
/**
2929
* Return the migrations that would be applied if the migration is run.
3030
*/
31-
public List<LocalMigrationResource> checkState() {
31+
public List<MigrationResource> checkState() {
3232
return checkState(migrationConfig.createConnection());
3333
}
3434

3535
/**
3636
* Return the migrations that would be applied if the migration is run.
3737
*/
38-
public List<LocalMigrationResource> checkState(DataSource dataSource) {
38+
public List<MigrationResource> checkState(DataSource dataSource) {
3939
return checkState(getConnection(dataSource));
4040
}
4141

4242
/**
4343
* Return the migrations that would be applied if the migration is run.
4444
*/
45-
public List<LocalMigrationResource> checkState(Connection connection) {
45+
public List<MigrationResource> checkState(Connection connection) {
4646
run(connection, true);
4747
return checkMigrations;
4848
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public LocalDdlMigrationResource(MigrationVersion version, String location, Reso
2424
/**
2525
* Return the content for the migration apply ddl script.
2626
*/
27-
public String getContent() {
27+
public String content() {
2828
try {
2929
return resource.loadAsString(StandardCharsets.UTF_8);
3030
} catch (NullPointerException e) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public int getChecksum() {
4040
}
4141

4242
@Override
43-
public String getContent() {
43+
public String content() {
4444
return "location:" + location;
4545
}
4646
}

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

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.ebean.migration.runner;
22

3+
import io.ebean.migration.MigrationResource;
34
import io.ebean.migration.MigrationVersion;
45

56
/**
67
* A DB migration resource (DDL or Jdbc)
78
*/
8-
public abstract class LocalMigrationResource implements Comparable<LocalMigrationResource> {
9+
public abstract class LocalMigrationResource implements MigrationResource {
910

1011
protected final MigrationVersion version;
1112

@@ -47,9 +48,8 @@ public boolean isRepeatableLast() {
4748
return version.isRepeatableLast();
4849
}
4950

50-
/**
51-
* Return the "key" that identifies the migration.
52-
*/
51+
52+
@Override
5353
public String key() {
5454
if (isRepeatable()) {
5555
return version.comment().toLowerCase();
@@ -58,45 +58,29 @@ public String key() {
5858
}
5959
}
6060

61-
/**
62-
* Return the migration comment.
63-
*/
64-
public String getComment() {
61+
@Override
62+
public String comment() {
6563
String comment = version.comment();
6664
return (comment == null || comment.isEmpty()) ? "-" : comment;
6765
}
6866

69-
/**
70-
* Default ordering by version.
71-
*/
7267
@Override
73-
public int compareTo(LocalMigrationResource o) {
74-
return version.compareTo(o.version);
68+
public int compareTo(MigrationResource other) {
69+
return version.compareTo(other.version());
7570
}
7671

77-
/**
78-
* Return the underlying migration version.
79-
*/
80-
public MigrationVersion getVersion() {
72+
@Override
73+
public MigrationVersion version() {
8174
return version;
8275
}
8376

84-
/**
85-
* Return the resource location.
86-
*/
87-
public String getLocation() {
77+
@Override
78+
public String location() {
8879
return location;
8980
}
9081

91-
/**
92-
* Return the content of the migration.
93-
*/
94-
public abstract String getContent();
95-
96-
/**
97-
* Return the type code ("R" or "V") for this migration.
98-
*/
99-
public String getType() {
82+
@Override
83+
public String type() {
10084
return type;
10185
}
10286

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import io.avaje.applog.AppLog;
44
import io.ebean.ddlrunner.ScriptTransform;
5-
import io.ebean.migration.JdbcMigration;
6-
import io.ebean.migration.MigrationConfig;
7-
import io.ebean.migration.MigrationException;
8-
import io.ebean.migration.MigrationVersion;
5+
import io.ebean.migration.*;
96

107
import java.io.IOException;
118
import java.net.URL;
@@ -60,7 +57,7 @@ public final class MigrationTable {
6057
private MigrationMetaRow lastMigration;
6158
private LocalMigrationResource priorVersion;
6259

63-
private final List<LocalMigrationResource> checkMigrations = new ArrayList<>();
60+
private final List<MigrationResource> checkMigrations = new ArrayList<>();
6461

6562
/**
6663
* Version of a dbinit script. When set this means all migration version less than this are ignored.
@@ -274,7 +271,7 @@ private String trim(String s) {
274271
private boolean shouldRun(LocalMigrationResource localVersion, LocalMigrationResource prior) throws SQLException {
275272
if (prior != null && !localVersion.isRepeatable()) {
276273
if (!migrationExists(prior)) {
277-
log.log(ERROR, "Migration {0} requires prior migration {1} which has not been run", localVersion.getVersion(), prior.getVersion());
274+
log.log(ERROR, "Migration {0} requires prior migration {1} which has not been run", localVersion.version(), prior.version());
278275
return false;
279276
}
280277
}
@@ -300,7 +297,7 @@ private boolean runMigration(LocalMigrationResource local, MigrationMetaRow exis
300297
String script = null;
301298
int checksum;
302299
if (local instanceof LocalDdlMigrationResource) {
303-
script = convertScript(local.getContent());
300+
script = convertScript(local.content());
304301
checksum = Checksum.calculate(script);
305302
} else {
306303
checksum = ((LocalJdbcMigrationResource) local).getChecksum();
@@ -321,7 +318,7 @@ private boolean runMigration(LocalMigrationResource local, MigrationMetaRow exis
321318
*/
322319
private boolean patchInsertMigration(LocalMigrationResource local, int checksum) throws SQLException {
323320
if (patchInsertVersions != null && patchInsertVersions.contains(local.key())) {
324-
log.log(INFO, "Patch migration, insert into history {0}", local.getLocation());
321+
log.log(INFO, "Patch migration, insert into history {0}", local.location());
325322
if (!checkStateOnly) {
326323
insertIntoHistory(local, checksum, 0);
327324
}
@@ -336,18 +333,18 @@ private boolean patchInsertMigration(LocalMigrationResource local, int checksum)
336333
boolean skipMigration(int checksum, LocalMigrationResource local, MigrationMetaRow existing) throws SQLException {
337334
boolean matchChecksum = (existing.getChecksum() == checksum);
338335
if (matchChecksum) {
339-
log.log(TRACE, "skip unchanged migration {0}", local.getLocation());
336+
log.log(TRACE, "skip unchanged migration {0}", local.location());
340337
return true;
341338

342339
} else if (patchResetChecksum(existing, checksum)) {
343-
log.log(INFO, "Patch migration, reset checksum on {0}", local.getLocation());
340+
log.log(INFO, "Patch migration, reset checksum on {0}", local.location());
344341
return true;
345342

346343
} else if (local.isRepeatable() || skipChecksum) {
347344
// re-run the migration
348345
return false;
349346
} else {
350-
throw new MigrationException("Checksum mismatch on migration " + local.getLocation());
347+
throw new MigrationException("Checksum mismatch on migration " + local.location());
351348
}
352349
}
353350

@@ -383,7 +380,7 @@ private void executeMigration(LocalMigrationResource local, String script, int c
383380
long exeMillis = 0;
384381
try {
385382
if (skipMigrationRun) {
386-
log.log(DEBUG, "skip migration {0}", local.getLocation());
383+
log.log(DEBUG, "skip migration {0}", local.location());
387384
} else {
388385
exeMillis = executeMigration(local, script);
389386
}
@@ -396,7 +393,7 @@ private void executeMigration(LocalMigrationResource local, String script, int c
396393
} catch (SQLException e) {
397394
if (allowErrorInRepeatable && local.isRepeatableLast()) {
398395
// log the exception and continue on repeatable migration
399-
log.log(ERROR, "Continue migration with error executing repeatable migration " + local.getVersion(), e);
396+
log.log(ERROR, "Continue migration with error executing repeatable migration " + local.version(), e);
400397
} else {
401398
throw e;
402399
}
@@ -406,11 +403,11 @@ private void executeMigration(LocalMigrationResource local, String script, int c
406403
private long executeMigration(LocalMigrationResource local, String script) throws SQLException {
407404
long start = System.currentTimeMillis();
408405
if (local instanceof LocalDdlMigrationResource) {
409-
log.log(DEBUG, "run migration {0}", local.getLocation());
410-
scriptRunner.runScript(script, "run migration version: " + local.getVersion());
406+
log.log(DEBUG, "run migration {0}", local.location());
407+
scriptRunner.runScript(script, "run migration version: " + local.version());
411408
} else {
412409
JdbcMigration migration = ((LocalJdbcMigrationResource) local).getMigration();
413-
log.log(INFO, "Executing jdbc migration version: {0} - {1}", local.getVersion(), migration);
410+
log.log(INFO, "Executing jdbc migration version: {0} - {1}", local.version(), migration);
414411
migration.migrate(connection);
415412
}
416413
executionCount++;
@@ -437,9 +434,9 @@ private MigrationMetaRow createMetaRow(LocalMigrationResource migration, int che
437434
nextId = lastMigration.getId() + 1;
438435
}
439436

440-
String type = migration.getType();
437+
String type = migration.type();
441438
String runVersion = migration.key();
442-
String comment = migration.getComment();
439+
String comment = migration.comment();
443440

444441
return new MigrationMetaRow(nextId, type, runVersion, comment, checksum, envUserName, runOn, exeMillis);
445442
}
@@ -495,10 +492,10 @@ public boolean isEmpty() {
495492
*
496493
* @return the migrations that have been run (collected if checkState is true).
497494
*/
498-
public List<LocalMigrationResource> runAll(List<LocalMigrationResource> localVersions) throws SQLException {
495+
public List<MigrationResource> runAll(List<LocalMigrationResource> localVersions) throws SQLException {
499496
checkMinVersion();
500497
for (LocalMigrationResource localVersion : localVersions) {
501-
if (!localVersion.isRepeatable() && dbInitVersion != null && dbInitVersion.compareTo(localVersion.getVersion()) >= 0) {
498+
if (!localVersion.isRepeatable() && dbInitVersion != null && dbInitVersion.compareTo(localVersion.version()) >= 0) {
502499
log.log(DEBUG, "migration skipped by dbInitVersion {0}", dbInitVersion);
503500
} else if (!shouldRun(localVersion, priorVersion)) {
504501
break;
@@ -523,7 +520,7 @@ private void checkMinVersion() {
523520
*
524521
* @return the migrations that have been run (collected if checkstate is true).
525522
*/
526-
public List<LocalMigrationResource> runInit(LocalMigrationResource initVersion, List<LocalMigrationResource> localVersions) throws SQLException {
523+
public List<MigrationResource> runInit(LocalMigrationResource initVersion, List<LocalMigrationResource> localVersions) throws SQLException {
527524
runRepeatableInit(localVersions);
528525
initVersion.setInitType();
529526
if (!shouldRun(initVersion, null)) {

ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.ebean.datasource.DataSourceConfig;
44
import io.ebean.datasource.DataSourceFactory;
55
import io.ebean.datasource.DataSourcePool;
6-
import io.ebean.migration.runner.LocalMigrationResource;
76
import org.junit.jupiter.api.Disabled;
87
import org.junit.jupiter.api.Test;
98

@@ -37,12 +36,12 @@ public void run_when_createConnection() {
3736
config.setMigrationPath("dbmig");
3837
MigrationRunner runner = new MigrationRunner(config);
3938

40-
List<LocalMigrationResource> check = runner.checkState();
39+
List<MigrationResource> check = runner.checkState();
4140
assertThat(check).hasSize(5);
4241

43-
assertThat(check.get(0).getContent()).contains("-- do nothing");
44-
assertThat(check.get(1).getContent()).contains("create table m1");
45-
assertThat(check.get(2).getContent()).contains("create table m3");
42+
assertThat(check.get(0).content()).contains("-- do nothing");
43+
assertThat(check.get(1).content()).contains("create table m1");
44+
assertThat(check.get(2).content()).contains("create table m3");
4645

4746
runner.run();
4847
}
@@ -95,9 +94,9 @@ public void run_when_suppliedDataSource() {
9594
config.setMigrationPath("dbmig4");
9695

9796
config.setPatchResetChecksumOn("m2_view,1.2");
98-
List<LocalMigrationResource> checkState = runner.checkState(dataSource);
97+
List<MigrationResource> checkState = runner.checkState(dataSource);
9998
assertThat(checkState).hasSize(1);
100-
assertThat(checkState.get(0).getVersion().asString()).isEqualTo("1.3");
99+
assertThat(checkState.get(0).version().asString()).isEqualTo("1.3");
101100

102101
config.setPatchInsertOn("1.3");
103102
checkState = runner.checkState(dataSource);

0 commit comments

Comments
 (0)