I do this exact thing in one of my Gradle builds. Runnig this task with 
*-Penvironment=test* will:

   1. Create a Postgres Docker container
   2. Initialize it with your Liquibase changelogs
   3. Generate your jOOQ artifacts, and 
   4. Shut down the container afterwards

You can see *if(environment == "test")* then Docker + Liquibase operations 
are invoked.

There's some hard-coded concepts (like environment names, Docker image 
version, etc), but you should be able to adapt it to your needs.

###################################

import liquibase.Liquibase
import liquibase.database.DatabaseFactory
import liquibase.database.jvm.JdbcConnection
import liquibase.resource.DirectoryResourceAccessor
import java.sql.DriverManager

tasks.register("jooqGenerate") {
  group = "jooq"
  description = "Runs the jOOQ code generator against the schema defined in 
the Liquibase changelogs"

  doLast {
    val environment = findProperty("environment")?.toString() ?: "local"

    var postgresContainer: PostgreSQLContainer<Nothing>? = null
    val schemaName: String
    val driverClassName: String
    val jdbcUrl: String
    val username: String
    val password: String

    if(environment == "test") {
      postgresContainer = 
PostgreSQLContainer<Nothing>("postgres:16.1").apply { start() }

      schemaName = "public"

      driverClassName = postgresContainer!!.driverClassName
      jdbcUrl = postgresContainer.jdbcUrl
      username = postgresContainer.username
      password = postgresContainer.password

      Class.forName(driverClassName)

      DriverManager.getConnection(jdbcUrl, username, password)
        .use { connection ->
          connection.autoCommit = true

          connection.createStatement().use { statement ->
            statement.executeUpdate("create schema if not exists 
$schemaName")
          }

          connection.schema = schemaName

          JdbcConnection(connection).use { liquibaseJdbcConnection ->
            val liquibaseDatabase = 
DatabaseFactory.getInstance().findCorrectDatabaseImplementation(liquibaseJdbcConnection)

            val liquibase = Liquibase("main.yaml", 
DirectoryResourceAccessor(File("...")), liquibaseDatabase)
            liquibase.update("")
            liquibase.forceReleaseLocks()
          }
        }

    } else {
      schemaName = if(environment == "local") "public" else environment

      driverClassName = findProperty(...)!!.toString()
      jdbcUrl = findProperty(...)!!.toString()
      username = findProperty(...)!!.toString()
      password = findProperty(...)!!.toString()
    }

    GenerationTool.generate(
      Configuration()
        .withJdbc(
          Jdbc()
            .withDriver(org.postgresql.Driver::class.java.name)
            .withUrl(jdbcUrl)
            .withUsername(username)
            .withPassword(password)
        )
        .withGenerator(
          Generator()
            .withName(...)
            .withStrategy(...)
            .withDatabase(...)
            .withGenerate(...)
            .withTarget(...)
        )
    )

    postgresContainer?.stop()
  }
}


On Thursday, February 22, 2024 at 6:31:34 PM UTC+9 [email protected] wrote:

> Well, what did you try?
>
> On Thu, Feb 22, 2024 at 10:19 AM Pasha Finkelshtein <
> [email protected]> wrote:
>
>> This obviously SHOULD work, but it doesn't. And I was hoping to get a 
>> concrete example to understand what I'm doing wrong.
>>
>> It seems to me that test containers just don't work during the gradle 
>> build, but maybe I'm wrong. And since nobody in the Testcontainers slack 
>> answer I thought that maybe here somebody has a working example.
>>
>> Sorry for bothering you
>>
>

-- 
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/1c9597f1-17ac-4d2b-b4a4-032bdc3b9c67n%40googlegroups.com.

Reply via email to