From 3f807d282ee7d8b8a9c776030500205fee61b48b Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Wed, 22 Nov 2023 13:18:49 +0100 Subject: [PATCH 1/2] Add testcases for java migrations with index --- .../src/test/java/dbmig/V1_2_1__test.java | 2 + .../src/test/java/dbmig_idx/V1_2_1__test.java | 39 +++++++++++++++++++ .../ebean/migration/MigrationRunnerTest.java | 26 ++++++++++++- .../test/resources/dbmig_idx/1.1__initial.sql | 5 +++ .../test/resources/dbmig_idx/1.2__add_m3.sql | 5 +++ .../src/test/resources/dbmig_idx/I__hello.sql | 1 + .../test/resources/dbmig_idx/R__m2_view.sql | 1 + .../resources/dbmig_idx/idx_h2.migrations | 5 +++ 8 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 ebean-migration/src/test/java/dbmig_idx/V1_2_1__test.java create mode 100644 ebean-migration/src/test/resources/dbmig_idx/1.1__initial.sql create mode 100644 ebean-migration/src/test/resources/dbmig_idx/1.2__add_m3.sql create mode 100644 ebean-migration/src/test/resources/dbmig_idx/I__hello.sql create mode 100644 ebean-migration/src/test/resources/dbmig_idx/R__m2_view.sql create mode 100644 ebean-migration/src/test/resources/dbmig_idx/idx_h2.migrations diff --git a/ebean-migration/src/test/java/dbmig/V1_2_1__test.java b/ebean-migration/src/test/java/dbmig/V1_2_1__test.java index 9b917c2..1aa3b29 100644 --- a/ebean-migration/src/test/java/dbmig/V1_2_1__test.java +++ b/ebean-migration/src/test/java/dbmig/V1_2_1__test.java @@ -5,6 +5,7 @@ import io.ebean.migration.ConfigurationAware; import io.ebean.migration.JdbcMigration; import io.ebean.migration.MigrationConfig; +import io.ebean.migration.MigrationRunnerTest; /** * Sample migration. @@ -27,6 +28,7 @@ public void setMigrationConfig(MigrationConfig config) { @Override public void migrate(Connection connection) { + MigrationRunnerTest.javaMigrationExecuted = true; System.out.println("Executing migration on " + connection); } diff --git a/ebean-migration/src/test/java/dbmig_idx/V1_2_1__test.java b/ebean-migration/src/test/java/dbmig_idx/V1_2_1__test.java new file mode 100644 index 0000000..e4c43e2 --- /dev/null +++ b/ebean-migration/src/test/java/dbmig_idx/V1_2_1__test.java @@ -0,0 +1,39 @@ +package dbmig_idx; + +import io.ebean.migration.ConfigurationAware; +import io.ebean.migration.JdbcMigration; +import io.ebean.migration.MigrationConfig; +import io.ebean.migration.MigrationRunnerTest; + +import java.sql.Connection; + +/** + * Sample migration. + * + * @author Roland Praml, FOCONIS AG + * + */ +public class V1_2_1__test implements JdbcMigration, ConfigurationAware{ + + private MigrationConfig config; + + public static class MyDto { + String id; + } + + @Override + public void setMigrationConfig(MigrationConfig config) { + this.config = config; + } + + @Override + public void migrate(Connection connection) { + MigrationRunnerTest.javaMigrationExecuted = true; + System.out.println("Executing migration on " + connection); + } + + @Override + public String toString() { + return "Dummy jdbc migration"; + } +} diff --git a/ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java b/ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java index eb1df6f..4fa8469 100644 --- a/ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java +++ b/ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java @@ -28,22 +28,46 @@ private MigrationConfig createMigrationConfig() { return config; } + public static boolean javaMigrationExecuted; + @Test public void run_when_createConnection() { + javaMigrationExecuted = false; MigrationConfig config = createMigrationConfig(); config.setMigrationPath("dbmig"); MigrationRunner runner = new MigrationRunner(config); - List check = runner.checkState(); assertThat(check).hasSize(5); assertThat(check.get(0).content()).contains("-- do nothing"); assertThat(check.get(1).content()).contains("create table m1"); assertThat(check.get(2).content()).contains("create table m3"); + assertThat(check.get(3).location()).isEqualTo("dbmig_idx/V1_2_1__test.class"); + assertThat(javaMigrationExecuted).isFalse(); + runner.run(); + assertThat(javaMigrationExecuted).isTrue(); + } + + @Test + public void win_with_idx_file() { + + javaMigrationExecuted = false; + MigrationConfig config = createMigrationConfig(); + config.setMigrationPath("dbmig_idx"); + MigrationRunner runner = new MigrationRunner(config); + List check = runner.checkState(); + assertThat(check).hasSize(5); + + assertThat(check.get(0).content()).contains("-- do nothing"); + assertThat(check.get(1).content()).contains("create table m1"); + assertThat(check.get(2).content()).contains("create table m3"); + assertThat(check.get(3).location()).isEqualTo("dbmig_idx/V1_2_1__test.class"); + assertThat(javaMigrationExecuted).isFalse(); runner.run(); + assertThat(javaMigrationExecuted).isTrue(); } @Test diff --git a/ebean-migration/src/test/resources/dbmig_idx/1.1__initial.sql b/ebean-migration/src/test/resources/dbmig_idx/1.1__initial.sql new file mode 100644 index 0000000..c627009 --- /dev/null +++ b/ebean-migration/src/test/resources/dbmig_idx/1.1__initial.sql @@ -0,0 +1,5 @@ +create table m1 (id integer, acol varchar(20)); +-- Check with DB2: +-- call sysproc.admin_cmd('reorg table m1'); +create table m2 (id integer, acol varchar(20), bcol timestamp); + diff --git a/ebean-migration/src/test/resources/dbmig_idx/1.2__add_m3.sql b/ebean-migration/src/test/resources/dbmig_idx/1.2__add_m3.sql new file mode 100644 index 0000000..3518305 --- /dev/null +++ b/ebean-migration/src/test/resources/dbmig_idx/1.2__add_m3.sql @@ -0,0 +1,5 @@ +create table m3 (id integer, acol varchar(20), bcol timestamp); + +alter table m1 add column addcol varchar(10); + +insert into m3 (id, acol) VALUES (1, 'text with ; sign'); -- plus some comment diff --git a/ebean-migration/src/test/resources/dbmig_idx/I__hello.sql b/ebean-migration/src/test/resources/dbmig_idx/I__hello.sql new file mode 100644 index 0000000..63a4934 --- /dev/null +++ b/ebean-migration/src/test/resources/dbmig_idx/I__hello.sql @@ -0,0 +1 @@ +-- do nothing diff --git a/ebean-migration/src/test/resources/dbmig_idx/R__m2_view.sql b/ebean-migration/src/test/resources/dbmig_idx/R__m2_view.sql new file mode 100644 index 0000000..6a7c4df --- /dev/null +++ b/ebean-migration/src/test/resources/dbmig_idx/R__m2_view.sql @@ -0,0 +1 @@ +create or replace view m2_vw as select id, acol from m2; diff --git a/ebean-migration/src/test/resources/dbmig_idx/idx_h2.migrations b/ebean-migration/src/test/resources/dbmig_idx/idx_h2.migrations new file mode 100644 index 0000000..a93c654 --- /dev/null +++ b/ebean-migration/src/test/resources/dbmig_idx/idx_h2.migrations @@ -0,0 +1,5 @@ +-423501496, I__hello.sql +-250044392, 1.1__initial.sql +531446402, 1.2__add_m3.sql +0, V1_2_1__test.class +-846848863, R__m2_view.sql From ea375a8c44052aa9da3946fa56e07e62e5f39217 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Wed, 22 Nov 2023 13:22:59 +0100 Subject: [PATCH 2/2] FIX: java migration support in idx file --- .../migration/runner/LocalMigrationResources.java | 12 ++++++++++-- .../java/io/ebean/migration/MigrationRunnerTest.java | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ebean-migration/src/main/java/io/ebean/migration/runner/LocalMigrationResources.java b/ebean-migration/src/main/java/io/ebean/migration/runner/LocalMigrationResources.java index cd605bb..94e2b42 100644 --- a/ebean-migration/src/main/java/io/ebean/migration/runner/LocalMigrationResources.java +++ b/ebean-migration/src/main/java/io/ebean/migration/runner/LocalMigrationResources.java @@ -90,8 +90,16 @@ private boolean loadFromIndexFile(URL idx, String base) { final var location = pair[1].trim(); final var substring = location.substring(0, location.length() - 4); final var version = MigrationVersion.parse(substring); - final var url = resource(base + location); - versions.add(new LocalUriMigrationResource(version, location, url, checksum)); + if (location.endsWith(".class")) { + String className = base.replace('/', '.').substring(1) + + location.substring(0, location.length() - 6); + JdbcMigration instance = migrationConfig.getJdbcMigrationFactory().createInstance(className); + assert instance.getChecksum() == checksum; + versions.add(new LocalJdbcMigrationResource(version, location, instance)); + } else { + final var url = resource(base + location); + versions.add(new LocalUriMigrationResource(version, location, url, checksum)); + } } } } diff --git a/ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java b/ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java index 4fa8469..e485f76 100644 --- a/ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java +++ b/ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java @@ -44,7 +44,7 @@ public void run_when_createConnection() { assertThat(check.get(0).content()).contains("-- do nothing"); assertThat(check.get(1).content()).contains("create table m1"); assertThat(check.get(2).content()).contains("create table m3"); - assertThat(check.get(3).location()).isEqualTo("dbmig_idx/V1_2_1__test.class"); + assertThat(check.get(3).location()).isEqualTo("dbmig/V1_2_1__test.class"); assertThat(javaMigrationExecuted).isFalse(); runner.run(); assertThat(javaMigrationExecuted).isTrue();