Skip to content

Commit 19fb329

Browse files
committed
Merge pull request #40821 from MazizEsa
* gh-40821: Polish "Fix Flyway 10 in a GraalVM native image" Fix Flyway 10 in a GraalVM native image Closes gh-40821
2 parents 244df31 + 10e23b8 commit 19fb329

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,9 +36,11 @@
3636
import org.flywaydb.core.api.migration.JavaMigration;
3737
import org.flywaydb.core.extensibility.ConfigurationExtension;
3838
import org.flywaydb.core.internal.database.postgresql.PostgreSQLConfigurationExtension;
39+
import org.flywaydb.core.internal.scanner.Scanner;
3940
import org.flywaydb.database.oracle.OracleConfigurationExtension;
4041
import org.flywaydb.database.sqlserver.SQLServerConfigurationExtension;
4142

43+
import org.springframework.aot.hint.MemberCategory;
4244
import org.springframework.aot.hint.RuntimeHints;
4345
import org.springframework.aot.hint.RuntimeHintsRegistrar;
4446
import org.springframework.beans.factory.ObjectProvider;
@@ -78,6 +80,7 @@
7880
import org.springframework.jdbc.support.JdbcUtils;
7981
import org.springframework.jdbc.support.MetaDataAccessException;
8082
import org.springframework.util.Assert;
83+
import org.springframework.util.ClassUtils;
8184
import org.springframework.util.CollectionUtils;
8285
import org.springframework.util.ObjectUtils;
8386
import org.springframework.util.StringUtils;
@@ -461,6 +464,9 @@ static class FlywayAutoConfigurationRuntimeHints implements RuntimeHintsRegistra
461464
@Override
462465
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
463466
hints.resources().registerPattern("db/migration/*");
467+
if (ClassUtils.isPresent("org.flywaydb.core.extensibility.Tier", classLoader)) {
468+
hints.reflection().registerType(Scanner.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
469+
}
464470
}
465471

466472
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,10 @@
1616

1717
package org.springframework.boot.autoconfigure.flyway;
1818

19+
import java.lang.reflect.Constructor;
1920
import java.util.Arrays;
2021

22+
import org.flywaydb.core.api.configuration.Configuration;
2123
import org.flywaydb.core.api.configuration.FluentConfiguration;
2224
import org.flywaydb.core.api.migration.JavaMigration;
2325
import org.flywaydb.core.internal.scanner.LocationScannerCache;
@@ -29,21 +31,44 @@
2931
* {@link org.flywaydb.core.api.ResourceProvider}.
3032
*
3133
* @author Moritz Halbritter
34+
* @author Maziz Esa
3235
*/
3336
class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer {
3437

3538
@Override
3639
public void customize(FluentConfiguration configuration) {
3740
if (configuration.getResourceProvider() == null) {
38-
Scanner<JavaMigration> scanner = new Scanner<>(JavaMigration.class,
39-
Arrays.asList(configuration.getLocations()), configuration.getClassLoader(),
40-
configuration.getEncoding(), configuration.isDetectEncoding(), false, new ResourceNameCache(),
41-
new LocationScannerCache(), configuration.isFailOnMissingLocations());
41+
Scanner<?> scanner = createScanner(configuration);
4242
NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner,
4343
configuration.getClassLoader(), Arrays.asList(configuration.getLocations()),
4444
configuration.getEncoding(), configuration.isFailOnMissingLocations());
4545
configuration.resourceProvider(resourceProvider);
4646
}
4747
}
4848

49+
private static Scanner<?> createScanner(FluentConfiguration configuration) {
50+
try {
51+
return new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()),
52+
configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(),
53+
false, new ResourceNameCache(), new LocationScannerCache(),
54+
configuration.isFailOnMissingLocations());
55+
}
56+
catch (NoSuchMethodError ex) {
57+
// Flyway 10
58+
return createFlyway10Scanner(configuration);
59+
}
60+
}
61+
62+
private static Scanner<?> createFlyway10Scanner(FluentConfiguration configuration) throws LinkageError {
63+
try {
64+
Constructor<?> scannerConstructor = Scanner.class.getDeclaredConstructor(Class.class, boolean.class,
65+
ResourceNameCache.class, LocationScannerCache.class, Configuration.class);
66+
return (Scanner<?>) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(),
67+
new LocationScannerCache(), configuration);
68+
}
69+
catch (Exception ex) {
70+
throw new RuntimeException(ex);
71+
}
72+
}
73+
4974
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.flyway;
18+
19+
import java.util.Collection;
20+
21+
import org.flywaydb.core.api.ResourceProvider;
22+
import org.flywaydb.core.api.configuration.FluentConfiguration;
23+
import org.flywaydb.core.api.resource.LoadableResource;
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link NativeImageResourceProviderCustomizer} with Flyway 10.
32+
*
33+
* @author Moritz Halbritter
34+
* @author Andy Wilkinson
35+
* @author Maziz Esa
36+
*/
37+
@ClassPathOverrides("org.flywaydb:flyway-core:10.12.0")
38+
class Flyway10xNativeImageResourceProviderCustomizerTests {
39+
40+
private final NativeImageResourceProviderCustomizer customizer = new NativeImageResourceProviderCustomizer();
41+
42+
@Test
43+
void nativeImageResourceProviderShouldFindMigrations() {
44+
FluentConfiguration configuration = new FluentConfiguration();
45+
this.customizer.customize(configuration);
46+
ResourceProvider resourceProvider = configuration.getResourceProvider();
47+
Collection<LoadableResource> migrations = resourceProvider.getResources("V", new String[] { ".sql" });
48+
LoadableResource migration = resourceProvider.getResource("V1__init.sql");
49+
assertThat(migrations).containsExactly(migration);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)