Skip to content

Commit

Permalink
Merge pull request #39816 from juanjogv/main
Browse files Browse the repository at this point in the history
Improvement in liquibase and liquibase-mongodb search-path handling in ResourceAccessor
  • Loading branch information
geoand authored Apr 2, 2024
2 parents 9ca43be + b88dcfa commit e00af30
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,36 @@ public LiquibaseMongodbFactory(LiquibaseMongodbConfig config,

private ResourceAccessor resolveResourceAccessor() throws FileNotFoundException {

if (liquibaseMongodbBuildTimeConfig.changeLog.startsWith("classpath:")) {
return new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader());
}

if (!liquibaseMongodbBuildTimeConfig.changeLog.startsWith("filesystem:") &&
liquibaseMongodbBuildTimeConfig.searchPath.size() == 1 &&
liquibaseMongodbBuildTimeConfig.searchPath.get(0).equals("/")) {
return new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader());
}

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

for (String searchPath : liquibaseMongodbBuildTimeConfig.searchPath) {
if (!liquibaseMongodbBuildTimeConfig.changeLog.startsWith("filesystem:")
&& liquibaseMongodbBuildTimeConfig.searchPath.isEmpty()) {
return compositeResourceAccessor;
}

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

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

return compositeResourceAccessor;
}

private String parseChangeLog(String changeLog) {

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

if (changeLog.startsWith("filesystem:")) {
return StringUtil.changePrefix(changeLog, "filesystem:", "");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.liquibase.mongodb.runtime;

import java.util.List;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
Expand All @@ -21,6 +22,6 @@ public class LiquibaseMongodbBuildTimeConfig {
/**
* The search path for DirectoryResourceAccessor
*/
@ConfigItem(defaultValue = "/")
public List<String> searchPath;
@ConfigItem
public Optional<List<String>> searchPath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,34 @@ public LiquibaseFactory(LiquibaseConfig config, DataSource datasource, String da

private ResourceAccessor resolveResourceAccessor() throws FileNotFoundException {

if (config.changeLog.startsWith("classpath:")) {
return new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader());
}

if (!config.changeLog.startsWith("filesystem:") &&
config.searchPath.size() == 1 &&
config.searchPath.get(0).equals("/")) {
return new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader());
}

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

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

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

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

return compositeResourceAccessor;
}

private String parseChangeLog(String changeLog) {

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

if (changeLog.startsWith("filesystem:")) {
return StringUtil.changePrefix(changeLog, "filesystem:", "");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.liquibase.runtime;

import static io.quarkus.liquibase.runtime.LiquibaseDataSourceBuildTimeConfig.DEFAULT_CHANGE_LOG;
import static io.quarkus.liquibase.runtime.LiquibaseDataSourceBuildTimeConfig.DEFAULT_SEARCH_PATH;
import static io.quarkus.liquibase.runtime.LiquibaseDataSourceRuntimeConfig.DEFAULT_LOCK_TABLE;
import static io.quarkus.liquibase.runtime.LiquibaseDataSourceRuntimeConfig.DEFAULT_LOG_TABLE;

Expand All @@ -22,7 +21,7 @@ public class LiquibaseConfig {
/**
* The search path for DirectoryResourceAccessor
*/
public List<String> searchPath = List.of(DEFAULT_SEARCH_PATH);
public Optional<List<String>> searchPath = Optional.empty();

/**
* The migrate at start flag
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.liquibase.runtime;

import java.util.List;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
Expand All @@ -12,7 +13,6 @@
public final class LiquibaseDataSourceBuildTimeConfig {

static final String DEFAULT_CHANGE_LOG = "db/changeLog.xml";
static final String DEFAULT_SEARCH_PATH = "/";

/**
* Creates a {@link LiquibaseDataSourceBuildTimeConfig} with default settings.
Expand All @@ -22,7 +22,7 @@ public final class LiquibaseDataSourceBuildTimeConfig {
public static final LiquibaseDataSourceBuildTimeConfig defaultConfig() {
LiquibaseDataSourceBuildTimeConfig defaultConfig = new LiquibaseDataSourceBuildTimeConfig();
defaultConfig.changeLog = DEFAULT_CHANGE_LOG;
defaultConfig.searchPath = List.of(DEFAULT_SEARCH_PATH);
defaultConfig.searchPath = Optional.empty();
return defaultConfig;
}

Expand All @@ -35,6 +35,6 @@ public static final LiquibaseDataSourceBuildTimeConfig defaultConfig() {
/**
* The search path for DirectoryResourceAccessor
*/
@ConfigItem(defaultValue = "/")
public List<String> searchPath;
@ConfigItem
public Optional<List<String>> searchPath;
}

0 comments on commit e00af30

Please sign in to comment.