Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support java migrations with index #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions ebean-migration/src/test/java/dbmig/V1_2_1__test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

Expand Down
39 changes: 39 additions & 0 deletions ebean-migration/src/test/java/dbmig_idx/V1_2_1__test.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MigrationResource> 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/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<MigrationResource> 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
Expand Down
5 changes: 5 additions & 0 deletions ebean-migration/src/test/resources/dbmig_idx/1.1__initial.sql
Original file line number Diff line number Diff line change
@@ -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);

5 changes: 5 additions & 0 deletions ebean-migration/src/test/resources/dbmig_idx/1.2__add_m3.sql
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions ebean-migration/src/test/resources/dbmig_idx/I__hello.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- do nothing
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create or replace view m2_vw as select id, acol from m2;
Original file line number Diff line number Diff line change
@@ -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
Loading