From 6f7790a8f100f167c246df4b80cb6456ec437de0 Mon Sep 17 00:00:00 2001 From: Juan Jose Garcia Date: Fri, 29 Mar 2024 13:18:40 -0500 Subject: [PATCH 1/2] Uploading dynamic resource accessor implementation according to changelog location uploading an adjustment adding ClassLoaderResourceAccessor to the compositeResourceAccessor formatting code --- .../quarkus/liquibase/LiquibaseFactory.java | 49 +++++++++++++++++-- .../liquibase/runtime/LiquibaseConfig.java | 6 +++ .../liquibase/runtime/LiquibaseCreator.java | 1 + .../LiquibaseDataSourceBuildTimeConfig.java | 9 ++++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/LiquibaseFactory.java b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/LiquibaseFactory.java index 00b34a5af882c..dd865652bb7e3 100644 --- a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/LiquibaseFactory.java +++ b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/LiquibaseFactory.java @@ -1,10 +1,13 @@ package io.quarkus.liquibase; +import java.io.FileNotFoundException; +import java.nio.file.Paths; import java.util.Map; import javax.sql.DataSource; import io.quarkus.liquibase.runtime.LiquibaseConfig; +import io.quarkus.runtime.util.StringUtil; import liquibase.Contexts; import liquibase.LabelExpression; import liquibase.Liquibase; @@ -12,6 +15,9 @@ import liquibase.database.DatabaseFactory; import liquibase.database.jvm.JdbcConnection; import liquibase.resource.ClassLoaderResourceAccessor; +import liquibase.resource.CompositeResourceAccessor; +import liquibase.resource.DirectoryResourceAccessor; +import liquibase.resource.ResourceAccessor; public class LiquibaseFactory { @@ -27,13 +33,48 @@ public LiquibaseFactory(LiquibaseConfig config, DataSource datasource, String da this.dataSourceName = dataSourceName; } + 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) { + 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(config.changeLog); Database database = DatabaseFactory.getInstance() .findCorrectDatabaseImplementation(new JdbcConnection(dataSource.getConnection())); - ; + if (database != null) { database.setDatabaseChangeLogLockTableName(config.databaseChangeLogLockTableName); database.setDatabaseChangeLogTableName(config.databaseChangeLogTableName); @@ -48,7 +89,7 @@ public Liquibase createLiquibase() { database.setDefaultSchemaName(config.defaultSchemaName.get()); } } - Liquibase liquibase = new Liquibase(config.changeLog, resourceAccessor, database); + Liquibase liquibase = new Liquibase(parsedChangeLog, resourceAccessor, database); for (Map.Entry entry : config.changeLogParameters.entrySet()) { liquibase.getChangeLogParameters().set(entry.getKey(), entry.getValue()); diff --git a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseConfig.java b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseConfig.java index 39923b207f611..b82a69fdf55a4 100644 --- a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseConfig.java +++ b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseConfig.java @@ -1,6 +1,7 @@ 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; @@ -18,6 +19,11 @@ public class LiquibaseConfig { */ public String changeLog = DEFAULT_CHANGE_LOG; + /** + * The search path for DirectoryResourceAccessor + */ + public List searchPath = List.of(DEFAULT_SEARCH_PATH); + /** * The migrate at start flag */ diff --git a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseCreator.java b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseCreator.java index 785425e30b1cd..c3d20635e8612 100644 --- a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseCreator.java +++ b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseCreator.java @@ -18,6 +18,7 @@ public LiquibaseCreator(LiquibaseDataSourceRuntimeConfig liquibaseRuntimeConfig, public LiquibaseFactory createLiquibaseFactory(DataSource dataSource, String dataSourceName) { LiquibaseConfig config = new LiquibaseConfig(); config.changeLog = liquibaseBuildTimeConfig.changeLog; + config.searchPath = liquibaseBuildTimeConfig.searchPath; config.changeLogParameters = liquibaseRuntimeConfig.changeLogParameters; if (liquibaseRuntimeConfig.labels.isPresent()) { diff --git a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseDataSourceBuildTimeConfig.java b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseDataSourceBuildTimeConfig.java index 02be6092e80cc..47e7fc0000c34 100644 --- a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseDataSourceBuildTimeConfig.java +++ b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseDataSourceBuildTimeConfig.java @@ -1,5 +1,7 @@ package io.quarkus.liquibase.runtime; +import java.util.List; + import io.quarkus.runtime.annotations.ConfigGroup; import io.quarkus.runtime.annotations.ConfigItem; @@ -10,6 +12,7 @@ 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. @@ -19,6 +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); return defaultConfig; } @@ -28,4 +32,9 @@ public static final LiquibaseDataSourceBuildTimeConfig defaultConfig() { @ConfigItem(defaultValue = DEFAULT_CHANGE_LOG) public String changeLog; + /** + * The search path for DirectoryResourceAccessor + */ + @ConfigItem(defaultValue = "/") + public List searchPath; } From 5c686ddd345612e751e6639916763027c909c0f1 Mon Sep 17 00:00:00 2001 From: Juan Jose Garcia Date: Mon, 1 Apr 2024 08:07:11 -0500 Subject: [PATCH 2/2] Adding the classpath to the CompositeResourceAccessor searchpath Added the classpath to the CompositeResourceAccessor of LiquibaseMongodbFactory, this way even if you specify a custom searchpath you will still be able to reference some file inside the resources folder. --- .../io/quarkus/liquibase/mongodb/LiquibaseMongodbFactory.java | 2 ++ 1 file changed, 2 insertions(+) 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 0a994f5066d58..ef8819d730185 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 @@ -51,6 +51,8 @@ private ResourceAccessor resolveResourceAccessor() throws FileNotFoundException } CompositeResourceAccessor compositeResourceAccessor = new CompositeResourceAccessor(); + compositeResourceAccessor + .addResourceAccessor(new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader())); for (String searchPath : liquibaseMongodbBuildTimeConfig.searchPath) { compositeResourceAccessor.addResourceAccessor(new DirectoryResourceAccessor(Paths.get(searchPath)));