1

I have a problem with proper configure mongock for my project.

I have added to pom.xml dependencies:

  <dependencies>
    <dependency>
        <groupId>io.mongock</groupId>
        <artifactId>mongock-springboot</artifactId>
        <version>5.2.2</version>
    </dependency>
    <dependency>
        <groupId>io.mongock</groupId>
        <artifactId>mongodb-springdata-v3-driver</artifactId>
        <version>5.2.2</version>
    </dependency>
    ...
  </dependencies>
  <dependencyManagement>
     <dependencies>
        <dependency>
            <groupId>io.mongock</groupId>
            <artifactId>mongock-driver-mongodb-bom</artifactId>
            <version>5.2.2</version>
            <type>pom</type>
        </dependency>
        ...
    </dependencies>
</dependencyManagement>

I have added annotation on ApplicationClass: @EnableMongock

In application.yml I have added configuration:

mongock:
  migration-scan-package:
    - com.test.project.config.dbmigrations
  enabled: true

Documentation says that this setup should be enough, but when I run app I have got error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method getBuilder in io.mongock.runner.springboot.config.MongockContext required a bean of type 'io.mongock.driver.api.driver.ConnectionDriver' that could not be found.


Action:

Consider defining a bean of type 'io.mongock.driver.api.driver.ConnectionDriver' in your configuration.

Do you know how to fix it? Thanks in advance.

Piotr
  • 569
  • 6
  • 20

4 Answers4

2

You can try by making a setup bean.

    @Bean
    public MongockApplicationRunner mongockApplicationRunner(
            ApplicationContext springContext,
            MongoTemplate mongoTemplate) {
        return MongockSpringboot.builder()
                .setDriver(SpringDataMongoV3Driver.withDefaultLock(mongoTemplate))
                .addMigrationScanPackage("your_changeLog_package_path")
                .setSpringContext(springContext)
                .buildApplicationRunner();
    }
1

Depending on the version of Spring Boot you are using, this likely has different causes:

When using Spring Boot 3.x.x you need to use

<dependency>
    <groupId>io.mongock</groupId>
    <artifactId>mongock-springboot-v3</artifactId>
</dependency>

instead of mongock-springboot as stated in GitHub issue #589.


If using Spring Boot 2.x.x or above change does not work, for some reason the Bean is not auto-detected.

Defining the Bean manually should solve/workaround this:

  • Either use the Builder approach instead of the @EnableMongock Annotation.

    You can choose between the ApplicationRunner or the InitializingBeanRunner.

    An example for such a Runner can be found in the Mongock-examples repo.

  • Alternatively you can only define the ConnectionDriver Bean while keeping the Annotation approach with something like this:

    @Bean
    // name of Method seems irrelevant
    public ConnectionDriver mongockConnection(MongoClient mongoClient) {
        return SpringDataMongoV3Driver.withDefaultLock(mongoClient, "your databaseName");
    }
    
1

If you are using spring 3.x.x version, the dependencies mentioned in documentations are not proper. Use dependencies like below in order to make autoconfiguration to work:

<dependency>
  <groupId>io.mongock</groupId>
  <artifactId>mongock-bom</artifactId>
  <version>5.2.4</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>
<dependency>
  <groupId>io.mongock</groupId>
  <artifactId>mongock-springboot-v3</artifactId>
  <version>5.2.4</version>
</dependency>
<dependency>
  <groupId>io.mongock</groupId>
  <artifactId>mongodb-springdata-v4-driver</artifactId>
  <version>5.2.4</version>
</dependency>

Post this, your configuration in application.yml will work

0

For Spring version 3.1 and Spring Boot, this worked for me in pom.xml:

<dependency>
    <groupId>io.mongock</groupId>
    <artifactId>mongock-springboot-v3</artifactId>
    <version>5.3.1</version>
</dependency>
<dependency>
    <groupId>io.mongock</groupId>
    <artifactId>mongodb-springdata-v4-driver</artifactId>
    <version>5.3.1</version>
</dependency>

and I completely removed the property:

mongock.transaction-enabled=true

in the properties file and instead just added @EnableMongock annotation under @SpringBootApplication annotation in my application class.

tlarson
  • 353
  • 4
  • 11