Quarkus Mongock

Mongock is a Java based migration tool as part of your application code for Distributed environments focused in managing changes for your favourite NoSQL databases.

Usage

To start using Mongock with your project, you just need to:

  • create ChangeUnits as you usually do with Mongock.

  • activate the migrate-at-start option to migrate automatically or inject the MongockFactory object and run your migration as you normally do.

Add the following dependency to your POM file:

<dependency>
    <groupId>io.quarkiverse.mongock</groupId>
    <artifactId>quarkus-mongock</artifactId>
    <version>0.2.0</version>
</dependency>

Mongock support relies on the Quarkus MongoDB client. First, you need to add the MongoDB client configuration to the application.properties file :

quarkus.mongodb.connection-string=mongodb://localhost:27017
quarkus.mongodb.database=test

# Optional, if you want to migrate automatically at startup
quarkus.mongock.migrate-at-start=true

Add ChangeUnits to the project, in the packages of your choice :

@ChangeUnit(id="myMigrationChangeUnitId", order = "001", author = "mongock_test", systemVersion = "1")
public class MyMigrationChangeUnit {
    private final MongoDatabase mongoDatabase;

    public MyMigrationChangeUnit(MongoDatabase mongoDatabase) {
        this.mongoDatabase = mongoDatabase;
    }

    @Execution
    public void migrationMethod() {
        mongoDatabase.getCollection("fruits").createIndex(Indexes.ascending("name"));
    }

    @RollbackExecution
    public void rollback() {
        mongoDatabase.getCollection("fruits").dropIndex(Indexes.ascending("name"));
    }
}

And finally, you can manually run the migration by injecting the MongockFactory bean :

import io.quarkiverse.mongock.MongockFactory;

public class MigrationService {

    @Inject
    MongockFactory mongockFactory;

    public void migrate() {
        MongockRunner mongockRunner = mongockFactory.createMongockRunner();
        mongockRunner.execute();
    }
}

Limitations

For now, this extension only support the MongoDB sync driver with the default MongoDB client (no support for multiple clients yet).

Extension Configuration Reference

Configuration property fixed at build time - All other configuration properties are overridable at runtime

Configuration property

Type

Default

Whether Mongock is enabled during the build.

If Mongock is disabled, the Mongock beans won’t be created and Mongock won’t be usable.

Environment variable: QUARKUS_MONGOCK_ENABLED

boolean

true

true to execute Mongock automatically when the application starts, false otherwise.

Environment variable: QUARKUS_MONGOCK_MIGRATE_AT_START

boolean

false

true to enable transaction, false otherwise. If the driver does not support transaction, it will be automatically disabled.

Environment variable: QUARKUS_MONGOCK_TRANSACTION_ENABLED

boolean

true