diff --git a/ebean-migration/src/main/java/io/ebean/migration/MigrationConfig.java b/ebean-migration/src/main/java/io/ebean/migration/MigrationConfig.java index fa28208..4f89396 100644 --- a/ebean-migration/src/main/java/io/ebean/migration/MigrationConfig.java +++ b/ebean-migration/src/main/java/io/ebean/migration/MigrationConfig.java @@ -66,7 +66,7 @@ public class MigrationConfig { private String platform; private Properties properties; private boolean earlyChecksumMode; - private boolean autoPatchChecksum = true; + /** * Return the name of the migration table. */ @@ -470,8 +470,7 @@ public void load(Properties props) { dbSchema = getProperty("schema", dbSchema); skipMigrationRun = getBool("skipMigrationRun", skipMigrationRun); skipChecksum = getBool("skipChecksum", skipChecksum); - earlyChecksumMode = getBool("earlyChecksum", earlyChecksumMode); - autoPatchChecksum = getBool("autoPatchChecksum", autoPatchChecksum); + earlyChecksumMode = getBool("earlyChecksumMode", earlyChecksumMode); createSchemaIfNotExists = getBool("createSchemaIfNotExists", createSchemaIfNotExists); setCurrentSchema = getBool("setCurrentSchema", setCurrentSchema); basePlatform = getProperty("basePlatform", basePlatform); @@ -578,21 +577,6 @@ public void setPlatform(String platform) { this.platform = platform; } - /** - * Return true if changing to use earlyChecksumMode, and we want to automatically - * patch checksums that were computed on the original mode. - */ - public boolean isAutoPatchChecksum() { - return autoPatchChecksum; - } - - /** - * Set this to false in order to turn off auto patching for earlyChecksumMode. - */ - public void setAutoPatchChecksum(boolean autoPatchChecksum) { - this.autoPatchChecksum = autoPatchChecksum; - } - /** * Return true if using the earlyChecksumMode which means checksums are computed * before any expressions in the scripts are translated. 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 2ee8f61..110168f 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 @@ -21,11 +21,12 @@ final class MigrationTable { static final System.Logger log = AppLog.getLogger("io.ebean.DDL"); private static final String INIT_VER_0 = "0"; + private static final int LEGACY_MODE_CHECKSUM = 0; + private static final int EARLY_MODE_CHECKSUM = 1; private final Connection connection; private final boolean checkStateOnly; - private final boolean autoPatchChecksum; - private final boolean useEarlyChecksum; + private final boolean earlyChecksumMode; private final MigrationPlatform platform; private final MigrationScriptRunner scriptRunner; private final String catalog; @@ -67,6 +68,8 @@ final class MigrationTable { private MigrationVersion dbInitVersion; private int executionCount; + private boolean patchLegacyChecksums; + private MigrationMetaRow initMetaRow; /** * Construct with server, configuration and jdbc connection (DB admin user). @@ -76,8 +79,7 @@ final class MigrationTable { this.connection = connection; this.scriptRunner = new MigrationScriptRunner(connection, platform); this.checkStateOnly = checkStateOnly; - this.useEarlyChecksum = config.isEarlyChecksumMode(); - this.autoPatchChecksum = !checkStateOnly && config.isAutoPatchChecksum(); + this.earlyChecksumMode = config.isEarlyChecksumMode(); this.migrations = new LinkedHashMap<>(); this.catalog = null; this.allowErrorInRepeatable = config.isAllowErrorInRepeatable(); @@ -308,8 +310,8 @@ private boolean runMigration(LocalMigrationResource local, MigrationMetaRow exis final String content = local.content(); script = convertScript(content); // checksum on original content (NEW) or converted script content (LEGACY) - checksum = Checksum.calculate(useEarlyChecksum ? content : script); - checksum2 = autoPatchChecksum ? Checksum.calculate(script) : 0; + checksum = Checksum.calculate(earlyChecksumMode ? content : script); + checksum2 = patchLegacyChecksums ? Checksum.calculate(script) : 0; } else { checksum = ((LocalJdbcMigrationResource) local).checksum(); } @@ -347,9 +349,11 @@ boolean skipMigration(int checksum, int checksum2, LocalMigrationResource local, log.log(TRACE, "skip unchanged migration {0}", local.location()); return true; - } else if (autoPatchChecksum && existing.checksum() == checksum2) { - log.log(INFO, "Patch migration, reset checksum on {0}", local.location()); - existing.resetChecksum(checksum, connection, updateChecksumSql); + } else if (patchLegacyChecksums && existing.checksum() == checksum2) { + if (!checkStateOnly) { + log.log(INFO, "Patch migration, set earlyChecksumMode on {0}", local.location()); + existing.resetChecksum(checksum, connection, updateChecksumSql); + } return true; } else if (patchResetChecksum(existing, checksum)) { @@ -437,7 +441,8 @@ private void insertIntoHistory(LocalMigrationResource local, int checksum, long } private MigrationMetaRow createInitMetaRow() { - return new MigrationMetaRow(0, "I", INIT_VER_0, "", 0, envUserName, runOn, 0); + final int mode = earlyChecksumMode ? EARLY_MODE_CHECKSUM : LEGACY_MODE_CHECKSUM; + return new MigrationMetaRow(0, "I", INIT_VER_0, "", mode, envUserName, runOn, 0); } /** @@ -473,7 +478,11 @@ private String convertScript(String script) { */ private void addMigration(String key, MigrationMetaRow metaRow) { if (INIT_VER_0.equals(key)) { - // ignore the version 0 row + if (metaRow.checksum() == EARLY_MODE_CHECKSUM && !earlyChecksumMode) { + throw new IllegalStateException(" 0 db migration row has unexpected checksum value? " + metaRow); + } + initMetaRow = metaRow; + patchLegacyChecksums = earlyChecksumMode && metaRow.checksum() == LEGACY_MODE_CHECKSUM; return; } lastMigration = metaRow; @@ -514,6 +523,10 @@ List runAll(List localVersions) throw break; } } + if (patchLegacyChecksums && !checkStateOnly) { + // only patch the legacy checksums once + initMetaRow.resetChecksum(EARLY_MODE_CHECKSUM, connection, updateChecksumSql); + } return checkMigrations; }