Continuous database migration for MongoDB using Spring

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.
A MongoDB database looks like a JSON file and consists of collections and documents (comparable to tables and rows in SQL). To add a new attribute to a document you will just need to change the document-related POJO in the source code. However a new attribute will only be written to the database when a new document is inserted or an existing document is updated. This means that a new attribute will default to null, false or 0. You may want to set your own default values or apply other changes directly to the database. The common way is to manually connect to the database and perform JavaScript code to apply changes.

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 @Service and @Autowired annotations. „Continuous database migration for MongoDB using Spring“ weiterlesen