Skip to content

Commit

Permalink
Merge pull request #42095 from gcw-it/pr_16292
Browse files Browse the repository at this point in the history
Enable Liquibase includeAll in Native Image
  • Loading branch information
zakkak authored Jul 26, 2024
2 parents 0b942a9 + bf6232b commit f7dee26
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import io.agroal.api.AgroalDataSource;
import io.quarkus.liquibase.runtime.LiquibaseConfig;
import io.quarkus.liquibase.runtime.NativeImageResourceAccessor;
import io.quarkus.runtime.ImageMode;
import io.quarkus.runtime.ResettableSystemProperties;
import io.quarkus.runtime.util.StringUtil;
import liquibase.Contexts;
Expand Down Expand Up @@ -38,33 +40,44 @@ public LiquibaseFactory(LiquibaseConfig config, DataSource datasource, String da
}

private ResourceAccessor resolveResourceAccessor() throws FileNotFoundException {
var rootAccessor = new CompositeResourceAccessor();
return ImageMode.current().isNativeImage()
? nativeImageResourceAccessor(rootAccessor)
: defaultResourceAccessor(rootAccessor);
}

private ResourceAccessor defaultResourceAccessor(CompositeResourceAccessor rootAccessor)
throws FileNotFoundException {

CompositeResourceAccessor compositeResourceAccessor = new CompositeResourceAccessor();
compositeResourceAccessor
.addResourceAccessor(new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader()));
rootAccessor.addResourceAccessor(
new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader()));

if (!config.changeLog.startsWith("filesystem:") && config.searchPath.isEmpty()) {
return compositeResourceAccessor;
return rootAccessor;
}

if (config.searchPath.isEmpty()) {
compositeResourceAccessor.addResourceAccessor(
return rootAccessor.addResourceAccessor(
new DirectoryResourceAccessor(
Paths.get(StringUtil.changePrefix(config.changeLog, "filesystem:", "")).getParent()));
return compositeResourceAccessor;
Paths.get(StringUtil
.changePrefix(config.changeLog, "filesystem:", ""))
.getParent()));
}

for (String searchPath : config.searchPath.get()) {
compositeResourceAccessor.addResourceAccessor(new DirectoryResourceAccessor(Paths.get(searchPath)));
rootAccessor.addResourceAccessor(new DirectoryResourceAccessor(Paths.get(searchPath)));
}
return rootAccessor;
}

return compositeResourceAccessor;
private ResourceAccessor nativeImageResourceAccessor(CompositeResourceAccessor rootAccessor) {
return rootAccessor.addResourceAccessor(new NativeImageResourceAccessor());
}

private String parseChangeLog(String changeLog) {

if (changeLog.startsWith("filesystem:") && config.searchPath.isEmpty()) {
return Paths.get(StringUtil.changePrefix(changeLog, "filesystem:", "")).getFileName().toString();
return Paths.get(StringUtil.changePrefix(changeLog, "filesystem:", ""))
.getFileName().toString();
}

if (changeLog.startsWith("filesystem:")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.liquibase.runtime;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import org.jboss.logging.Logger;

import liquibase.resource.AbstractPathResourceAccessor;
import liquibase.resource.PathResource;
import liquibase.resource.Resource;

public class NativeImageResourceAccessor extends AbstractPathResourceAccessor {
private static final URI NATIVE_IMAGE_FILESYSTEM_URI = URI.create("resource:/");
private static final Logger log = Logger.getLogger(NativeImageResourceAccessor.class);

private final FileSystem fileSystem;

public NativeImageResourceAccessor() {
FileSystem fs;
try {
fs = FileSystems.newFileSystem(
NATIVE_IMAGE_FILESYSTEM_URI,
Collections.singletonMap("create", "true"));
log.debug("Creating new filesystem for native image");
} catch (FileSystemAlreadyExistsException ex) {
fs = FileSystems.getFileSystem(NATIVE_IMAGE_FILESYSTEM_URI);
log.debug("Native image file system already exists", ex);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
fileSystem = fs;
}

@Override
protected Path getRootPath() {
return fileSystem.getPath("/");
}

@Override
protected Resource createResource(Path file, String pathToAdd) {
return new PathResource(pathToAdd, file);
}

@Override
public void close() {
}

@Override
public List<String> describeLocations() {
return Collections.singletonList(fileSystem.toString());
}

@Override
public String toString() {
return getClass().getName() + " (" + getRootPath() + ") (" + fileSystem.toString() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@

<include relativeToChangelogFile="true" file="create-views.xml" />

<!-- errorIfMissingOrEmpty to avoid hard error in native,
see also: LiquibaseFunctionalityNativeIT.isIncludeAllExpectedToWork() -->
<includeAll relativeToChangelogFile="true" path="includeAll" errorIfMissingOrEmpty="false" />
<includeAll relativeToChangelogFile="true" path="includeAll" />
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,4 @@

@QuarkusIntegrationTest
public class LiquibaseFunctionalityNativeIT extends LiquibaseFunctionalityTest {

// see: https://github.com/quarkusio/quarkus/issues/16292
// if this is ever resolved, make sure to remove errorIfMissingOrEmpty="false" from includeAll in changeLog.xml
@Override
protected boolean isIncludeAllExpectedToWork() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class LiquibaseFunctionalityPMT {

@Test
public void test() {
LiquibaseFunctionalityTest.doTestLiquibaseQuarkusFunctionality(true);
LiquibaseFunctionalityTest.doTestLiquibaseQuarkusFunctionality();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class LiquibaseFunctionalityTest {
@Test
@DisplayName("Migrates a schema correctly using integrated instance")
public void testLiquibaseQuarkusFunctionality() {
doTestLiquibaseQuarkusFunctionality(isIncludeAllExpectedToWork());
doTestLiquibaseQuarkusFunctionality();
}

@Test
Expand All @@ -25,21 +25,17 @@ public void testLiquibaseUsingDedicatedUsernameAndPassword() {
"ADMIN"));
}

static void doTestLiquibaseQuarkusFunctionality(boolean isIncludeAllExpectedToWork) {
static void doTestLiquibaseQuarkusFunctionality() {
when()
.get("/liquibase/update")
.then()
.body(is(
"create-tables-1,test-1,create-view-inline,create-view-file-abs,create-view-file-rel,"
+ (isIncludeAllExpectedToWork ? "includeAll-1,includeAll-2," : "")
+ ("includeAll-1,includeAll-2,")
+ "json-create-tables-1,json-test-1,"
+ "sql-create-tables-1,sql-test-1,"
+ "yaml-create-tables-1,yaml-test-1,"
+ "00000000000000,00000000000001,00000000000002,"
+ "1613578374533-1,1613578374533-2,1613578374533-3"));
}

protected boolean isIncludeAllExpectedToWork() {
return true;
}
}

0 comments on commit f7dee26

Please sign in to comment.