plugins {
    id 'org.flywaydb.flyway' version '9.7.0'
    id 'nu.studer.jooq' version '10.1.1'
    id 'java'
}

repositories {
    mavenCentral()
}

configurations {
    flywayMigration
}

dependencies {
    flywayMigration 'com.h2database:h2:2.4.240'
    jooqGenerator 'com.h2database:h2:2.4.240'
}

flyway {
    configurations = ['flywayMigration']
    url = 'jdbc:h2:~/test;AUTO_SERVER=TRUE'
    user = 'sa'
    password = ''
}

jooq {
    configurations {
        main {
            generationTool {
                logging = org.jooq.meta.jaxb.Logging.WARN
                jdbc {
                    driver = 'org.h2.Driver'
                    url = flyway.url
                    user = flyway.user
                    password = flyway.password
                }
                generator {
                    name = 'org.jooq.codegen.DefaultGenerator'
                    database {
                        name = 'org.jooq.meta.h2.H2Database'
                        includes = '.*'
                        excludes = ''
                    }
                    target {
                        packageName = 'nu.studer.sample'
                    }
                }
            }
        }
    }
}

// configure jOOQ task such that it only executes when something has changed that potentially affects the generated JOOQ sources
// - the jOOQ configuration has changed (Jdbc, Generator, Strategy, etc.)
// - the classpath used to execute the jOOQ generation tool has changed (jOOQ library, database driver, strategy classes, etc.)
// - the schema files from which the schema is generated and which is used by jOOQ to generate the sources have changed (scripts added, modified, etc.)
tasks.named('generateJooq').configure {
    // ensure database schema has been prepared by Flyway before generating the jOOQ sources
    dependsOn tasks.named('flywayMigrate')

    // declare Flyway migration scripts as inputs on the jOOQ task
    inputs.files(fileTree('src/main/resources/db/migration'))
        .withPropertyName('migrations')
        .withPathSensitivity(PathSensitivity.RELATIVE)

    // make jOOQ task participate in incremental builds (and build caching)
    allInputsDeclared = true
}
