I have an already populated SQL database where I would like to add Javers auditing. Is there a way to initialize the jv_snapshot table with the current state of objects prior to running the initial update on a specific object? I find that I am losing the previous state as the jv_snapshot table contains the update state as the initial state. I am running a spring boot app with hibernate/jpa.
Depending on the size of your database it may take hours or days to sync every record with Javers even if those records may never change from the Initial state.
It may not be exactly what you're looking for, but it works for me.
// UserService.java
@Autowired
private Javers javers;
@Autowired
private UserRepository userRepository;
public void updateUser(UserDTO user){
User existing = userRepository.findOne(user.getId());
// Initial Commit Assuming This Record is Not Tracked by Javers Yet
javers.commit("Initial", existing);
// Update Logic ...
existing.setUpdatedTimestamp(LocalDateTime.now());
userRepository.save(existing);
}
public void deleteUser(Long id){
User existing = userRepository.findOne(id);
// Initial Commit Assuming This Record is Not Tracked by Javers Yet
javers.commit("Initial", existing);
// Delete Logic ...
userRepository.delete(id);
}
// UserRepository.java
@Repository
@Transactional
@JaversSpringDataAuditable
public interface UserRepository extends JpaRepository<User, Long>{}
With this logic above you will always have the initial state your object was in before an UPDATE or DELETE occurs. In the event of a CREATE the initial state would be captured by Javers automatically.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With