Dropwizard Mongo Migrations is a simple library to assist in migrating Mongo databases in Dropwizard applications. This library provides a set of Dropwizard commands similar to Dropwizard's migration library for RDBMS systems.
The migrations are performed by the library Mongock. This bundle sets up the configuration to use the Mongock migration framework as a Dropwizard command.
To use this library, you need to add a MongoMigrationsBundle
. Here is an example of adding a MongoMigrationsBundle
to a Dropwizard application.
First, provide necessary properties in your Configuration class. For example:
// imports...
// Note this assumes Lombok to generate getter and setter methods
@Getter
@Setter
public class AppConfiguration extends Configuration {
private String migrationPackage;
private String mongoUri;
private String mongoDbName;
}
Next, create a new MongoMigrationsBundle
in the initialize
of your Application
class and add the bundle to
the Bootstrap
object.
// imports...
public class App extends Application<AppConfiguration> {
public void initialize(Bootstrap<AppConfiguration> bootstrap) {
var migrationsBundle = new MongoMigrationsBundle<AppConfiguration>() {
@Override
public String getMigrationPackage(AppConfiguration config) {
return config.getMigrationPackage();
}
@Override
public String getMongoUri(AppConfiguration config) {
return config.getMongoUri();
}
@Override
public String getDatabaseName(AppConfiguration config) {
return config.getMongoDbName();
}
@Override
public ConnectionDriver getConnectionDriver(AppConfiguration config) {
var mongoClient = MongoClients.create(getMongoUri(config));
var mongoTemplate = new MongoTemplate(mongoClient, getDatabaseName(config));
return SpringDataMongoV3Driver.withDefaultLock(mongoTemplate);
}
};
bootstrap.addBundle(migrationsBundle);
}
@Override
public void run(AppConfiguration configuration, Environment environment) {
// code to set up your application's resources, etc.
}
}
Once this setup is complete, add Mongock migrations to the migration package that you provided to the bundle. You can
then run db migrate
to run pending migrations.