4

I want to know if there are any tools like Flyway which can assist in database initialization / migration for mongodb. Some of the thoughts I had is

  1. I have used flapdoodle as embedded mongo with springboot. This works fine but, I need to manually put data into it.
  2. For Junit tests with mongo db, I use nosqlunit. This works perfectly fine with Fongo(Fake mongo). It has support to read data from a json file and prefill the database with data during startup. But this works only with junit, since this is a JUnit extension.

What I am looking for is a mix of both of the above, an embedded mongo which works not only with JUnit and can prefill data from a given json(similar to V1__init.sql in Flyway)
Is there any such tool ?

pvpkiran
  • 25,582
  • 8
  • 87
  • 134

3 Answers3

6

You can also use Mongobee for this. If runs your changeset when the application loads.

Maven dependency

 <dependency>
    <groupId>com.github.mongobee</groupId>
    <artifactId>mongobee</artifactId>
</dependency>

you will need to create the 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 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 flyway, it also maintains the schema version table, so that same change set does not ran again in same environment.

lrathod
  • 1,094
  • 1
  • 9
  • 17
1

Finally, I developed this simple version of data prefill for Mongo. Here is the code.

https://github.com/pvpkiran/mongoprefill

Given seed data, this Autoconfiguration fills up mongo.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
-1

You can import JSON data during your tests with flapdoodle

See the answer to a similar question here: Import JSON file in Mongo db using Spring Data Embedded Mongo

jchrbrt
  • 1,006
  • 1
  • 11
  • 12
  • read my question again. I am not talking about tests. I am talking about regular application startup – pvpkiran Jun 07 '18 at 12:04
  • @pvpkiran Hi the code shows how to "manually" import data into MongoDB with flapdoodle. This is exactly what you ask (point 1.), and you are also talking about tests in your question, however Although the code example is in a test class, you can use it in a "normal" java class of your application as well. – jchrbrt Jun 08 '18 at 13:29