Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run migrations programmatically in Dropwizard

I have dropwizard-application (0.7.0) for which I want to run integration tests.

I've set up an integration test using DropwizardAppRule, like this:

@ClassRule
public static final DropwizardAppRule<MyAppConfiguration> RULE =
        new DropwizardAppRule<MyAppConfiguration>(
                MyApplication.class, Resources.getResource("testconfiguration.yml").getPath());

When I try to run the below tests using it, it doesn't work because I haven't run my migrations. What is the best way to run the migrations?

Test:

@Test
public void fooTest() {
    Client client = new Client();
    String root = String.format("http://localhost:%d/", RULE.getLocalPort());
    URI uri = UriBuilder.fromUri(root).path("/users").build();
    client.resource(uri).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(User.class, new LoginUserDTO("[email protected]", "password"));
}

Configuration:

 public class MyAppConfiguration extends Configuration {
@Valid
@NotNull
private DataSourceFactory database = new DataSourceFactory();

@JsonProperty("database")
public DataSourceFactory getDataSourceFactory() {
    return database;
}

@JsonProperty("database")
public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
    this.database = dataSourceFactory;
}

}

like image 270
andersem Avatar asked May 10 '14 09:05

andersem


1 Answers

Thanks to Kimble and andersem for putting me on the right track. Here's what I came up with in my @BeforeClass method:

// Create the test database with the LiquiBase migrations.
@BeforeClass
public static void up() throws Exception
{
    ManagedDataSource ds = RULE.getConfiguration().getMainDataSource().build(
        RULE.getEnvironment().metrics(), "migrations");
    try (Connection connection = ds.getConnection())
    {
        Liquibase migrator = new Liquibase("migrations.xml", new ClassLoaderResourceAccessor(), new JdbcConnection(connection));
        migrator.update("");
    }
}
like image 152
David Small Avatar answered Sep 21 '22 13:09

David Small