Skip to content

Commit

Permalink
WIP for resource loading in flyway extension
Browse files Browse the repository at this point in the history
  • Loading branch information
cristhiank committed Mar 21, 2019
1 parent adb810d commit 35a5498
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,33 @@

package io.quarkus.flyway;

import static java.nio.file.Files.walk;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.substrate.SubstrateResourceBuildItem;

class FlywayBuildStep {

private static final String FLYWAY_APPLICATION_ARCHIVE_MARKERS = "org/flywaydb";
private static final String FLYWAY_DATABASES_PATH_ROOT = "org/flywaydb/core/internal/database";
private static final String FLYWAY_METATADA_TABLE_FILENAME = "createMetadataTable.sql";
private static final String FLYWAY_METADATA_TABLE_FILENAME = "createMetaDataTable.sql";
private static final String[] FLYWAY_DATABASES_WITH_SQL_FILE = {
"cockroachdb",
"derby",
Expand All @@ -46,28 +57,53 @@ class FlywayBuildStep {
"sybasease"
};

private static final Logger LOGGER = Logger.getLogger("io.quarkus.flyway.FlywayBuildStep");
private static final Logger LOGGER = Logger.getLogger(FlywayBuildStep.class);

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FeatureBuildItem.FLYWAY);
}

@BuildStep(providesCapabilities = "io.quarkus.flyway", applicationArchiveMarkers = { FLYWAY_APPLICATION_ARCHIVE_MARKERS })
@Record(ExecutionTime.STATIC_INIT)
@BuildStep(providesCapabilities = "io.quarkus.flyway")
void registerSubstrateResources(
BuildProducer<SubstrateResourceBuildItem> resource,
ApplicationArchivesBuildItem archives) {
ApplicationArchivesBuildItem appArchives) throws IOException, URISyntaxException {
Path root = appArchives.getRootArchive().getArchiveRoot();
List<String> resources = generateDatabasesSQLFiles();
resources.add("db/migration/*.sql");
resources.addAll(discoverApplicationMigrations(root));
resource.produce(new SubstrateResourceBuildItem(resources.toArray(new String[0])));
}

private List<String> discoverApplicationMigrations(Path root) throws IOException, URISyntaxException {
List<String> resources = new ArrayList<>();
try {
// TODO: this should be configurable, using flyway default
String location = "db/migration";
Enumeration<URL> migrations = Thread.currentThread().getContextClassLoader().getResources(location);
while (migrations.hasMoreElements()) {
URL path = migrations.nextElement();
LOGGER.info("Adding application migrations in path: " + path);
List<String> applicationMigrations = walk(Paths.get(path.toURI()))
.filter(Files::isRegularFile)
.map(it -> root.relativize(it).toString())
.peek(it -> LOGGER.info("Discovered: " + it))
.collect(Collectors.toList());
resources.addAll(applicationMigrations);
}
return resources;
} catch (IOException | URISyntaxException e) {
LOGGER.fatal("Error discovering application migrations: " + e.getMessage(), e);
throw e;
}
}

private List<String> generateDatabasesSQLFiles() {
List<String> result = new ArrayList<>();
for (String database : FLYWAY_DATABASES_WITH_SQL_FILE) {
String filePath = FLYWAY_DATABASES_PATH_ROOT + "/" + database + "/" + FLYWAY_METATADA_TABLE_FILENAME;
String filePath = FLYWAY_DATABASES_PATH_ROOT + "/" + database + "/" + FLYWAY_METADATA_TABLE_FILENAME;
result.add(filePath);
LOGGER.info("Adding " + filePath);
LOGGER.info("Adding flyway internal migration: " + filePath);
}
return result;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.quarkus.flyway.runtime;

import java.util.Collection;
import java.util.Collections;

import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.logging.Log;
import org.flywaydb.core.api.logging.LogFactory;
import org.flywaydb.core.internal.resource.LoadableResource;
import org.flywaydb.core.internal.scanner.classpath.ResourceAndClassScanner;

public final class QuarkusPathLocationScanner implements ResourceAndClassScanner {
private static final Log LOG = LogFactory.getLog( QuarkusPathLocationScanner.class );
private final Location location;

public QuarkusPathLocationScanner(Location location) {
this.location = location;
LOG.warn( "LOCATION " + location );
}

/**
* Scans the classpath for resources under the configured location.
*
* @return The resources that were found.
*/
@Override
public Collection<LoadableResource> scanForResources() {
// NEED TO DO SOMETHING TO RETURN THE RESOURCES ADDED TO SVM in FlywayBuildStep
return Collections.emptyList();
}

/**
* Scans the classpath for concrete classes under the specified package implementing this interface.
* Non-instantiable abstract classes are filtered out.
*
* @return The non-abstract classes that were found.
*/
@Override
public Collection<Class<?>> scanForClasses() {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

import org.flywaydb.core.api.Location;
import org.flywaydb.core.internal.resource.LoadableResource;
import org.flywaydb.core.internal.scanner.classpath.ClassPathScanner;
import org.flywaydb.core.internal.scanner.classpath.ResourceAndClassScanner;
import org.flywaydb.core.internal.scanner.filesystem.FileSystemScanner;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.Substitute;
Expand All @@ -51,14 +49,8 @@ public final class ScannerSubstitutions {
*/
@Substitute
public ScannerSubstitutions(Collection<Location> locations, ClassLoader classLoader, Charset encoding) {
FileSystemScanner fileSystemScanner = new FileSystemScanner(encoding);
Logger log = Logger.getLogger("io.quarkus.test");
for (Location location : locations) {
log.warning(location.toString());
ResourceAndClassScanner resourceAndClassScanner = new ClassPathScanner(
classLoader,
encoding,
location);
ResourceAndClassScanner resourceAndClassScanner = new QuarkusPathLocationScanner(location);
resources.addAll(resourceAndClassScanner.scanForResources());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class FlywayFunctionalityResource {
private static final Logger LOGGER = Logger.getLogger("io.quarkus.example.flyway.FlywayFunctionalityResource");
private static final Logger LOGGER = Logger.getLogger(FlywayFunctionalityResource.class);
@ConfigProperty(name = "datasource.url")
String dbURL;
@ConfigProperty(name = "datasource.username")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
datasource.url=jdbc:h2:tcp://localhost/mem:test;DB_CLOSE_DELAY=-1
datasource.username=sa
datasource.password=sa
quarkus.log.console.level=DEBUG
quarkus.log.category."org.flywaydb.core".level=DEBUG
quarkus.log.category."io.quarkus.flyway".level=DEBUG

0 comments on commit 35a5498

Please sign in to comment.