Skip to content

Commit bd0c12a

Browse files
committed
Preserve nested Flyway migration paths in native images
Preserve the classpath-relative path for Flyway migrations discovered by the native image resource provider so nested migrations remain readable. See gh-50422 Signed-off-by: Dongliang Xie <dragonfsky@gmail.com>
1 parent a329d70 commit bd0c12a

2 files changed

Lines changed: 89 additions & 5 deletions

File tree

module/spring-boot-flyway/src/main/java/org/springframework/boot/flyway/autoconfigure/NativeImageResourceProvider.java

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.UncheckedIOException;
21+
import java.net.URI;
2122
import java.nio.charset.Charset;
2223
import java.util.ArrayList;
2324
import java.util.Collection;
@@ -46,6 +47,7 @@
4647
* {@link PathMatchingResourcePatternResolver} to find migration files in a native image.
4748
*
4849
* @author Moritz Halbritter
50+
* @author Dongliang Xie
4951
*/
5052
class NativeImageResourceProvider implements ResourceProvider {
5153

@@ -106,9 +108,8 @@ public Collection<LoadableResource> getResources(String prefix, String[] suffixe
106108
}
107109

108110
private ClassPathResource asClassPathResource(LocatedResource locatedResource) {
109-
Location location = locatedResource.location();
110-
String fileNameWithAbsolutePath = location.getRootPath() + "/" + locatedResource.resource().getFilename();
111-
return new ClassPathResource(location, fileNameWithAbsolutePath, this.classLoader, this.encoding);
111+
return new ClassPathResource(locatedResource.location(), locatedResource.path(), this.classLoader,
112+
this.encoding);
112113
}
113114

114115
private void ensureInitialized() {
@@ -140,11 +141,60 @@ private void initialize() {
140141
}
141142
Resource[] resources = getResources(resolver, location, root);
142143
for (Resource resource : resources) {
143-
this.locatedResources.add(new LocatedResource(resource, location));
144+
this.locatedResources
145+
.add(new LocatedResource(resource, location, getClassPathResourcePath(location, root, resource)));
144146
}
145147
}
146148
}
147149

150+
private String getClassPathResourcePath(Location location, Resource root, Resource resource) {
151+
if (resource instanceof org.springframework.core.io.ClassPathResource classPathResource) {
152+
return classPathResource.getPath();
153+
}
154+
String rootPath = location.getRootPath();
155+
String resourcePath = getResourcePathRelativeToRoot(root, resource, rootPath);
156+
return (rootPath.isEmpty()) ? resourcePath : rootPath + "/" + resourcePath;
157+
}
158+
159+
private String getResourcePathRelativeToRoot(Resource root, Resource resource, String rootPath) {
160+
try {
161+
URI rootUri = root.getURI();
162+
URI resourceUri = resource.getURI();
163+
String relativePath = getRelativePath(rootUri, resourceUri);
164+
if (relativePath != null) {
165+
return relativePath;
166+
}
167+
String path = getUriPath(resourceUri);
168+
if (!rootPath.isEmpty()) {
169+
int rootPathIndex = path.indexOf(rootPath + "/");
170+
if (rootPathIndex != -1) {
171+
return path.substring(rootPathIndex + rootPath.length() + 1);
172+
}
173+
}
174+
String filename = resource.getFilename();
175+
return (filename != null) ? filename : path;
176+
}
177+
catch (IOException ex) {
178+
throw new UncheckedIOException("Failed to determine path for " + resource, ex);
179+
}
180+
}
181+
182+
private @Nullable String getRelativePath(URI rootUri, URI resourceUri) {
183+
String rootPath = asDirectoryPath(rootUri);
184+
String resourcePath = getUriPath(resourceUri);
185+
return (resourcePath.startsWith(rootPath)) ? resourcePath.substring(rootPath.length()) : null;
186+
}
187+
188+
private String asDirectoryPath(URI uri) {
189+
String path = getUriPath(uri);
190+
return (path.endsWith("/")) ? path : path + "/";
191+
}
192+
193+
private String getUriPath(URI uri) {
194+
String path = uri.getPath();
195+
return (path != null) ? path : uri.toString();
196+
}
197+
148198
private Resource[] getResources(PathMatchingResourcePatternResolver resolver, Location location, Resource root) {
149199
try {
150200
return resolver.getResources(root.getURI() + "/**/*");
@@ -154,7 +204,7 @@ private Resource[] getResources(PathMatchingResourcePatternResolver resolver, Lo
154204
}
155205
}
156206

157-
private record LocatedResource(Resource resource, Location location) {
207+
private record LocatedResource(Resource resource, Location location, String path) {
158208

159209
}
160210

module/spring-boot-flyway/src/test/java/org/springframework/boot/flyway/autoconfigure/NativeImageResourceProviderCustomizerTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,34 @@
1616

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

19+
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
1921
import java.util.Collection;
22+
import java.util.Collections;
23+
import java.util.List;
2024

25+
import org.flywaydb.core.api.Location;
2126
import org.flywaydb.core.api.ResourceProvider;
2227
import org.flywaydb.core.api.configuration.FluentConfiguration;
2328
import org.flywaydb.core.api.resource.LoadableResource;
2429
import org.flywaydb.core.internal.resource.NoopResourceProvider;
30+
import org.flywaydb.core.internal.scanner.Scanner;
2531
import org.junit.jupiter.api.Test;
2632

33+
import org.springframework.boot.testsupport.classpath.ForkedClassPath;
2734
import org.springframework.boot.testsupport.classpath.resources.WithResource;
2835
import org.springframework.boot.testsupport.classpath.resources.WithResources;
36+
import org.springframework.util.FileCopyUtils;
2937

3038
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.mockito.BDDMockito.given;
40+
import static org.mockito.Mockito.mock;
3141

3242
/**
3343
* Tests for {@link NativeImageResourceProviderCustomizer}.
3444
*
3545
* @author Moritz Halbritter
46+
* @author Dongliang Xie
3647
*/
3748
class NativeImageResourceProviderCustomizerTests {
3849

@@ -70,6 +81,29 @@ void nativeImageResourceProviderShouldFindNestedMigrations() {
7081
assertThat(migrations).containsExactlyInAnyOrder(v1, v2);
7182
}
7283

84+
@Test
85+
@ForkedClassPath
86+
@WithResource(name = "db/migration/nested/V2__users.sql", content = "select 1;")
87+
void nativeImageResourceProviderShouldReadNestedMigrations() throws IOException {
88+
System.setProperty("org.graalvm.nativeimage.imagecode", "true");
89+
try {
90+
@SuppressWarnings("unchecked")
91+
Scanner<Object> scanner = mock(Scanner.class);
92+
given(scanner.getResources("V", ".sql")).willReturn(Collections.emptyList());
93+
Location location = Location.fromPath("classpath:", "db/migration");
94+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
95+
ResourceProvider resourceProvider = new NativeImageResourceProvider(scanner, classLoader, List.of(location),
96+
StandardCharsets.UTF_8, true);
97+
Collection<LoadableResource> migrations = resourceProvider.getResources("V", new String[] { ".sql" });
98+
assertThat(migrations).hasSize(1);
99+
LoadableResource migration = migrations.iterator().next();
100+
assertThat(FileCopyUtils.copyToString(migration.read())).isEqualTo("select 1;");
101+
}
102+
finally {
103+
System.clearProperty("org.graalvm.nativeimage.imagecode");
104+
}
105+
}
106+
73107
@Test
74108
void shouldBackOffOnCustomResourceProvider() {
75109
FluentConfiguration configuration = new FluentConfiguration();

0 commit comments

Comments
 (0)