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

Fixed Java migrations from different packages #32733

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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