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

Receive files outside the classpath in quarkus-liquibase #39720

Merged
merged 2 commits into from
Apr 1, 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 @@ -51,6 +51,8 @@ private ResourceAccessor resolveResourceAccessor() throws FileNotFoundException
}

CompositeResourceAccessor compositeResourceAccessor = new CompositeResourceAccessor();
compositeResourceAccessor
.addResourceAccessor(new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader()));
geoand marked this conversation as resolved.
Show resolved Hide resolved

for (String searchPath : liquibaseMongodbBuildTimeConfig.searchPath) {
compositeResourceAccessor.addResourceAccessor(new DirectoryResourceAccessor(Paths.get(searchPath)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
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;
import liquibase.database.Database;
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 {

Expand All @@ -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);
Expand All @@ -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<String, String> entry : config.changeLogParameters.entrySet()) {
liquibase.getChangeLogParameters().set(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,6 +19,11 @@ public class LiquibaseConfig {
*/
public String changeLog = DEFAULT_CHANGE_LOG;

/**
* The search path for DirectoryResourceAccessor
*/
public List<String> searchPath = List.of(DEFAULT_SEARCH_PATH);

/**
* The migrate at start flag
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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.
Expand All @@ -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;
}

Expand All @@ -28,4 +32,9 @@ public static final LiquibaseDataSourceBuildTimeConfig defaultConfig() {
@ConfigItem(defaultValue = DEFAULT_CHANGE_LOG)
public String changeLog;

/**
* The search path for DirectoryResourceAccessor
*/
@ConfigItem(defaultValue = "/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually wondering if this is a good default... Does this mean it will start searching at the root of the file system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you check here Im validating that if it is set by default that / and if the changelog property does not have the prefix filesystem: it refers to classpath

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I saw that but I have to admit it's confusing. Anyway, let's leave it like this for now, but I assume that using Optional<List<String>> for searchPath would be better.

Copy link
Contributor Author

@juanjogv juanjogv Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it this way because at the time I could not find how to declare the default list as an empty list in the build time config, because if I put a default value of "" in the tag I got an error related to null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    /**
     * The search path for DirectoryResourceAccessor
     */
    @ConfigItem
    public Optional<List<String>> searchPath;

should work, no?

Copy link
Contributor Author

@juanjogv juanjogv Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try and made the change if u prefer it that way, no biggie for me. As this is my first time contributing in quarkus I am not very clear on how to do certain things and that was a solution that I found viable at the time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this and you can try and update both configs with my suggestion. If all goes well, you can open a new PR

public List<String> searchPath;
}