【问题标题】:Is there a way to generate jooq code from flyway migrations?有没有办法从 flyway 迁移生成 jooq 代码?
【发布时间】:2023-02-04 14:56:12
【问题描述】:

我有一个使用 Jooq 的 kotlin spring boot 应用程序。 我的数据库是 postresql,我为其配置了飞路迁移。 问题是当我在我的开发环境中时,为 jooq 生成代码没有问题,但是当我尝试将我的应用程序部署到我的服务器时(使用 github 操作和 docker compose)我无法构建项目,因为 Jooq 正在尝试连接到数据库(尚未启动)以生成必要的代码。 我的 gradle 构建如下所示:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jooq.meta.jaxb.ForcedType
import org.jooq.meta.jaxb.Logging
import org.jooq.meta.jaxb.Property

val environment = System.getenv()

plugins {
    id("org.springframework.boot") version "2.7.5"
    id("io.spring.dependency-management") version "1.0.15.RELEASE"
    kotlin("jvm") version "1.8.0"
    kotlin("plugin.spring") version "1.8.0"

    id("nu.studer.jooq") version "7.0"
}

group = "osu.salat23"
version = "ALPHA"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-actuator")


    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.flywaydb:flyway-core")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
    implementation("com.squareup.okhttp3:okhttp:4.10.0")
    implementation("com.github.yvasyliev:java-vk-bots-longpoll-api:3.5.3")
    implementation("org.telegram:telegrambots-spring-boot-starter:6.3.0")
    implementation("com.microsoft.playwright:driver-bundle:1.28.1")
    implementation("com.microsoft.playwright:playwright:1.28.1")

    runtimeOnly("org.postgresql:postgresql")

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.security:spring-security-test")
    testImplementation("com.squareup.okhttp3:mockwebserver:4.10.0")
    testImplementation("junit:junit:4.13.2")

    jooqGenerator("org.postgresql:postgresql:42.5.1")
    jooqGenerator("jakarta.xml.bind:jakarta.xml.bind-api:3.0.1")
}
configurations {
    all {
        exclude("org.apache.logging.log4j", "log4j-slf4j-impl")
    }
}


tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

jooq {
    version.set("3.16.0")  // default (can be omitted)
    edition.set(nu.studer.gradle.jooq.JooqEdition.OSS)  // default (can be omitted)

    configurations {
        create("main") {  // name of the jOOQ configuration
            generateSchemaSourceOnCompilation.set(false)  // default (can be omitted)

            jooqConfiguration.apply {
                logging = Logging.WARN
                jdbc.apply {
                    driver = "org.postgresql.Driver"
                    url = environment["DATABASE_URL"] ?: "jdbc:postgresql://localhost:5432/postgres"
                    user = environment["DATABASE_USER"] ?: "postgres"
                    password = environment["DATABASE_PASSWORD"] ?: "postgres"
                    properties.add(Property().apply {
                        key = "ssl"
                        value = "false"
                    })
                }
                generator.apply {
                    name = "org.jooq.codegen.DefaultGenerator"
                    database.apply {
                        name = "org.jooq.meta.postgres.PostgresDatabase"
                        inputSchema = "public"
                        forcedTypes.addAll(listOf(
                            ForcedType().apply {
                                name = "varchar"
                                includeExpression = ".*"
                                includeTypes = "JSONB?"
                            },
                            ForcedType().apply {
                                name = "varchar"
                                includeExpression = ".*"
                                includeTypes = "INET"
                            }
                        ))
                    }
                    generate.apply {
                        isDeprecated = false
                        isRecords = true
                        isImmutablePojos = true
                        isFluentSetters = true
                    }
                    target.apply {
                        packageName = "osu.salat23.circler"
                        directory = "src/generated-src/jooq/main"  // default (can be omitted)
                    }
                    strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy"
                }
            }
        }
    }
}

有没有办法不是从数据库而是从我的飞行路线迁移生成代码?我试图遵循官方文档,但信息太少,也没有 gladle kotlin dsl 代码示例,所以我在这里问。

【问题讨论】:

    标签: spring-boot kotlin gradle jooq flyway


    【解决方案1】:

    有两种方法可以做到这一点:

    • 如果您认为您没有使用 RDBMS 供应商特定的任何东西,那么 jOOQ 可以使用 DDLDatabase 模拟(简单的)Flyway 迁移(很像任何基于 SQL 脚本的迁移和数据库设置,包括转储)
    • 如果您正在使用 RDBMS 供应商特定的东西,或更高级的 Flyway 功能,这些功能不能简单地模拟(例如可重复迁移),那么理想情况下,您只需设置一个 runs an actual Flyway migration prior to code generation, e.g. in testcontainers 的构建

    上面的文章使用了 Maven,但原则上应该与 Gradle 的工作方式相同。

    【讨论】:

      【解决方案2】:

      您可以考虑有意将 jOOQ 生成的代码提交到您的存储库,而不是作为构建过程的一部分每次都重新生成它。

      基本思想是,当您打算更改数据库时,大概您会在您的开发环境中:运行数据库迁移,然后生成 jooq 代码,更改您的项目以使用新代码进行编译,提交。

      【讨论】:

        猜你喜欢
        • 2017-01-22
        • 1970-01-01
        • 2019-02-19
        • 2010-09-07
        • 2021-04-13
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        • 2012-02-01
        相关资源
        最近更新 更多