1

I'm trying to transfer the previously created ID to other changesets in mongock. This is my solution but I don't know if it's correct. I will be grateful if you could share your proposition.

@ChangeLog(order = "001")
public class DatabaseChangelock {

    private Map<String, ObjectId> myIdMap = new HashMap<>();

    @ChangeSet(order = "001", id = "logs001", author = "Me")
    public void userTest(UserEntityRepo user) {
        UserEntity userEntity = new UserEntity;
        
        userEntity.setName("Mike");
        userEntity.setSurname("Smith");
        
        user.save(userEntity);
        myIdMap.put("test",userEntity.getId());

    }
    @ChangeSet(order = "002", id = "logs002", author = "Me")
    public void factoryTest(FactoryEntityRepo factory) {
        FactoryEntity factoryEntity = new FactoryEntity;
        
        factoryEntity.setName("BigCompany");
        factoryEntity.setBossId(myIdMap.get("test"));
        factory.save(factoryEntity);
    }
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
LittleBee
  • 11
  • 1

1 Answers1

0

If you are considering to use transactions, I assume your MongoDB database is 4.0 or higher.

In that case I suggest you to follow the official MongoDB and Spring documentation and then just follow the Mongock documentation to tell it to use transactions.

Please notice that right now, Mongock only supports all-or-nothing transactions, this means that just one transaction is created to the entire migration.

We are working to provide different levels of transactions as well as other mechanisms to help in non-transactional environments.

Mongock team
  • 1,188
  • 5
  • 9