0

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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.