Kontainers with Flyway
Flyway is a tool that lets you version control incremental changes to your database. Kontainers easily integrates with Flyway.
tip
If you're using a framework like Spring Boot or Micronaut, you can skip this section and instead read either the Spring Boot Flyway or Micronaut Flyway documentation.
Getting Started
Add Kontainers' to your project's test dependencies.
build.gradle.kts
dependencies {
// MariaDB is used here as an example, but any JDBC aware container may be used.
testImplementation("io.microkt.kontainers:kontainers-mariadb")
testImplementation("io.microkt.kontainers:kontainers-junit5")
}
Running a Flyway Migration
@Kontainers // annotate your test to load the Kontainers extension
internal class FlywayMariaKontainerTest(
// pass the Database Kontainer you'd like to use as a constructor
// parameter; Kontainers will take care of its lifecycle
mariaKontainer: MariaKontainer
) {
// define a datasource
private var dataSource: DataSource = HikariDataSource(
HikariConfig().apply {
jdbcUrl = mariaKontainer.createJdbcUrl()
username = mariaKontainer.getUsername()
password = mariaKontainer.getPassword()
}
)
init {
// configure Flyway and run the migration
val flyway = Flyway.configure().dataSource(dataSource).load()
flyway.migrate()
}
// your integration tests here
}