Skip to content

Use the quarkus MongoClient in liquibase-mongodb extension #46312

@Malandril

Description

@Malandril

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions