In this post I introduce a continuous database migration mechanism for MongoDB using Java and Spring Boot.
When you look at a development with Continuous Deployment the database is also continuously adapted. Typical database adaptions are e.g. initialising new attributes for all documents with proper values or performing cleanups. It is advantageous to store those change scripts in the version control system. This way the database can stay in sync with the source code once the database scripts are executed in the correct order.
For many SQL databases it is possible to use one of the powerful Java libraries Liquibase or Flyway to manage database changes (like adding columns or modifying existing table rows).
Regarding MongoDB however, things tight up. MongoDB is a non-relational database, thus Liquibase or Flyway can’t be used for it.
Once you work on a larger project though, you cannot rely on manual changes anymore. You will need a database management. For this problem of a missing database migration mechanism I have worked out a solution: The idea is to scan a folder containing update scripts and to apply them to the database if needed. The database will store executed scripts to distinguish them from not executed scripts. This feature will then be attached to the application launch, so whenever the application is started, it’ll check for new scripts. The mechanism is fully integrated into the Spring Boot environment and uses helpful key features like
@Autowired annotations. „Continuous database migration for MongoDB using Spring“ weiterlesen