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

Change to receive files outside the classpath in mongodb liquibase #39680

Merged
merged 1 commit into from
Mar 29, 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
@@ -1,5 +1,7 @@
package io.quarkus.liquibase.mongodb;

import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -8,12 +10,16 @@
import io.quarkus.liquibase.mongodb.runtime.LiquibaseMongodbBuildTimeConfig;
import io.quarkus.liquibase.mongodb.runtime.LiquibaseMongodbConfig;
import io.quarkus.mongodb.runtime.MongoClientConfig;
import io.quarkus.runtime.util.StringUtil;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.CompositeResourceAccessor;
import liquibase.resource.DirectoryResourceAccessor;
import liquibase.resource.ResourceAccessor;

public class LiquibaseMongodbFactory {

Expand All @@ -32,9 +38,42 @@ public LiquibaseMongodbFactory(LiquibaseMongodbConfig config,
this.mongoClientConfig = mongoClientConfig;
}

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();

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

return compositeResourceAccessor;
}

private String parseChangeLog(String changeLog) {
if (changeLog.startsWith("filesystem:")) {
return StringUtil.changePrefix(changeLog, "filesystem:", "");
}

if (changeLog.startsWith("classpath:")) {
return StringUtil.changePrefix(changeLog, "classpath:", "");
}

return changeLog;
}

public Liquibase createLiquibase() {
try (ClassLoaderResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(
Thread.currentThread().getContextClassLoader())) {
try (ResourceAccessor resourceAccessor = resolveResourceAccessor()) {
String parsedChangeLog = parseChangeLog(liquibaseMongodbBuildTimeConfig.changeLog);
String connectionString = this.mongoClientConfig.connectionString.orElse("mongodb://localhost:27017");

// Every MongoDB client configuration must be added to the connection string, we didn't add all as it would be too much to support.
Expand Down Expand Up @@ -83,7 +122,7 @@ public Liquibase createLiquibase() {
database.setDefaultSchemaName(liquibaseMongodbConfig.defaultSchemaName.get());
}
}
Liquibase liquibase = new Liquibase(liquibaseMongodbBuildTimeConfig.changeLog, resourceAccessor, database);
Liquibase liquibase = new Liquibase(parsedChangeLog, resourceAccessor, database);

for (Map.Entry<String, String> entry : liquibaseMongodbConfig.changeLogParameters.entrySet()) {
liquibase.getChangeLogParameters().set(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.liquibase.mongodb.runtime;

import java.util.List;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
Expand All @@ -15,4 +17,10 @@ public class LiquibaseMongodbBuildTimeConfig {
*/
@ConfigItem(defaultValue = "db/changeLog.xml")
public String changeLog;

/**
* The search path for DirectoryResourceAccessor
*/
@ConfigItem(defaultValue = "/")
public List<String> searchPath;
}