0

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.

Captain Levi
  • 804
  • 7
  • 18

1 Answers1

0

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();
Mongock team
  • 1,188
  • 5
  • 9
  • Thanks, I understand I can call both the methods individually, the reason I asked this question was in case there was a scenario where I also wanted to test the roll back works automatically. I again understand we can call the roll back in a catch block with execution in try. But wanted to see if there was a better option, looks like there isn't one for now. Thanks for quick reply tho. – Captain Levi Feb 17 '22 at 11:18
  • That totally makes sense. But in case of a failure, in a transactional environment the rollback won't be executed and the execution is like it never took place. However, the `@RollbackBeforeExecution` method is executed, as the `@BeforeExecution` is executed outside the transaction. – Mongock team Feb 17 '22 at 11:59
  • 1
    Notice we are working in providing some test utilities as you are not the first one asking about testing ;) – Mongock team Feb 17 '22 at 11:59