Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement in liquibase and liquibase-mongodb search-path handling in ResourceAccessor #39816

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}