Skip to content

Commit

Permalink
Add earlyChecksumMode, with automatic patching of legacy checksums
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-bygrave committed Nov 1, 2023
1 parent 95e24bc commit 9b30c00
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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).
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -437,7 +441,8 @@ private void insertIntoHistory(LocalMigrationResource local, int checksum, long
}

private MigrationMetaRow createInitMetaRow() {
return new MigrationMetaRow(0, "I", INIT_VER_0, "<init>", 0, envUserName, runOn, 0);
final int mode = earlyChecksumMode ? EARLY_MODE_CHECKSUM : LEGACY_MODE_CHECKSUM;
return new MigrationMetaRow(0, "I", INIT_VER_0, "<init>", mode, envUserName, runOn, 0);
}

/**
Expand Down Expand Up @@ -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 <init> row
if (metaRow.checksum() == EARLY_MODE_CHECKSUM && !earlyChecksumMode) {
throw new IllegalStateException("<init> 0 db migration row has unexpected checksum value? " + metaRow);
}
initMetaRow = metaRow;
patchLegacyChecksums = earlyChecksumMode && metaRow.checksum() == LEGACY_MODE_CHECKSUM;
return;
}
lastMigration = metaRow;
Expand Down Expand Up @@ -514,6 +523,10 @@ List<MigrationResource> runAll(List<LocalMigrationResource> localVersions) throw
break;
}
}
if (patchLegacyChecksums && !checkStateOnly) {
// only patch the legacy checksums once
initMetaRow.resetChecksum(EARLY_MODE_CHECKSUM, connection, updateChecksumSql);
}
return checkMigrations;
}

Expand Down

0 comments on commit 9b30c00

Please sign in to comment.