Currently I have some new modules using Spring Boot and a H2 embedded database for functional testing.
The legacy module works with a lot of Liquibase scripts to construct the whole database.
I was looking to use Wix Embedded Mysql to make the test database more production-like. After read the docs I did not find anything specific about how to handle scripts using tools like Liquibase or Flyway.
Is it possible to execute a Liquibase goal on this embedded database after his startup?
After a few days of research, yes, there is a way of running Liquibase over Wix Embedded MySQL.
Here is the step by step:
The configuration around Wix is pretty straight forward as described on their GitHub:
MysqldConfig config = aMysqldConfig(v5_7_latest)
.withCharset(UTF8)
.withPort(3060)
.withUser("myuser", "mypassword")
.withTimeZone("America/Sao_Paulo")
.build();
EmbeddedMysql mysqld = anEmbeddedMysql(config)
.addSchema("myschema")
.start();
I have added the Liquibase maven dependency on my project, so we have access to Liquibase code programmatically, the API can be found here.
First we have to build a datasource and pass to Liquibase find the correct implementation of our database, with the result we can then manipulate the Liquibase object to execute the goals:
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.username(mysqld.getConfig().getUsername());
dataSourceBuilder.password(mysqld.getConfig().getPassword());
dataSourceBuilder.driverClassName(com.mysql.jdbc.Driver.class.getName());
dataSourceBuilder.url("jdbc:mysql://localhost:3060/myschema");
DataSource dataSource = dataSourceBuilder.build();
Database database = DatabaseFactory
.getInstance()
.findCorrectDatabaseImplementation(
new JdbcConnection(dataSource.getConnection()) // Fetch MySQL database implementation
);
Liquibase liquibase = new Liquibase("liquibase/mychanges.xml", // Path to liquibase changes
new ClassLoaderResourceAccessor(),
database);
liquibase.update(new Contexts()); // This execute the liquibase:update on the embedded database