Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize Javers jv_snapshot with current state if the database is already in use?

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.

like image 847
cwillingham Avatar asked Dec 11 '25 04:12

cwillingham


1 Answers

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.

like image 138
wheelerswebservices Avatar answered Dec 12 '25 16:12

wheelerswebservices



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!