mongock / mongock

Lightweight Java based migration tool
Apache License 2.0
450 stars 63 forks source link

Correct the classpath of your application so that it contains compatible versions of the classes io.mongock.driver.mongodb.springdata.v3.SpringDataMongoV3DriverBase and org.springframework.data.mongodb.core.MongoTemplate #687

Open Spartaques opened 3 weeks ago

Spartaques commented 3 weeks ago

Description

[Description of the issue] Mongock is not working with java spring boot 3.3.2 and

org.springframework.boot spring-boot-starter-data-mongodb

PRIORITY

[CRITICAL, NORMAL or MINOR]

Version and environment

Mongock

Environment

Steps to Reproduce

Here is my code

package com.platform8.wallet.config;

import io.mongock.driver.api.driver.ConnectionDriver; import io.mongock.runner.springboot.MongockSpringboot; import io.mongock.runner.springboot.base.MongockInitializingBeanRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.data.mongodb.core.MongoTemplate;

@Configuration
public class MongockConfig {

    @Bean
    @Profile("!test")  // Exclude this bean in the test environment
    public MongockInitializingBeanRunner mongockRunner(ConnectionDriver driver, ApplicationContext applicationContext) {
        return MongockSpringboot.builder()
                .setDriver(driver)
                .setSpringContext(applicationContext)
                .addMigrationScanPackage("com.platform8.wallet.mongock") // Specify the package for ChangeUnits
                .buildInitializingBeanRunner();
    }

    @Bean
    public ConnectionDriver connectionDriver(MongoTemplate mongoTemplate) {
        return io.mongock.driver.mongodb.springdata.v3.SpringDataMongoV3Driver.withDefaultLock(mongoTemplate);
    }
}
package com.platform8.wallet.mongock;

import com.mongodb.client.MongoDatabase;
import io.mongock.api.annotations.ChangeUnit;
import io.mongock.api.annotations.Execution;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.IndexOperations;

@RequiredArgsConstructor
@ChangeUnit(id = "makeOperationCollectionSharded", order = "001", author = "andrii.bashuk", systemVersion = "1")
public class DatabaseChangeLog {

    private final MongoTemplate mongoTemplate;
    private final MongoDatabase mongoDatabase;

    @Execution
    public void createShardKeyIndex() {

        // Create index on the 'wId' field if not already created
        IndexOperations indexOps = mongoTemplate.indexOps("operations");
        indexOps.ensureIndex(new org.springframework.data.mongodb.core.index.Index().on("wId", Sort.Direction.ASC));

        // Enable sharding on the database
        mongoDatabase.runCommand(new Document("enableSharding", mongoTemplate.getDb().getName()));

        // Shard the operations collection on the 'wId' field
        mongoDatabase.runCommand(new Document("shardCollection", mongoTemplate.getDb().getName() + ".operations")
                .append("key", new Document("wId", 1)));
    }
}

here is error stack trace APPLICATION FAILED TO START


Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

io.mongock.driver.mongodb.springdata.v3.SpringDataMongoV3DriverBase.specificInitialization(SpringDataMongoV3DriverBase.java:65)

The following method did not exist:

'org.springframework.data.mongodb.MongoDatabaseFactory org.springframework.data.mongodb.core.MongoTemplate.getMongoDbFactory()'

The calling method's class, io.mongock.driver.mongodb.springdata.v3.SpringDataMongoV3DriverBase, was loaded from the following location:

jar:file:/home/andrew/.m2/repository/io/mongock/mongodb-springdata-v3-driver/5.4.4/mongodb-springdata-v3-driver-5.4.4.jar!/io/mongock/driver/mongodb/springdata/v3/SpringDataMongoV3DriverBase.class

The called method's class, org.springframework.data.mongodb.core.MongoTemplate, is available from the following locations:

jar:file:/home/andrew/.m2/repository/org/springframework/data/spring-data-mongodb/4.3.2/spring-data-mongodb-4.3.2.jar!/org/springframework/data/mongodb/core/MongoTemplate.class

The called method's class hierarchy was loaded from the following locations:

org.springframework.data.mongodb.core.MongoTemplate: file:/home/andrew/.m2/repository/org/springframework/data/spring-data-mongodb/4.3.2/spring-data-mongodb-4.3.2.jar

Action:

Correct the classpath of your application so that it contains compatible versions of the classes io.mongock.driver.mongodb.springdata.v3.SpringDataMongoV3DriverBase and org.springframework.data.mongodb.core.MongoTemplate

dieppa commented 3 weeks ago

Hello @Spartaques , we'll take a look and will get back to you as soon as possible

dieppa commented 3 weeks ago

Hello @Spartaques , we are a bit busy with some clients, would you be happy realising a PR for this and we'll review it asap?

Cheers

borjarodriguezjscingenium commented 23 hours ago

With spring boot 3.3.2 spring-data-mongo version is 4.x.x.

You are using in your pom mongock driver version 3.

Change the dependency to v4 driver and give a try.

<dependency>
    <groupId>io.mongock</groupId>
    <artifactId>mongodb-springdata-v4-driver</artifactId>
</dependency>