Skip to content

Commit

Permalink
Use automatic detection of earlyChecksumMode rather than IllegalState…
Browse files Browse the repository at this point in the history
…Exception
  • Loading branch information
rob-bygrave committed Nov 1, 2023
1 parent afaa4a6 commit 3ef3c77
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<MigrationResource> run(Connection connection) {
connection.commit();
if (!checkStateOnly) {
long commitMs = System.currentTimeMillis();
log.log(INFO, "DB migrations completed in {0}ms - executed:{1} totalMigrations:{2}", (commitMs - startMs), table.count(), table.size());
log.log(INFO, "DB migrations completed in {0}ms - executed:{1} totalMigrations:{2} mode:{3}", (commitMs - startMs), table.count(), table.size(), table.mode());
int countNonTransactional = table.runNonTransactional();
if (countNonTransactional > 0) {
log.log(INFO, "Non-transactional DB migrations completed in {0}ms - executed:{1}", (System.currentTimeMillis() - commitMs), countNonTransactional);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class MigrationTable {

private final Connection connection;
private final boolean checkStateOnly;
private final boolean earlyChecksumMode;
private boolean earlyChecksumMode;
private final MigrationPlatform platform;
private final MigrationScriptRunner scriptRunner;
private final String catalog;
Expand Down Expand Up @@ -479,7 +479,8 @@ private String convertScript(String script) {
private void addMigration(String key, MigrationMetaRow metaRow) {
if (INIT_VER_0.equals(key)) {
if (metaRow.checksum() == EARLY_MODE_CHECKSUM && !earlyChecksumMode) {
throw new IllegalStateException("Unexpected checksum value on <init> row " + metaRow);
log.log(DEBUG, "automatically detected earlyChecksumMode");
earlyChecksumMode = true;
}
initMetaRow = metaRow;
patchLegacyChecksums = earlyChecksumMode && metaRow.checksum() == LEGACY_MODE_CHECKSUM;
Expand Down Expand Up @@ -587,4 +588,11 @@ int runNonTransactional() {
int count() {
return executionCount;
}

/**
* Return the mode being used by this migration run.
*/
String mode() {
return !earlyChecksumMode ? "legacy" : (patchLegacyChecksums ? "earlyChecksum with patching" : "earlyChecksum");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.ebean.migration.runner;

import io.avaje.applog.AppLog;
import io.ebean.datasource.DataSourceConfig;
import io.ebean.datasource.DataSourceFactory;
import io.ebean.datasource.DataSourcePool;
import io.ebean.migration.MigrationConfig;
import io.ebean.migration.MigrationRunner;
import io.ebean.test.containers.PostgresContainer;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import static java.lang.System.Logger.Level.INFO;
import static org.assertj.core.api.Assertions.fail;

class MigrationEarlyModeTest {

private static final System.Logger log = AppLog.getLogger(MigrationEarlyModeTest.class);

String un = "mig_early";
String pw = "test";

private static PostgresContainer createPostgres() {
return PostgresContainer.builder("15")
.port(9823)
.containerName("pg15")
.user("mig_early")
.dbName("mig_early")
.build();
}

// @Disabled
@Test
void testEarlyMode() {
createPostgres().startWithDropCreate();

String url = createPostgres().jdbcUrl();
DataSourcePool dataSource = dataSource(url);

MigrationConfig config = new MigrationConfig();
config.setDbUrl(url);
config.setDbUsername(un);
config.setDbPassword(pw);
config.setMigrationPath("dbmig_postgres_early");
config.setRunPlaceholderMap(Map.of("my_table_name", "my_table"));

// legacy mode
new MigrationRunner(config).run(dataSource);

// early mode
log.log(INFO, "-- EARLY MODE -- ");
config.setEarlyChecksumMode(true);
new MigrationRunner(config).run(dataSource);

log.log(INFO, "-- RE-RUN EARLY MODE -- ");
new MigrationRunner(config).run(dataSource);

log.log(INFO, "-- EARLY MODE AGAIN -- ");
config.setEarlyChecksumMode(false);
new MigrationRunner(config).run(dataSource);

config.setRunPlaceholderMap(Map.of("my_table_name", "my_table", "other_table_name", "other"));
config.setMigrationPath("dbmig_postgres_early1");

new MigrationRunner(config).run(dataSource);

config.setEarlyChecksumMode(true);
new MigrationRunner(config).run(dataSource);

}

private DataSourcePool dataSource(String url) {
DataSourceConfig dataSourceConfig = new DataSourceConfig()
.setUrl(url)
.setUsername(un)
.setPassword(pw);
return DataSourceFactory.create("mig_early", dataSourceConfig);
}

// private static void dropTable(Connection conn) throws SQLException {
// try (PreparedStatement stmt = conn.prepareStatement("drop table if exists db_migration")) {
// stmt.executeUpdate();
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class MigrationTableCreateTableRaceTest {
private final MigrationPlatform platform = new MigrationPlatform();

private static PostgresContainer createPostgres() {
PostgresContainer.Builder builder = PostgresContainer.builder("13")
PostgresContainer.Builder builder = PostgresContainer.builder("15")
.port(9823);
builder.containerName("test_ebean_migration_pg13");
builder.containerName("pg15");
builder.user("mig_create_test");
builder.dbName("mig_create_test");
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


create table ${my_table_name} (id integer, acol varchar(20), bcol timestamp);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create table m1 (id integer, acol varchar(20));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


create table ${my_table_name} (id integer, acol varchar(20), bcol timestamp);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create table m1 (id integer, acol varchar(20));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


create table ${other_table_name} (acol varchar(20));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


create table m4 (acol varchar(20));

0 comments on commit 3ef3c77

Please sign in to comment.