Skip to content

Commit 345edb1

Browse files
Mazizwilkinsona
authored andcommitted
Fix Flyway 10 in a GraalVM native image
See gh-40821
1 parent 244df31 commit 345edb1

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

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

19+
import java.lang.reflect.Constructor;
20+
import java.lang.reflect.InvocationTargetException;
1921
import java.util.Arrays;
2022

2123
import org.flywaydb.core.api.configuration.FluentConfiguration;
@@ -24,26 +26,63 @@
2426
import org.flywaydb.core.internal.scanner.ResourceNameCache;
2527
import org.flywaydb.core.internal.scanner.Scanner;
2628

29+
import org.springframework.util.ClassUtils;
30+
2731
/**
2832
* Registers {@link NativeImageResourceProvider} as a Flyway
2933
* {@link org.flywaydb.core.api.ResourceProvider}.
3034
*
3135
* @author Moritz Halbritter
36+
* @author Maziz Esa
3237
*/
3338
class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer {
3439

3540
@Override
3641
public void customize(FluentConfiguration configuration) {
3742
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());
43+
final var scanner = getFlyway9OrFallbackTo10ScannerObject(configuration);
4244
NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner,
4345
configuration.getClassLoader(), Arrays.asList(configuration.getLocations()),
4446
configuration.getEncoding(), configuration.isFailOnMissingLocations());
4547
configuration.resourceProvider(resourceProvider);
4648
}
4749
}
4850

51+
private static Scanner getFlyway9OrFallbackTo10ScannerObject(FluentConfiguration configuration) {
52+
Scanner scanner;
53+
try {
54+
scanner = getFlyway9Scanner(configuration);
55+
}
56+
catch (NoSuchMethodError noSuchMethodError) {
57+
// happens when scanner is flyway version 10, which the constructor accepts
58+
// different number of parameters.
59+
scanner = getFlyway10Scanner(configuration);
60+
}
61+
return scanner;
62+
}
63+
64+
private static Scanner getFlyway10Scanner(FluentConfiguration configuration) {
65+
final Constructor<?> scannerConstructor;
66+
final Scanner scanner;
67+
try {
68+
scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null)
69+
.getDeclaredConstructors()[0];
70+
scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(),
71+
new LocationScannerCache(), configuration);
72+
}
73+
catch (ClassNotFoundException | InstantiationException | IllegalAccessException
74+
| InvocationTargetException ex) {
75+
throw new RuntimeException(ex);
76+
}
77+
return scanner;
78+
}
79+
80+
private static Scanner getFlyway9Scanner(FluentConfiguration configuration) {
81+
Scanner scanner;
82+
scanner = new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()),
83+
configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(), false,
84+
new ResourceNameCache(), new LocationScannerCache(), configuration.isFailOnMissingLocations());
85+
return scanner;
86+
}
87+
4988
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2023 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
36+
*/
37+
@ClassPathOverrides("org.flywaydb:flyway-core:10.12.0")
38+
class Flyway10NativeImageResourceProviderCustomizerTests {
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)