1

I have simple document class:

public class Player {
    private String name;
    private String age;
}

I want to extend it with field Parameters:

public class Player {
    private String name;
    private String age;
    private Parameters parameters;

    public class Parameters {
        boolean leftFooted;
        boolean leftHanded;
    }
} 

My Mongock @Execution method would be:

@Execution
public void execution(PlayerRepository playerRepository) {

    playerRepository.findAll()
            .stream()
            .map(this::setDefaultParameters)
            .forEach(playerRepository::save);
}

with method:

private Player setDefaultParameters(Player player) {
    if (player.getParameters == null) {
        player.setParameters(new Parameters(false, false));
    }

    return player;
}

My question is - how to implement @RollbackExecution method if documents created after model extension can also have Parameters field with values 'false, false' and I just can not set Parameters field to null for all documents in database?

krzysiekcr
  • 11
  • 3

1 Answers1

0

First, am I right on the assumption that, before executing the @Execution method, there may be some documents which already have the parameters field with some value? And, after the execution, you struggle to identify those ones amended in the actual change unit versus those that already had the parameters field?

If that’s the case, you really are not breaking anything, and you would leave the @RollbackExecution empty, and the updated documents will remain with its default value in parameters field.

If you still care, you need to somehow mark those documents that you updated in the ChangeUnit (with a flag or something), so you can filter by the flag and restore your changes.

Mongock team
  • 1,188
  • 5
  • 9
  • Yes, your assumption is correct - my main problem was identifying records that should not be changed during @RollbackExectuion. By observing the application logs, I realized that the native transactions set by the mongock.transaction-enabled property were enough for me. Thanks for the answer :) – krzysiekcr Oct 12 '22 at 07:41