Hi,
Does anyone integrated Liquibase in a mysql testcontainer to apply DB
migrations and use Jooq to connect to the MySql test container DB to
generate java files?
I wanted to do this inside a gradle Spring Boot application. Can someone
help me on this ?
Any example or reference might be helpful ?
I am trying to go in the following sequence.
- Spinup MySql test container
- Apply DB migrations using Liquibase against MySql test container.
- Generate Java files from the DB against MySql testcontainer DB.
Also I wanted to have the code separated in three .gradle files.
Here is what I have started with
*mysqltc.gradle*
------------------------
```
// mysql-tc.gradle
import org.testcontainers.containers.MySQLContainer
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.testcontainers:testcontainers:${testcontainersVersion}"
classpath "org.testcontainers:mysql:${testcontainersVersion}"
classpath "com.mysql:mysql-connector-j:8.4.0"
}
}
tasks.register('startMySQLContainer') {
println("----Stating MySql DB TC----")
doLast {
def mysqlContainer = new MySQLContainer("mysql:latest")
.withDatabaseName("lmsdb")
.withUsername("test")
.withPassword("test")
mysqlContainer.start()
project.ext.dbUrl = mysqlContainer.getJdbcUrl()
project.ext.dbUsername = mysqlContainer.getUsername()
project.ext.dbPassword = mysqlContainer.getPassword()
project.ext.dbDriver = mysqlContainer.getDriverClassName()
println "MySQL container started with URL: ${project.ext.dbUrl}"
}
}
tasks.register('stopMySqlContainer') {
doLast {
if (mysqlContainer) {
mysqlContainer.stop()
}
}
}
```
* liquibase.gradle*
```
println("----Configuring Liquibase----")
dependencies {
implementation 'org.liquibase:liquibase-core:4.24.0'
}
liquibase {
activities {
main {
changeLogFile 'src/main/resources/db/changelog/changelog-master.sql'
url project.ext.has('d') ? project.ext.jdbcUrl :
'jdbc:tc:mysql:8.0.36:///lmsdb?TC_TMPFS=/testtmpfs:rw'
username project.ext.has('username') ? project.ext.username : 'test'
password project.ext.has('password') ? project.ext.password : 'test'
}
}
runList = 'main'
}
tasks.named('update') {
dependsOn ':startMySQLContainer'
}
```
*jooq.gradle*
-------------------
```/**
* This gradle file is used to generate JOOQ DAO, POJOs and fluent API for
writing SQL queries using
* generated classes. The default configuration below should work in most
cases, but it can be
* modified as needed based on the plugin documentation:
*
* https://github.com/etiennestuder/gradle-jooq-plugin
*/
import nu.studer.gradle.jooq.JooqGenerate
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath "nu.studer:gradle-jooq-plugin:${jooqPluginVersion}"
}
ext {
// use jOOQ version defined in Spring Boot
jooqVersion = dependencyManagement.importedProperties['jooq.version']
}
configurations.classpath {
// Enforce the jOOQ configuration XML schema version
resolutionStrategy.eachDependency {
if (requested.group == 'org.jooq' && requested.name.startsWith('jooq')) {
useVersion jooqVersion
}
}
}
}
apply plugin: nu.studer.gradle.jooq.JooqPlugin
println("Configuring JOOQ dependencies and code generation plugin...")
// JOOQ dependencies
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jooq'
jooqGenerator 'com.mysql:mysql-connector-j'
jooqGenerator 'org.slf4j:slf4j-simple'
jooqGenerator 'com.fasterxml.jackson.core:jackson-databind'
jooqGenerator "org.testcontainers:mysql"
jooqGenerator 'software.aws.rds:aws-mysql-jdbc:1.+'
implementation 'org.testcontainers:jdbc'
}
// Configure JOOQ
jooq {
version = jooqVersion
configurations {
main {
generationTool {
logging = org.jooq.meta.jaxb.Logging.WARN
jdbc {
driver = 'org.testcontainers.jdbc.ContainerDatabaseDriver'
}
generator {
name = 'org.jooq.codegen.DefaultGenerator'
database {
name = 'org.jooq.meta.mysql.MySQLDatabase'
inputSchema = 'lmsdb'
}
generate {
records = true
immutablePojos = true
fluentSetters = true
javaTimeTypes = false
}
target {
packageName = "com.chegg.${mainPackageName}.db"
}
strategy.name = 'org.jooq.codegen.DefaultGeneratorStrategy'
}
}
}
}
}
// Add database-specific JDBC driver dependencies for JOOQ code generation
and plugin configuration
String databaseType = findProperty("databaseType") as String
if (databaseType == "aurora-mysql") {
ext.jooqSecrets = project.getSecretsById(project.getPropertyValue(
"app.db.app-secret-id"))
jooq.configurations.main {
generationTool {
jdbc {
//url = "jdbc:mysql://" +
(project.getPropertyValue("spring.datasource.url") as String).split("://",
2)[1]
//user = jooqSecrets.get("username")
//password = jooqSecrets.get("password")
url = project.ext.has('jdbcUrl') ? project.ext.jdbcUrl :
'jdbc:tc:mysql:8.0.36:///lmsdb?TC_TMPFS=/testtmpfs:rw'
}
}
}
} else if (databaseType == "mysql") {
jooq.configurations.main {
generationTool {
jdbc {
url = project.getPropertyValue("spring.datasource.url")
user = project.getPropertyValue("spring.datasource.username")
password = project.getPropertyValue("spring.datasource.password")
}
}
}
}
// Configure the JVM used for the JOOQ code generation process
tasks.withType(JooqGenerate).configureEach {
javaExecSpec = { JavaExecSpec s ->
s.systemProperties System.properties
}
}
tasks.named('generateJooq') {
dependsOn ':startMySQLContainer', ':update'
}```
In my build.gradle I am applying from those three .gradle files.
I am stuck with an error.
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jooq-user/61012e09-94c7-4287-b1fd-7254fee3178en%40googlegroups.com.