Skip to content

Use the quarkus MongoClient in liquibase-mongodb extension #46312

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

Closed
Malandril opened this issue Feb 17, 2025 · 4 comments · Fixed by #46326
Closed

Use the quarkus MongoClient in liquibase-mongodb extension #46312

Malandril opened this issue Feb 17, 2025 · 4 comments · Fixed by #46326

Comments

@Malandril
Copy link
Contributor

Malandril commented Feb 17, 2025

Description

When using liquibase-mongodb extension currently it does not reuse the quarkus generated MongoClient.

For example the MongoClientCustomizer have no effects on the mongo clients liquibase use.
This creates some unexpected inconsistencies.
Also quarkus has to rebuild the connection string from the parameters which can create some problems.

Allowing to reuse the quarkus mongo client in the quarkus liquibase extension would allow to fix those issues.

This would also allow to reuse the tls registry settings from #46293.

Implementation ideas

It could be done by implementing a custom MongoClientDriver that injects the quarkus MongoClientsand passing it to liquibase.
For example the following liquibase driver extending MongoClientDriver:

public class QuarkusMongoClientDriver extends MongoClientDriver {
    private static final Logger log = LoggerFactory.getLogger(QuarkusMongoClientDriver.class);

    public MongoClient connect(ConnectionString connectionString) throws DatabaseException {
        try {
            Optional<MongoClients> clients = Arc.container().select(MongoClients.class).stream().findFirst();
            if (clients.isPresent()) {
                log.info("Using quarkus mongo client");
                MongoClients mongoClients = clients.get();
                return mongoClients.createMongoClient(MongoClientBeanUtil.DEFAULT_MONGOCLIENT_NAME);
            }
            return com.mongodb.client.MongoClients.create(connectionString);
        } catch (Exception e) {
            throw new DatabaseException(
                    "Connection could not be established to: " + connectionString.getConnectionString(), e);
        }
    }
}

And then using it in the LiquibaseMongodbFactory:

            Database database = DatabaseFactory.getInstance().openDatabase(connectionString,
                    this.mongoClientConfig.credentials().username().orElse(null),
                    this.mongoClientConfig.credentials().password().orElse(null),
                    QuarkusMongoClientDriver.class.getName(),
                    null,
                    null,
                    null,
                    resourceAccessor);

This is a simple example that works with the default client.
It works with the JVM quarkus liquibase mongodb integrations tests, but not the native ones some more configuration is needed.

Copy link

quarkus-bot bot commented Feb 17, 2025

/cc @andrejpetras (liquibase), @geoand (liquibase), @gsmet (liquibase), @loicmathieu (mongodb)

@geoand
Copy link
Contributor

geoand commented Feb 17, 2025

We should definitely do this!

@geoand
Copy link
Contributor

geoand commented Feb 18, 2025

@Malandril do you want to open a PR for this?

@Malandril
Copy link
Contributor Author

I created a draft PR #46326 i used a different way to set the mongo client to liquibase which i think is simpler

@quarkus-bot quarkus-bot bot added this to the 3.21 - main milestone Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants