Spring specific features

MongockTemplate

As mentioned in ChangeLogs section, when using Spring, you must use MongockTemplate, instead of Spring MongoTemplate. MongockTemplate is just a decorator/wrapper providing exactly the same API than MongoTemplate, but ensuring your changes are correctly synchronised.

You can find the technical reason behind in the Lock section.

@ChangeSet(order = "005", id = "changeWithMongockTemplate", author = "mongock")
public void changeWithMongockTemplate(MongockTemplate mongockTemplate) {
mongockTemplate.save(new MyEntity());
}

Profiles

Mongock accepts Spring's org.springframework.context.annotation.Profile annotation. If a changeLog or changeSet class is annotated with @Profile, then it is activated for current application profiles.

Mongock will automatically pick the active profiles from the Spring ApplicationContext . Then you only need to annotate your changeLogs and changeSets.

Annotating ChangeLogs and ChangeSets with Profile

Example 1: annotated changeSet will be invoked for a dev profile

@Profile("dev")
@ChangeSet(author = "mongock", id = "changeSetForDevOnly", order = "01")
public void changeSetForDevOnly(MongoDatabase db){
// ...
}

Example 2: all change sets in a changeLog will be invoked for a test profile

@ChangeLog(order = "1")
@Profile("test")
public class changeLogForTestOnly{
@ChangeSet(author = "mongock", id = "changeSetForTestOnly", order = "01")
public void changeSetForTestOnly(MongoDatabase db){
// ...
}
}

Mongock will support in next versions the new Profile expression approach from Spring. Please check our roadmap

ApplicationRunner vs InitializingBean

When using Spring runner, you choose what type of bean you want to build; Spring ApplicationRunner bean or a InitializingBean.

With annotation approach:

ApplicationRunner
InitializingBean
ApplicationRunner
mongock:
change-logs-scan-package:
- com.github.cloudyrock.mongock...changelogs.client.initializer
runner-type: applicationrunner
InitializingBean
mongock:
change-logs-scan-package:
- com.github.cloudyrock.mongock...changelogs.client.initializer
runner-type: initializingbean

With traditional builder approach:

ApplicationRunner
InitializingBean
ApplicationRunner
MongockSpring5.builder().
.addChangeLogsScanPackage("changelogs.package.path")
//...
.buildApplicationRunner();
InitializingBean
MongockSpring5.builder().
.addChangeLogsScanPackage("com.github.cloudyrock.mongock.integrationtests.spring5.springdata3.changelogs.client.initializer")
//...
.buildInitializingBeanRunner();