mongodbspring-datamongockmongobee

How to test change unit in mongock with its multiple attributes/lifecycle methods?


We recently migrated from MongoBee to Mongock, and with Mongock 5 version the @ChangeLog and @ChangeSet are depricated. Writing the @ChangeUnit is easy enough and rollback methods are very helpful.

However, I'm unable to figure out how to write a test simulating the migration in test DB and validating the changes in DB, as there are @BeforeExecution, @RollbackBeforeExecution, @Execution and @RollbackExecution attributes or lifecycle methods in a @ChangeUnit.

Earlier, I used to just call the method with @ChangeSet annotation like

assertOriginalStructure();
someMigrationChangeLog.updateIndexOnSomething();
assertIndexUpdated();

Now, I'm unsure if there is a clean way to write the above test as there is some logic in @BeforeExecution and also in @Execution. I know individually calling the annotated methods will work, but I wanted to know if there is a way to just run one @ChangeUnit as a whole.


Solution

  • In the new version 5, the basic change is that a ChangeUnit holds the unit of execution. That's normally done in the method annotated with @Execution, so the first approach is just doing the same you are doing but calling the @Execution method:

    assertOriginalStructure();
    someMigrationChangeUnit.updateIndexOnSomething();//annotated with @Execution
    assertIndexUpdated();
    

    However, your ChangeUnit can also provide@BeforeExecution, which would be used to perform any action that cannot be within the execution, for example, in a transactional MongoDB migration, DDL are not allowed inside a transaction, so that would be done in the @BeforeExecution. So if your changeUnit has both, @Execution and @BeforeExecution, you should do this:

    assertOriginalStructure();
    someMigrationChangeUnit.beforeExecution();//annotated with @BeforeExecution
    someMigrationChangeUnit.updateIndexOnSomething();//annotated with @Execution
    assertIndexUpdated();