|
import org.flywaydb.gradle.task.FlywayMigrateTask |
|
import org.jooq.codegen.GenerationTool |
|
import org.jooq.meta.jaxb.* |
|
import org.jooq.meta.jaxb.Configuration |
|
import org.jooq.meta.jaxb.Target |
|
import org.testcontainers.containers.PostgreSQLContainer |
|
|
|
plugins { |
|
kotlin("jvm") version "1.9.10" |
|
id("org.flywaydb.flyway") version "9.22.1" |
|
} |
|
|
|
buildscript { |
|
repositories { |
|
mavenCentral() |
|
} |
|
|
|
dependencies { |
|
classpath("org.testcontainers:postgresql:1.19.0") |
|
classpath("org.postgresql:postgresql:42.6.0") |
|
classpath("org.jooq:jooq-codegen:3.18.6") |
|
} |
|
} |
|
|
|
repositories { |
|
mavenCentral() |
|
} |
|
|
|
dependencies { |
|
implementation("org.jooq:jooq:3.18.6") |
|
} |
|
|
|
sourceSets.main { |
|
kotlin.srcDir("src/main/generated") |
|
} |
|
|
|
val db = PostgreSQLContainer("postgres:latest") |
|
.withDatabaseName("smart_home") |
|
|
|
val jooqConfig = Configuration() |
|
.withLogging(org.jooq.meta.jaxb.Logging.DEBUG) |
|
.withGenerator( |
|
Generator() |
|
.withName("org.jooq.codegen.KotlinGenerator") |
|
.withDatabase( |
|
Database() |
|
.withInputSchema(db.databaseName) |
|
.withExcludes("FLYWAY_.*") |
|
) |
|
.withGenerate( |
|
Generate() |
|
.withGlobalObjectReferences(false) |
|
.withGeneratedAnnotation(false) |
|
.withGlobalSchemaReferences(false) |
|
) |
|
.withTarget( |
|
Target() |
|
.withPackageName("io.github.dfrommi.db") |
|
.withDirectory(projectDir.absolutePath + "/src/main/generated") |
|
) |
|
) |
|
|
|
|
|
val startDb by tasks.creating { |
|
outputs.upToDateWhen { false } |
|
|
|
doLast { |
|
db.start() |
|
} |
|
} |
|
|
|
val stopDb by tasks.creating { |
|
dependsOn(startDb) |
|
outputs.upToDateWhen { false } |
|
|
|
doLast { |
|
db.stop() |
|
} |
|
} |
|
|
|
tasks.withType<FlywayMigrateTask> { |
|
dependsOn(startDb) |
|
finalizedBy(stopDb) |
|
|
|
doFirst { |
|
url = db.getJdbcUrl() |
|
user = db.username |
|
password = db.password |
|
defaultSchema = db.databaseName |
|
locations = arrayOf("filesystem:src/main/resources/db") |
|
} |
|
} |
|
|
|
task("jooq") { |
|
dependsOn(startDb, "flywayMigrate") |
|
finalizedBy(stopDb) |
|
outputs.upToDateWhen { false } |
|
|
|
|
|
doFirst { |
|
File(jooqConfig.generator.target.directory).deleteRecursively() |
|
} |
|
|
|
doLast { |
|
GenerationTool.generate( |
|
jooqConfig.withJdbc( |
|
Jdbc() |
|
.withDriver(db.driverClassName) |
|
.withUrl(db.getJdbcUrl()) |
|
.withUsername(db.username) |
|
.withPassword(db.password) |
|
) |
|
) |
|
} |
|
} |
|
|
|
task("generate").dependsOn("jooq") |
|
tasks["build"].dependsOn("generate") |