Spring Boot Embedded MongoDb data pre-populated
I want to know if there are tools like Flyway that can help with database initialization / migration for mongodb. Some of my thoughts were
- I used flapdoodle as embedded mongo using springboot. This works fine, but I need to manually put data in it.
- For Junit tests with mongo db, I use nosqlunit . This works great with Fongo (Fake mongo). It supports reading data from json file and pre-filling the database with data at startup time. But this only works with junit as it is a JUnit extension.
What I'm looking for is a combination of both of the above, inline mongo that not only works with JUnit but can prefill data from a given json (similar to V1__init.sql in Flyway)
Is there such a tool?
source to share
Finally, I developed this simple pre-signed version for Mongo. Here is the code.
https://github.com/pvpkiran/mongoprefill
Given the seed data, this autoconfiguration fills the mango.
source to share
You can also use Mongobee for this. If the application is downloaded set of changes
Maven dependency
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
</dependency>
you will need to create bean for Mongobee in your context xml file
<bean id="mongobee" class="com.github.mongobee.Mongobee">
<constructor-arg ref="mongo"/>
<property name="dbName" value="${mongo.databaseName}"/>
<property name="enabled" value="true"/>
<property name="changeLogsScanPackage" value="basepackagewherechangesetispresent"/>
Now add the changeset class
@ChangeLog(order = "1")
public class DatabaseChangeLog {
@ChangeSet(order = "101", id = "somelogicalnameforthischangeset", author = "nameofpersonwhodidthischange")
public void setupSeedData(MongoTemplate mongoTemplate) {
// run your datasetup, prefill,migration here.
}
And just like a span, it also maintains a schema version table so that the same changeset does not run again in the same environment.
source to share