From 42872b1c59b89430a765d72da0358ed9ace4fd4c Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 2 Nov 2023 22:56:24 +1300 Subject: [PATCH] Add support for "*" for PatchResetChecksumOn to mean patch all mismatched checksums Use via: migrationConfig.setPatchResetChecksumOn("*"); All checksums that are mismatched will be patched to the new checksum value. --- .../migration/runner/MigrationTable.java | 6 ++-- .../MigrationRunner_PatchResetTest.java | 36 +++++++++++++++++++ .../indexPatchReset_0/1.0__initial.sql | 6 ++++ .../test/resources/indexPatchReset_0/1.1.sql | 1 + .../test/resources/indexPatchReset_0/1.2.sql | 1 + .../indexPatchReset_0/idx_h2.migrations | 4 +++ .../indexPatchReset_1/1.0__initial.sql | 6 ++++ .../test/resources/indexPatchReset_1/1.1.sql | 1 + .../test/resources/indexPatchReset_1/1.2.sql | 1 + 9 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 ebean-migration/src/test/java/io/ebean/migration/MigrationRunner_PatchResetTest.java create mode 100644 ebean-migration/src/test/resources/indexPatchReset_0/1.0__initial.sql create mode 100644 ebean-migration/src/test/resources/indexPatchReset_0/1.1.sql create mode 100644 ebean-migration/src/test/resources/indexPatchReset_0/1.2.sql create mode 100644 ebean-migration/src/test/resources/indexPatchReset_0/idx_h2.migrations create mode 100644 ebean-migration/src/test/resources/indexPatchReset_1/1.0__initial.sql create mode 100644 ebean-migration/src/test/resources/indexPatchReset_1/1.1.sql create mode 100644 ebean-migration/src/test/resources/indexPatchReset_1/1.2.sql diff --git a/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java b/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java index d82d936..ca17608 100644 --- a/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java +++ b/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java @@ -366,13 +366,13 @@ boolean skipMigration(int checksum, int checksum2, LocalMigrationResource local, } else if (patchLegacyChecksums && (existing.checksum() == checksum2 || checksum2 == AUTO_PATCH_CHECKSUM)) { if (!checkStateOnly) { - log.log(INFO, "Patch migration, set early mode checksum on {0}", local.location()); + log.log(INFO, "Auto patch migration, set early mode checksum on {0} to {1,number} from {2,number}", local.location(), checksum, existing.checksum()); existing.resetChecksum(checksum, connection, updateChecksumSql); } return true; } else if (patchResetChecksum(existing, checksum)) { - log.log(INFO, "Patch migration, reset checksum on {0}", local.location()); + log.log(INFO, "Patch migration, reset checksum on {0} to {1,number} from {2,number}", local.location(), checksum, existing.checksum()); return true; } else if (local.isRepeatable() || skipChecksum) { @@ -398,7 +398,7 @@ private boolean patchResetChecksum(MigrationMetaRow existing, int newChecksum) t } private boolean isResetOnVersion(String version) { - return patchResetChecksumVersions != null && patchResetChecksumVersions.contains(version); + return patchResetChecksumVersions != null && (patchResetChecksumVersions.contains(version) || patchResetChecksumVersions.contains("*")); } /** diff --git a/ebean-migration/src/test/java/io/ebean/migration/MigrationRunner_PatchResetTest.java b/ebean-migration/src/test/java/io/ebean/migration/MigrationRunner_PatchResetTest.java new file mode 100644 index 0000000..d57ea4a --- /dev/null +++ b/ebean-migration/src/test/java/io/ebean/migration/MigrationRunner_PatchResetTest.java @@ -0,0 +1,36 @@ +package io.ebean.migration; + +import io.ebean.datasource.DataSourceConfig; +import io.ebean.datasource.DataSourceFactory; +import io.ebean.datasource.DataSourcePool; +import org.junit.jupiter.api.Test; + +class MigrationRunner_PatchResetTest { + + @Test + void patchReset() { + + String url = "jdbc:h2:mem:patchReset"; + DataSourceConfig dataSourceConfig = new DataSourceConfig() + .setUrl(url) + .setUsername("sa") + .setPassword(""); + + DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig); + + MigrationConfig config = new MigrationConfig(); + config.setPlatform("h2"); + + config.setMigrationPath("indexPatchReset_0"); + MigrationRunner runner = new MigrationRunner(config); + runner.run(dataSource); + + // add an index file now, expect automatically go to early mode + patch checksums + config.setMigrationPath("indexPatchReset_1"); + config.setPatchResetChecksumOn("*"); + new MigrationRunner(config).run(dataSource); + + dataSource.shutdown(); + } + +} diff --git a/ebean-migration/src/test/resources/indexPatchReset_0/1.0__initial.sql b/ebean-migration/src/test/resources/indexPatchReset_0/1.0__initial.sql new file mode 100644 index 0000000..8f18f2a --- /dev/null +++ b/ebean-migration/src/test/resources/indexPatchReset_0/1.0__initial.sql @@ -0,0 +1,6 @@ +-- apply changes +create table m1 ( + id integer generated by default as identity not null, + name varchar(255), + constraint pk_m1 primary key (id) +); diff --git a/ebean-migration/src/test/resources/indexPatchReset_0/1.1.sql b/ebean-migration/src/test/resources/indexPatchReset_0/1.1.sql new file mode 100644 index 0000000..8583711 --- /dev/null +++ b/ebean-migration/src/test/resources/indexPatchReset_0/1.1.sql @@ -0,0 +1 @@ +insert into m1 (name) values ('hi'); diff --git a/ebean-migration/src/test/resources/indexPatchReset_0/1.2.sql b/ebean-migration/src/test/resources/indexPatchReset_0/1.2.sql new file mode 100644 index 0000000..eb186f5 --- /dev/null +++ b/ebean-migration/src/test/resources/indexPatchReset_0/1.2.sql @@ -0,0 +1 @@ +create table m2 (acol varchar(10)); diff --git a/ebean-migration/src/test/resources/indexPatchReset_0/idx_h2.migrations b/ebean-migration/src/test/resources/indexPatchReset_0/idx_h2.migrations new file mode 100644 index 0000000..6920195 --- /dev/null +++ b/ebean-migration/src/test/resources/indexPatchReset_0/idx_h2.migrations @@ -0,0 +1,4 @@ +1, 1.0__initial.sql +2, 1.1.sql +3, 1.2.sql + diff --git a/ebean-migration/src/test/resources/indexPatchReset_1/1.0__initial.sql b/ebean-migration/src/test/resources/indexPatchReset_1/1.0__initial.sql new file mode 100644 index 0000000..8f18f2a --- /dev/null +++ b/ebean-migration/src/test/resources/indexPatchReset_1/1.0__initial.sql @@ -0,0 +1,6 @@ +-- apply changes +create table m1 ( + id integer generated by default as identity not null, + name varchar(255), + constraint pk_m1 primary key (id) +); diff --git a/ebean-migration/src/test/resources/indexPatchReset_1/1.1.sql b/ebean-migration/src/test/resources/indexPatchReset_1/1.1.sql new file mode 100644 index 0000000..8583711 --- /dev/null +++ b/ebean-migration/src/test/resources/indexPatchReset_1/1.1.sql @@ -0,0 +1 @@ +insert into m1 (name) values ('hi'); diff --git a/ebean-migration/src/test/resources/indexPatchReset_1/1.2.sql b/ebean-migration/src/test/resources/indexPatchReset_1/1.2.sql new file mode 100644 index 0000000..eb186f5 --- /dev/null +++ b/ebean-migration/src/test/resources/indexPatchReset_1/1.2.sql @@ -0,0 +1 @@ +create table m2 (acol varchar(10));