From 7297a5f8ca81851129ee9ebeb092a9b0762bebf8 Mon Sep 17 00:00:00 2001 From: Juan Jose Garcia Date: Fri, 29 Mar 2024 12:45:12 -0500 Subject: [PATCH] Change to receive files outside the classpath Changing contais to startsWith Implementing CompositeResourceAccessor and the search-path property Setting default value of the search path and an error in Paths.get() Improving validation with search path default value making suggested improvements formatting code sorting imports Cleaning code --- .../mongodb/LiquibaseMongodbFactory.java | 45 +++++++++++++++++-- .../LiquibaseMongodbBuildTimeConfig.java | 8 ++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java b/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java index bbe7cd8579a7f..0a994f5066d58 100644 --- a/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java +++ b/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java @@ -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; @@ -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 { @@ -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. @@ -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 entry : liquibaseMongodbConfig.changeLogParameters.entrySet()) { liquibase.getChangeLogParameters().set(entry.getKey(), entry.getValue()); diff --git a/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/runtime/LiquibaseMongodbBuildTimeConfig.java b/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/runtime/LiquibaseMongodbBuildTimeConfig.java index 08bdc6b5363bb..9267af734848b 100644 --- a/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/runtime/LiquibaseMongodbBuildTimeConfig.java +++ b/extensions/liquibase-mongodb/runtime/src/main/java/io/quarkus/liquibase/mongodb/runtime/LiquibaseMongodbBuildTimeConfig.java @@ -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; @@ -15,4 +17,10 @@ public class LiquibaseMongodbBuildTimeConfig { */ @ConfigItem(defaultValue = "db/changeLog.xml") public String changeLog; + + /** + * The search path for DirectoryResourceAccessor + */ + @ConfigItem(defaultValue = "/") + public List searchPath; }