Peter Butkovic
Peter Butkovic

Reputation: 12179

Using Liquibase Mongodb extension with Quarkus

Trying to use liquibase-mongodb extension with Quarkus. Without any sucess. Anyone able to guide me to some working example?

application.yaml contents:

quarkus:
  mongodb:
    connection-string: mongodb://localhost:27017
    write-concern:
      journal: false
    database: foo1
  liquibase:
    migrate-at-start: true
    change-log: db/changeLog.yaml

db/changeLog.yaml contents:

databaseChangeLog:
  - include:
    file: changesets/foo.json

build.gradle contains:


    implementation "io.quarkus:quarkus-liquibase"
    implementation "org.liquibase.ext:liquibase-mongodb:${liquibaseVersion}"
    implementation "org.mongodb:mongodb-driver-sync:${mongodbVersion}"

output:

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Slf4jLoggerFactory]
__  ____  __  _____   ___  __ ____  ______ 
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
2021-03-07 12:26:39,254 WARN  [io.qua.dep.QuarkusAugmentor] (main) Using Java versions older than 11 to build Quarkus applications is deprecated and will be disallowed in a future release!
2021-03-07 12:26:39,573 WARN  [io.qua.agr.dep.AgroalProcessor] (build-24) The Agroal dependency is present but no JDBC datasources have been defined.
2021-03-07 12:26:40,583 INFO  [org.mon.dri.cluster] (Quarkus Main Thread) Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-03-07 12:26:40,595 INFO  [org.mon.dri.cluster] (Quarkus Main Thread) Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-03-07 12:26:40,617 INFO  [org.mon.dri.connection] (cluster-rtt-ClusterId{value='6044b870c8687d11c71dfb0b', description='null'}-localhost:27017) Opened connection [connectionId{localValue:1, serverValue:5}] to localhost:27017
2021-03-07 12:26:40,617 INFO  [org.mon.dri.connection] (cluster-ClusterId{value='6044b870c8687d11c71dfb0b', description='null'}-localhost:27017) Opened connection [connectionId{localValue:2, serverValue:6}] to localhost:27017
2021-03-07 12:26:40,617 INFO  [org.mon.dri.cluster] (cluster-ClusterId{value='6044b870c8687d11c71dfb0b', description='null'}-localhost:27017) Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=9, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=11510711}
2021-03-07 12:26:40,620 INFO  [org.mon.dri.connection] (cluster-ClusterId{value='6044b870c8687d11c71dfb0c', description='null'}-localhost:27017) Opened connection [connectionId{localValue:3, serverValue:8}] to localhost:27017
2021-03-07 12:26:40,620 INFO  [org.mon.dri.cluster] (cluster-ClusterId{value='6044b870c8687d11c71dfb0c', description='null'}-localhost:27017) Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=9, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3728082}
2021-03-07 12:26:40,621 INFO  [org.mon.dri.connection] (cluster-rtt-ClusterId{value='6044b870c8687d11c71dfb0c', description='null'}-localhost:27017) Opened connection [connectionId{localValue:4, serverValue:7}] to localhost:27017
2021-03-07 12:26:40,704 INFO  [io.quarkus] (Quarkus Main Thread) foo-app 0.0.1-SNAPSHOT on JVM (powered by Quarkus 1.11.3.Final) started in 1.524s. Listening on: http://localhost:8080
2021-03-07 12:26:40,705 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2021-03-07 12:26:40,705 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, config-yaml, liquibase, mongodb-client, mongodb-panache, mongodb-rest-data-panache, mutiny, narayana-jta, resteasy, resteasy-jackson, smallrye-context-propagation, smallrye-openapi, swagger-ui]

so liquibase is known to quarkus, but mongodb changesets are not executed.

Upvotes: 0

Views: 1328

Answers (2)

Peter Butkovic
Peter Butkovic

Reputation: 12179

Due to lack of official support, ended up with custom implementation, that is all I needed:


import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import io.quarkus.runtime.StartupEvent;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.ext.mongodb.database.MongoLiquibaseDatabase;
import liquibase.resource.ClassLoaderResourceAccessor;
import lombok.SneakyThrows;

@ApplicationScoped
public class MongoDBMigration {

    @ConfigProperty(name = "quarkus.mongodb.connection-string")
    String connectionString;

    @ConfigProperty(name = "quarkus.mongodb.credentials.username")
    Optional<String> username;

    @ConfigProperty(name = "quarkus.mongodb.credentials.password")
    Optional<String> password;

    @ConfigProperty(name = "quarkus.liquibase.migrate-at-start")
    boolean liquibaseEnabled;

    @SneakyThrows
    void onStart(@Observes StartupEvent ev) {
        if (liquibaseEnabled) {
            Database database = (MongoLiquibaseDatabase) DatabaseFactory.getInstance().openDatabase(connectionString, username.orElse(null), password.orElse(null), null, null);
            Liquibase liquiBase = new Liquibase("db/changeLog.json", new ClassLoaderResourceAccessor(), database);
            liquiBase.update("");
        }
    }
}

having application.yaml:

quarkus:
  mongodb:
    connection-string: mongodb://localhost:27017/foo?socketTimeoutMS=1000&connectTimeoutMS=1000&serverSelectionTimeoutMS=1000
    write-concern:
      journal: false
    database: foo
  liquibase:
    migrate-at-start: true
    change-log: db/changeLog.xml

and build.gradle:

dependencies {
    implementation "io.quarkus:quarkus-liquibase"
    implementation "org.liquibase.ext:liquibase-mongodb:${liquibaseVersion}"
    implementation "org.mongodb:mongodb-driver-sync:${mongodbVersion}"

Upvotes: 0

Guillaume Smet
Guillaume Smet

Reputation: 10539

The Quarkus Liquibase extension only targets JDBC datasources for now.

Probably worth opening an enhancement request in our tracker so that we track this need, if it hasn't already been done.

Upvotes: 1

Related Questions