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

Remove Flyway transformer by providing tighter integration #10360

Merged
merged 1 commit into from
Jun 30, 2020
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.
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 @@ -45,7 +45,6 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.BytecodeTransformerBuildItem;
import io.quarkus.deployment.builditem.CapabilityBuildItem;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
Expand Down Expand Up @@ -77,13 +76,6 @@ CapabilityBuildItem capability() {
return new CapabilityBuildItem(Capability.FLYWAY);
}

@BuildStep
void scannerTransformer(BuildProducer<BytecodeTransformerBuildItem> transformers) {
transformers
.produce(new BytecodeTransformerBuildItem(true, ScannerTransformer.FLYWAY_SCANNER_CLASS_NAME,
new ScannerTransformer()));
}

@BuildStep
IndexDependencyBuildItem indexFlyway() {
return new IndexDependencyBuildItem("org.flywaydb", "flyway-core");
Expand All @@ -106,21 +98,23 @@ void build(BuildProducer<FeatureBuildItem> featureProducer,
List<String> applicationMigrations = discoverApplicationMigrations(getMigrationLocations(dataSourceNames));
recorder.setApplicationMigrationFiles(applicationMigrations);

Set<Class<?>> javaMigrationClasses = new HashSet<>();
Set<Class<? extends JavaMigration>> javaMigrationClasses = new HashSet<>();
addJavaMigrations(combinedIndexBuildItem.getIndex().getAllKnownImplementors(JAVA_MIGRATION), context,
reflectiveClassProducer, javaMigrationClasses);
recorder.setApplicationMigrationClasses(javaMigrationClasses);

resourceProducer.produce(new NativeImageResourceBuildItem(applicationMigrations.toArray(new String[0])));
}

@SuppressWarnings("unchecked")
private void addJavaMigrations(Collection<ClassInfo> candidates, RecorderContext context,
BuildProducer<ReflectiveClassBuildItem> reflectiveClassProducer, Set<Class<?>> javaMigrationClasses) {
BuildProducer<ReflectiveClassBuildItem> reflectiveClassProducer,
Set<Class<? extends JavaMigration>> javaMigrationClasses) {
for (ClassInfo javaMigration : candidates) {
if (Modifier.isAbstract(javaMigration.flags())) {
continue;
}
javaMigrationClasses.add(context.classProxy(javaMigration.name().toString()));
javaMigrationClasses.add((Class<JavaMigration>) context.classProxy(javaMigration.name().toString()));
reflectiveClassProducer.produce(new ReflectiveClassBuildItem(false, false, javaMigration.name().toString()));
}
}
Expand Down

This file was deleted.

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

import java.util.Arrays;

import javax.sql.DataSource;

import org.flywaydb.core.Flyway;
Expand Down Expand Up @@ -47,6 +49,18 @@ public Flyway createFlyway(DataSource dataSource) {
configure.baselineDescription(flywayRuntimeConfig.baselineDescription.get());
}
configure.placeholders(flywayRuntimeConfig.placeholders);

/*
* Ensure that no classpath scanning takes place by setting the ClassProvider and the ResourceProvider
* (see Flyway#createResourceAndClassProviders)
*/

// the static fields of this class have already been set at static-init
QuarkusPathLocationScanner quarkusPathLocationScanner = new QuarkusPathLocationScanner(
Arrays.asList(configure.getLocations()));
configure.javaMigrationClassProvider(new QuarkusFlywayClassProvider<>(quarkusPathLocationScanner.scanForClasses()));
configure.resourceProvider(new QuarkusFlywayResourceProvider(quarkusPathLocationScanner.scanForResources()));

return configure.load();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.sql.DataSource;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.migration.JavaMigration;
import org.jboss.logging.Logger;

import io.quarkus.agroal.runtime.DataSources;
Expand All @@ -26,7 +27,7 @@ public void setApplicationMigrationFiles(Collection<String> migrationFiles) {
QuarkusPathLocationScanner.setApplicationMigrationFiles(migrationFiles);
}

public void setApplicationMigrationClasses(Collection<Class<?>> migrationClasses) {
public void setApplicationMigrationClasses(Collection<Class<? extends JavaMigration>> migrationClasses) {
log.debugv("Setting the following application migration classes: {0}", migrationClasses);
QuarkusPathLocationScanner.setApplicationMigrationClasses(migrationClasses);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.flyway.runtime;

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

import org.flywaydb.core.api.ClassProvider;

public class QuarkusFlywayClassProvider<I> implements ClassProvider<I> {

private final Collection<Class<? extends I>> classes;

public QuarkusFlywayClassProvider(Collection<Class<? extends I>> classes) {
this.classes = Collections.unmodifiableCollection(classes);
}

@Override
public Collection<Class<? extends I>> getClasses() {
return classes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.flyway.runtime;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.flywaydb.core.api.ResourceProvider;
import org.flywaydb.core.internal.resource.LoadableResource;
import org.flywaydb.core.internal.util.StringUtils;
import org.jboss.logging.Logger;

/**
* This class is very similar to {@link org.flywaydb.core.internal.scanner.Scanner}
* TODO: refactor upstream to move common methods to utility class
*/
public class QuarkusFlywayResourceProvider implements ResourceProvider {

private static final Logger log = Logger.getLogger(FlywayRecorder.class);

private final Collection<LoadableResource> resources;

public QuarkusFlywayResourceProvider(Collection<LoadableResource> resources) {
this.resources = resources;
}

@Override
public LoadableResource getResource(String name) {
for (LoadableResource resource : resources) {
String fileName = resource.getRelativePath();
if (fileName.equals(name)) {
return resource;
}
}
return null;
}

/**
* Returns all known resources starting with the specified prefix and ending with any of the specified suffixes.
*
* @param prefix The prefix of the resource names to match.
* @param suffixes The suffixes of the resource names to match.
* @return The resources that were found.
*/
public Collection<LoadableResource> getResources(String prefix, String... suffixes) {
List<LoadableResource> result = new ArrayList<>();
for (LoadableResource resource : resources) {
String fileName = resource.getFilename();
if (StringUtils.startsAndEndsWith(fileName, prefix, suffixes)) {
result.add(resource);
} else {
log.debug("Filtering out resource: " + resource.getAbsolutePath() + " (filename: " + fileName + ")");
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.internal.resource.LoadableResource;
import org.flywaydb.core.internal.resource.classpath.ClassPathResource;
import org.flywaydb.core.internal.scanner.classpath.ResourceAndClassScanner;
Expand All @@ -18,8 +20,8 @@
public final class QuarkusPathLocationScanner implements ResourceAndClassScanner {
private static final Logger LOGGER = Logger.getLogger(QuarkusPathLocationScanner.class);
private static final String LOCATION_SEPARATOR = "/";
private static Collection<String> applicationMigrationFiles;
private static Collection<Class<?>> applicationMigrationClasses;
private static Collection<String> applicationMigrationFiles = Collections.emptyList(); // the set default to aid unit tests
private static Collection<Class<? extends JavaMigration>> applicationMigrationClasses = Collections.emptyList(); // the set default to aid unit tests

private final Collection<LoadableResource> scannedResources;

Expand Down Expand Up @@ -72,15 +74,15 @@ private boolean canHandleMigrationFile(Collection<Location> locations, String mi
* @return The non-abstract classes that were found.
*/
@Override
public Collection<Class<?>> scanForClasses() {
public Collection<Class<? extends JavaMigration>> scanForClasses() {
return applicationMigrationClasses;
}

public static void setApplicationMigrationFiles(Collection<String> applicationMigrationFiles) {
QuarkusPathLocationScanner.applicationMigrationFiles = applicationMigrationFiles;
}

public static void setApplicationMigrationClasses(Collection<Class<?>> applicationMigrationClasses) {
public static void setApplicationMigrationClasses(Collection<Class<? extends JavaMigration>> applicationMigrationClasses) {
QuarkusPathLocationScanner.applicationMigrationClasses = applicationMigrationClasses;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.flyway.runtime.graal;

import java.nio.charset.Charset;
import java.util.Collection;

import org.flywaydb.core.api.Location;
import org.flywaydb.core.internal.scanner.LocationScannerCache;
import org.flywaydb.core.internal.scanner.ResourceNameCache;

import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;

/**
* Needed to get rid of some Android related classes
*/
@TargetClass(className = "org.flywaydb.core.internal.scanner.Scanner")
public final class ScannerSubstitutions {

@Substitute
public ScannerSubstitutions(Class<?> implementedInterface, Collection<Location> locations, ClassLoader classLoader,
Charset encoding, ResourceNameCache resourceNameCache, LocationScannerCache locationScannerCache) {
throw new IllegalStateException("'org.flywaydb.core.internal.scanner.Scanner' is never used in Quarkus");
}
}