Skip to content

Commit 1097773

Browse files
committed
Automatically enable earlyChecksumMode when using index file with pre-computed checksums and automatically patch legacy checksums for this case
1 parent 15e5c42 commit 1097773

File tree

15 files changed

+98
-2
lines changed

15 files changed

+98
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ boolean readInitResources() {
4848
*/
4949
boolean readResources() {
5050
if (readFromIndex()) {
51+
// automatically enable earlyChecksumMode when using index file with pre-computed checksums
52+
migrationConfig.setEarlyChecksumMode(true);
5153
return true;
5254
}
5355
return readResourcesForPath(migrationConfig.getMigrationPath());
@@ -86,7 +88,7 @@ private boolean loadFromIndexFile(URL idx, String base) {
8688
if (pair.length == 2) {
8789
final var checksum = Integer.parseInt(pair[0]);
8890
final var location = pair[1].trim();
89-
final String substring = location.substring(0, location.length() - 4);
91+
final var substring = location.substring(0, location.length() - 4);
9092
final var version = MigrationVersion.parse(substring);
9193
final var url = resource(base + location);
9294
versions.add(new LocalUriMigrationResource(version, location, url, checksum));

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ final class MigrationTable {
2323
private static final String INIT_VER_0 = "0";
2424
private static final int LEGACY_MODE_CHECKSUM = 0;
2525
private static final int EARLY_MODE_CHECKSUM = 1;
26+
private static final int AUTO_PATCH_CHECKSUM = -1;
2627

2728
private final Connection connection;
2829
private final boolean checkStateOnly;
@@ -305,6 +306,7 @@ private boolean runMigration(LocalMigrationResource local, MigrationMetaRow exis
305306
int checksum2 = 0;
306307
if (local instanceof LocalUriMigrationResource) {
307308
checksum = ((LocalUriMigrationResource)local).checksum();
309+
checksum2 = patchLegacyChecksums ? AUTO_PATCH_CHECKSUM : 0;
308310
script = convertScript(local.content());
309311
} else if (local instanceof LocalDdlMigrationResource) {
310312
final String content = local.content();
@@ -349,7 +351,7 @@ boolean skipMigration(int checksum, int checksum2, LocalMigrationResource local,
349351
log.log(TRACE, "skip unchanged migration {0}", local.location());
350352
return true;
351353

352-
} else if (patchLegacyChecksums && existing.checksum() == checksum2) {
354+
} else if (patchLegacyChecksums && (existing.checksum() == checksum2 || checksum2 == AUTO_PATCH_CHECKSUM)) {
353355
if (!checkStateOnly) {
354356
log.log(INFO, "Patch migration, set early mode checksum on {0}", local.location());
355357
existing.resetChecksum(checksum, connection, updateChecksumSql);

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package io.ebean.migration;
22

3+
import io.ebean.datasource.DataSourceConfig;
4+
import io.ebean.datasource.DataSourceFactory;
5+
import io.ebean.datasource.DataSourcePool;
36
import org.junit.jupiter.api.Test;
47

8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
513
public class MigrationRunner_FastCheckTest {
614

715
private MigrationConfig createMigrationConfig() {
@@ -39,4 +47,54 @@ public void run_when() {
3947
runner.run();
4048
}
4149

50+
@Test
51+
public void autoEnableEarlyMode_when_indexFileAddedToExistingMigrations() {
52+
53+
String url = "jdbc:h2:mem:autoEnableEarlyMode";
54+
DataSourceConfig dataSourceConfig = new DataSourceConfig()
55+
.setUrl(url)
56+
.setUsername("sa")
57+
.setPassword("");
58+
59+
DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig);
60+
61+
MigrationConfig config = new MigrationConfig();
62+
config.setPlatform("h2");
63+
config.setRunPlaceholderMap(Map.of("my_table_name", "bar"));
64+
65+
// initial traditional migration
66+
config.setMigrationPath("indexB_0");
67+
MigrationRunner runner = new MigrationRunner(config);
68+
runner.run(dataSource);
69+
70+
assertThat(config.isEarlyChecksumMode()).isFalse();
71+
72+
// add an index file now, expect automatically go to early mode + patch checksums
73+
config.setMigrationPath("indexB_1");
74+
new MigrationRunner(config).run(dataSource);
75+
assertThat(config.isEarlyChecksumMode()).isTrue();
76+
77+
// early mode via <init> row + add an extra migration
78+
config.setMigrationPath("indexB_2");
79+
new MigrationRunner(config).run(dataSource);
80+
81+
dataSource.shutdown();
82+
}
83+
84+
@Test
85+
public void autoEnableEarlyMode() {
86+
87+
MigrationConfig config = createMigrationConfig();
88+
89+
config.setPlatform("h2");
90+
config.setMigrationPath("indexB_1");
91+
config.setDbUrl("jdbc:h2:mem:autoEnableEarlyMode_simple");
92+
config.setRunPlaceholderMap(Map.of("my_table_name", "bar"));
93+
94+
MigrationRunner runner = new MigrationRunner(config);
95+
runner.run();
96+
97+
assertThat(config.isEarlyChecksumMode()).isTrue();
98+
}
99+
42100
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- apply changes
2+
create table ${my_table_name} (
3+
id integer generated by default as identity not null,
4+
name varchar(255),
5+
constraint pk_${my_table_name} primary key (id)
6+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
insert into ${my_table_name} (name) values ('hi');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
create table foo (acol varchar(10));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- apply changes
2+
create table ${my_table_name} (
3+
id integer generated by default as identity not null,
4+
name varchar(255),
5+
constraint pk_${my_table_name} primary key (id)
6+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
insert into ${my_table_name} (name) values ('hi');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
create table foo (acol varchar(10));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
845768926, 1.0__initial.sql
2+
19858255, 1.1.sql
3+
513154593, 1.2.sql
4+

0 commit comments

Comments
 (0)