Skip to content

Commit

Permalink
Fixed Java migrations from different packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
morhei committed Apr 20, 2023
1 parent f3bcef2 commit cc1d756
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package db.migration;

import java.sql.Statement;

import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

/**
* Migration class for some testcases.
*/
public class V1_0_1__Update extends BaseJavaMigration {
@Override
public void migrate(Context context) throws Exception {
try (Statement statement = context.getConnection().createStatement()) {
statement.executeUpdate("INSERT INTO quarked_flyway VALUES (1001, 'test')");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package db.migration;

import java.sql.Statement;

import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.migration.Context;
import org.flywaydb.core.api.migration.JavaMigration;

/**
* Migration class for some testcases.
*/
public class V1_0_2__Update implements JavaMigration {
@Override
public MigrationVersion getVersion() {
return MigrationVersion.fromVersion("1.0.2");
}

@Override
public String getDescription() {
return getClass().getSimpleName();
}

@Override
public Integer getChecksum() {
return null;
}

@Override
public boolean canExecuteInTransaction() {
return true;
}

@Override
public void migrate(Context context) throws Exception {
try (Statement statement = context.getConnection().createStatement()) {
statement.executeUpdate("INSERT INTO quarked_flyway VALUES (1002, 'test')");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.flywaydb.core.api.migration.JavaMigration;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import db.migration.V1_0_1__Update;
import db.migration.V1_0_2__Update;
import io.agroal.api.AgroalDataSource;
import io.quarkus.test.QuarkusUnitTest;

Expand All @@ -33,7 +34,7 @@ public class FlywayExtensionCleanAndMigrateAtStartWithJavaMigrationTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(V1_0_1__Update.class, V1_0_2__Update.class)
.addClasses(V1_0_1__Update.class, V1_0_2__Update.class, V9_9_9__Update.class)
.addAsResource("db/migration/V1.0.0__Quarkus.sql")
.addAsResource("clean-and-migrate-at-start-config.properties", "application.properties"));

Expand All @@ -53,19 +54,10 @@ public void testFlywayConfigInjection() throws SQLException {
assertEquals("1.0.2", currentVersion, "Expected to be 1.0.2 as there is a SQL and two Java migration scripts");
}

public static class V1_0_1__Update extends BaseJavaMigration {
@Override
public void migrate(Context context) throws Exception {
try (Statement statement = context.getConnection().createStatement()) {
statement.executeUpdate("INSERT INTO quarked_flyway VALUES (1001, 'test')");
}
}
}

public static class V1_0_2__Update implements JavaMigration {
public static class V9_9_9__Update implements JavaMigration {
@Override
public MigrationVersion getVersion() {
return MigrationVersion.fromVersion("1.0.2");
return MigrationVersion.fromVersion("9.9.9");
}

@Override
Expand All @@ -86,7 +78,7 @@ public boolean canExecuteInTransaction() {
@Override
public void migrate(Context context) throws Exception {
try (Statement statement = context.getConnection().createStatement()) {
statement.executeUpdate("INSERT INTO quarked_flyway VALUES (1002, 'test')");
statement.executeUpdate("INSERT INTO quarked_flyway VALUES (9999, 'should-not-be-added')");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
import jakarta.inject.Inject;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.flywaydb.core.api.migration.JavaMigration;
import org.h2.jdbc.JdbcSQLSyntaxErrorException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import db.migration.V1_0_1__Update;
import db.migration.V1_0_2__Update;
import io.agroal.api.AgroalDataSource;
import io.quarkus.test.QuarkusUnitTest;

Expand Down Expand Up @@ -59,42 +57,4 @@ public void testFlywayConfigInjection() throws SQLException {
assertEquals("1.0.3", currentVersion, "Expected to be 1.0.3 as there is a SQL and two Java migration scripts");
}

public static class V1_0_1__Update extends BaseJavaMigration {
@Override
public void migrate(Context context) throws Exception {
try (Statement statement = context.getConnection().createStatement()) {
statement.executeUpdate("INSERT INTO quarked_flyway VALUES (1001, 'test')");
}
}
}

public static class V1_0_2__Update implements JavaMigration {
@Override
public MigrationVersion getVersion() {
return MigrationVersion.fromVersion("1.0.2");
}

@Override
public String getDescription() {
return getClass().getSimpleName();
}

@Override
public Integer getChecksum() {
return null;
}

@Override
public boolean canExecuteInTransaction() {
return true;
}

@Override
public void migrate(Context context) throws Exception {
try (Statement statement = context.getConnection().createStatement()) {
statement.executeUpdate("INSERT INTO quarked_flyway VALUES (1002, 'test')");
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ quarkus.flyway.table=test_flyway_history
quarkus.flyway.baseline-on-migrate=false
quarkus.flyway.baseline-version=0.0.1
quarkus.flyway.baseline-description=Initial description for test
quarkus.flyway.users.locations=db/migration
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ quarkus.flyway.table=test_flyway_history
quarkus.flyway.baseline-on-migrate=false
quarkus.flyway.baseline-version=0.0.1
quarkus.flyway.baseline-description=Initial description for test
quarkus.flyway.locations=filesystem:src/test/resources/db/migration
quarkus.flyway.locations=filesystem:src/test/resources/db/migration,classpath:db/migration
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ public final class QuarkusPathLocationScanner implements ResourceAndClassScanner
private static Map<String, Collection<Callback>> applicationCallbackClasses = Collections.emptyMap(); // the set default to aid unit tests

private final Collection<LoadableResource> scannedResources;
private final Collection<Class<? extends JavaMigration>> scannedMigrationClasses;

public QuarkusPathLocationScanner(Collection<Location> locations) {
LOGGER.debugv("Locations: {0}", locations);

this.scannedResources = new ArrayList<>();
this.scannedMigrationClasses = new ArrayList<>();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

FileSystemScanner fileSystemScanner = null;
for (String migrationFile : applicationMigrationFiles) {
if (isClassPathResource(locations, migrationFile)) {
LOGGER.debugf("Loading %s", migrationFile);

scannedResources.add(new ClassPathResource(null, migrationFile, classLoader, StandardCharsets.UTF_8));
} else if (migrationFile.startsWith(Location.FILESYSTEM_PREFIX)) {
if (fileSystemScanner == null) {
Expand All @@ -51,6 +54,13 @@ public QuarkusPathLocationScanner(Collection<Location> locations) {
}
}

// Filter the provided migration classes to match the provided locations.
for (Class<? extends JavaMigration> migrationClass : applicationMigrationClasses) {
if (isClassPathResource(locations, migrationClass.getCanonicalName().replace('.', '/'))) {
LOGGER.debugf("Loading migration class %s", migrationClass.getCanonicalName());
scannedMigrationClasses.add(migrationClass);
}
}
}

public static void setApplicationCallbackClasses(Map<String, Collection<Callback>> callbackClasses) {
Expand Down Expand Up @@ -96,7 +106,7 @@ private boolean isClassPathResource(Collection<Location> locations, String migra
*/
@Override
public Collection<Class<? extends JavaMigration>> scanForClasses() {
return applicationMigrationClasses;
return scannedMigrationClasses;
}

public static void setApplicationMigrationFiles(Collection<String> applicationMigrationFiles) {
Expand Down

0 comments on commit cc1d756

Please sign in to comment.