1Z10
1Z10

Reputation: 3331

Spring-Boot: how to check if Mongock has completed?

I'm using Mongock for migrating and initializing my MongoDB database with Spring-Boot. What I need is a way to check when all the changelogs have been applied.

How can I check if Mongock completed all the changelogs/changesets?

I found that Mongock creates a collection mongockLock in which there is an entry while the migration is running.

Would it be enough to check for the absence of any document in the above collection to conclude that Mongock has completed the changelogs?

How can I query for the documents in this collection?

Upvotes: 0

Views: 1957

Answers (2)

Mongock team
Mongock team

Reputation: 1533

With spring boot, there are two approaches to use Mongock, like shown in Mongock's documentation:

  1. Via properties, using @EnableMongock

    mongock:
      runner-type: InitializingBean
    
    
    
  2. With the builder approach

    @Bean
    public MongockInitializingBeanRunner mongockInitializingBeanRunner(
                                           ApplicationContext springContext,
                                           MongoTemplate mongoTemplate){
    return MongockSpring5.builder()
        .setDriver(SpringDataMongo3Driver.withDefaultLock(mongoTemplate))
        .addChangeLogsScanPackage("your_changeLog_package_path")
        .setSpringContext(springContext)
        .buildInitializingBeanRunner();
    

}

  • The schedule job shouldn't happen before Mongock, if runner-type is InitializingBean. However it may happen if it's ApplicationRunner

Upvotes: 1

Mongock team
Mongock team

Reputation: 1533

first at all, the mongockLock collection is just the collection for the distributed pessimistic lock. It's the mechanism Mongock uses to ensure that a changeLog is only run by an instance at a time. If it's successful, just once(unless flag runAlways is true, in which case will obviously run every time). So mongockLock is not what you are looking for

There are 3 ways you can check your migration has run successfully, or not.

  1. Do nothing. If you don't change the default value of failFast in any ChangeSet, the Mongock runner will throw an exception if any of the changeLogs fails
  2. Subscribing to the Mongock events. You can check this example project
  3. Checking the collection mongockChangeLog

Upvotes: 1

Related Questions