Skip to content

Commit

Permalink
Properly support subfolders for Flyway database migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Aug 10, 2020
1 parent 3e17e2d commit 94cf7f7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,8 @@ private Collection<String> discoverApplicationMigrations(Collection<String> loca
LinkedHashSet<String> applicationMigrationResources = new LinkedHashSet<>();
// Locations can be a comma separated list
for (String location : locations) {
// Strip any 'classpath:' protocol prefixes because they are assumed
// but not recognized by ClassLoader.getResources()
if (location != null && location.startsWith(CLASSPATH_APPLICATION_MIGRATIONS_PROTOCOL + ':')) {
location = location.substring(CLASSPATH_APPLICATION_MIGRATIONS_PROTOCOL.length() + 1);
if (location.startsWith("/")) {
location = location.substring(1);
}
}
location = normalizeLocation(location);

Enumeration<URL> migrations = Thread.currentThread().getContextClassLoader().getResources(location);
while (migrations.hasMoreElements()) {
URL path = migrations.nextElement();
Expand Down Expand Up @@ -232,11 +226,33 @@ private Collection<String> discoverApplicationMigrations(Collection<String> loca
}
}

private String normalizeLocation(String location) {
if (location == null) {
throw new IllegalStateException("Flyway migration location may not be null.");
}

// Strip any 'classpath:' protocol prefixes because they are assumed
// but not recognized by ClassLoader.getResources()
if (location.startsWith(CLASSPATH_APPLICATION_MIGRATIONS_PROTOCOL + ':')) {
location = location.substring(CLASSPATH_APPLICATION_MIGRATIONS_PROTOCOL.length() + 1);
if (location.startsWith("/")) {
location = location.substring(1);
}
}
if (!location.endsWith("/")) {
location += "/";
}

return location;
}

private Set<String> getApplicationMigrationsFromPath(final String location, final URL path)
throws IOException, URISyntaxException {
Path rootPath = Paths.get(path.toURI());

try (final Stream<Path> pathStream = Files.walk(Paths.get(path.toURI()))) {
return pathStream.filter(Files::isRegularFile)
.map(it -> Paths.get(location, it.getFileName().toString()).toString())
.map(it -> Paths.get(location, rootPath.relativize(it).toString()).toString())
// we don't want windows paths here since the paths are going to be used as classpath paths anyway
.map(it -> it.replace('\\', '/'))
.peek(it -> LOGGER.debugf("Discovered path: %s", it))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.flyway.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.inject.Inject;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfo;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class FlywayExtensionMigrateAtStartSubfolderTest {
// Quarkus built object
@Inject
Flyway flyway;

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource("db/migration-subfolder/subfolder/V1.0.0__Quarkus.sql")
.addAsResource("migrate-at-start-subfolder-config.properties", "application.properties"));

@Test
@DisplayName("Migrates at start correctly")
public void testFlywayConfigInjection() {
MigrationInfo migrationInfo = flyway.info().current();
assertNotNull(migrationInfo, "No Flyway migration was executed");

String currentVersion = migrationInfo
.getVersion()
.toString();
// Expected to be 1.0.0 as migration runs at start
assertEquals("1.0.0", currentVersion);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE quarked_flyway
(
id INT,
name VARCHAR(20)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
quarkus.datasource.db-kind=h2
quarkus.datasource.username=sa
quarkus.datasource.password=sa
quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:test-quarkus-migrate-at-start-subfolder;DB_CLOSE_DELAY=-1

# Flyway config properties
quarkus.flyway.locations=db/migration-subfolder
quarkus.flyway.migrate-at-start=true

0 comments on commit 94cf7f7

Please sign in to comment.