diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..61d3b31 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,37 @@ +name: Check + +on: + push: + branches: + - master + tags: + - '*' + + pull_request: + branches: + - master + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v1 + + - name: Set up JDK 1.11 + uses: actions/setup-java@v1 + with: + java-version: 1.11 + + - name: Cache global .gradle folder + uses: actions/cache@v2 + with: + path: ~/.gradle/caches + key: global-gradle-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }}-${{ hashFiles('**/*.kt*') }} + restore-keys: | + gradle-cache-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }} + + - name: Check with Gradle + run: ./gradlew check diff --git a/README.md b/README.md index c99a296..42d178b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ Jooq-Modelator ============== +[![GitHub license](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://raw.githubusercontent.com/ayedo/jooq-modelator/master/LICENSE) +[![Build Status](https://github.com/ayedo/jooq-modelator/workflows/Build/badge.svg)](https://github.com/ayedo/jooq-modelator/actions) + ## Overview This gradle plugin generates Jooq metamodel classes from Flyway & Liquibase migration files. It does so by running them against a dockerized database, and then running the Jooq generator against that database. @@ -31,8 +34,8 @@ The plugin has been tested with Version 18.06.1-ce-mac73 (26764). Two migration engines are supported: -- Flyway (version '6.0.1') -- Liquibase (version '3.8.0') +- Flyway (version '6.4.4') +- Liquibase (version '3.10.0') __For Liquibase there are limitations:__ diff --git a/build.gradle b/build.gradle index a0469dc..b134ea8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { - id 'org.jetbrains.kotlin.jvm' version '1.3.50' - id 'com.gradle.plugin-publish' version '0.10.1' + id 'org.jetbrains.kotlin.jvm' version '1.3.72' + id 'com.gradle.plugin-publish' version '0.12.0' } apply plugin: 'java-gradle-plugin' @@ -25,19 +25,19 @@ dependencies { compile('org.jetbrains.kotlin:kotlin-stdlib-jdk8') compile('net.jodah:failsafe:1.1.1') compile('com.spotify:docker-client:8.16.0') - compile('org.flywaydb:flyway-core:6.0.1') - compile('org.liquibase:liquibase-core:3.8.0') { + compile('org.flywaydb:flyway-core:6.4.4') + compile('org.liquibase:liquibase-core:3.10.0') { exclude group: 'ch.qos.logback', module: 'logback-classic' } - compile('org.slf4j:slf4j-simple:1.7.28') + compile('org.slf4j:slf4j-api:1.7.30') compile('javax.xml.bind:jaxb-api:2.3.1') compile('com.sun.xml.bind:jaxb-core:2.3.0.1') compile('javax.activation:activation:1.1.1') - compile('com.sun.xml.bind:jaxb-impl:2.3.2') + compile('com.sun.xml.bind:jaxb-impl:2.3.3') - testCompile('org.junit.jupiter:junit-jupiter-api:5.5.1') - testRuntime('org.junit.jupiter:junit-jupiter-engine:5.5.1') + testCompile('org.junit.jupiter:junit-jupiter-api:5.6.2') + testRuntime('org.junit.jupiter:junit-jupiter-engine:5.6.2') } pluginBundle { diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 0d4a951..5c2d1cf 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8d58bda..622ab64 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index cccdd3d..83f2acf 100755 --- a/gradlew +++ b/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,7 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" @@ -109,8 +125,8 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` diff --git a/gradlew.bat b/gradlew.bat index e95643d..24467a1 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome diff --git a/src/main/kotlin/ch/ayedo/jooqmodelator/core/DockerClientExtensions.kt b/src/main/kotlin/ch/ayedo/jooqmodelator/core/DockerClientExtensions.kt index 46d09d5..d27ae0b 100644 --- a/src/main/kotlin/ch/ayedo/jooqmodelator/core/DockerClientExtensions.kt +++ b/src/main/kotlin/ch/ayedo/jooqmodelator/core/DockerClientExtensions.kt @@ -20,6 +20,6 @@ fun DockerClient.useContainer(containerId: String, fn: () -> T) = fun DockerClient.findLabeledContainers(key: String, value: String) = this.listContainers(allContainers()) - .filter({ container -> + .filter { container -> container.labels()?.get(key) == value - }) \ No newline at end of file + } \ No newline at end of file diff --git a/src/main/kotlin/ch/ayedo/jooqmodelator/core/HealthCheck.kt b/src/main/kotlin/ch/ayedo/jooqmodelator/core/HealthCheck.kt index aecaa5e..0d45c28 100644 --- a/src/main/kotlin/ch/ayedo/jooqmodelator/core/HealthCheck.kt +++ b/src/main/kotlin/ch/ayedo/jooqmodelator/core/HealthCheck.kt @@ -36,9 +36,10 @@ class FlywayDependentHealthChecker(databaseConfig: DatabaseConfig, healthCheckCo withMaxDuration(maxDurationMs, MILLISECONDS) } + @Suppress("RedundantLambdaArrow") override fun waitForDatabase() { - net.jodah.failsafe.Failsafe.with(retryPolicy).run { -> + net.jodah.failsafe.Failsafe.with(retryPolicy).run { _ -> // for some reason 'use' does not work anymore var connection: Connection? = null diff --git a/src/main/kotlin/ch/ayedo/jooqmodelator/core/Migration.kt b/src/main/kotlin/ch/ayedo/jooqmodelator/core/Migration.kt index 572d085..dd0f1d9 100644 --- a/src/main/kotlin/ch/ayedo/jooqmodelator/core/Migration.kt +++ b/src/main/kotlin/ch/ayedo/jooqmodelator/core/Migration.kt @@ -15,6 +15,7 @@ import org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection import java.io.File import java.nio.file.Path +@Suppress("SpellCheckingInspection") interface Migrator { /* deletes all objects in the database */ @@ -69,8 +70,8 @@ class LiquibaseMigrator(databaseConfig: DatabaseConfig, migrationsPaths: List path.toFile() }) - .flatMap({ file: File -> file.listFiles({ pathName -> pathName.nameWithoutExtension == "databaseChangeLog" }).toList() }) + .map { path -> path.toFile() } + .flatMap { file: File -> file.listFiles { pathName -> pathName.nameWithoutExtension == "databaseChangeLog" }?.toList() ?: emptyList() } if (changeLogFiles.isEmpty()) { throw IllegalStateException("Cannot find liquibase changelog file. It must be named 'databaseChangeLog'.") diff --git a/src/main/kotlin/ch/ayedo/jooqmodelator/core/Modelator.kt b/src/main/kotlin/ch/ayedo/jooqmodelator/core/Modelator.kt index 919ba05..bf99902 100644 --- a/src/main/kotlin/ch/ayedo/jooqmodelator/core/Modelator.kt +++ b/src/main/kotlin/ch/ayedo/jooqmodelator/core/Modelator.kt @@ -5,6 +5,7 @@ import ch.ayedo.jooqmodelator.core.configuration.DatabaseConfig import com.spotify.docker.client.DefaultDockerClient import org.slf4j.LoggerFactory +@Suppress("SpellCheckingInspection") class Modelator(configuration: Configuration) { private val log = LoggerFactory.getLogger(Modelator::class.java) @@ -39,7 +40,7 @@ class Modelator(configuration: Configuration) { log.warn("More than one container with tag ${dockerConfig.labelKey}=$tag has been found. " + "Using the one which was most recently created") } - existingContainers.sortedBy({ it.created() }).map({ it.id() }).first() + existingContainers.sortedBy { it.created() }.map { it.id() }.first() } docker.useContainer(containerId) { diff --git a/src/main/kotlin/ch/ayedo/jooqmodelator/core/configuration/Configuration.kt b/src/main/kotlin/ch/ayedo/jooqmodelator/core/configuration/Configuration.kt index 2eecad3..eba249a 100644 --- a/src/main/kotlin/ch/ayedo/jooqmodelator/core/configuration/Configuration.kt +++ b/src/main/kotlin/ch/ayedo/jooqmodelator/core/configuration/Configuration.kt @@ -17,6 +17,7 @@ enum class MigrationEngine { LIQUIBASE } +@Suppress("SpellCheckingInspection") data class DockerConfig(val tag: String, val labelKey: String = "ch.ayedo.jooqmodelator", val env: List, diff --git a/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorExtension.kt b/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorExtension.kt index c4bb1d1..1e01053 100644 --- a/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorExtension.kt +++ b/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorExtension.kt @@ -1,6 +1,7 @@ package ch.ayedo.jooqmodelator.gradle +@Suppress("SpellCheckingInspection") open class JooqModelatorExtension { var jooqVersion: String? = null diff --git a/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorPlugin.kt b/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorPlugin.kt index d4b2f65..c4af0a2 100644 --- a/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorPlugin.kt +++ b/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorPlugin.kt @@ -5,7 +5,7 @@ import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import java.nio.file.Paths - +@Suppress("SpellCheckingInspection") open class JooqModelatorPlugin : Plugin { override fun apply(project: Project) { @@ -16,7 +16,7 @@ open class JooqModelatorPlugin : Plugin { modelatorRuntime.description = "Add JDBC drivers or generator extensions here." - project.afterEvaluate({ + project.afterEvaluate { val config = project.extensions.findByType(JooqModelatorExtension::class.java)!! @@ -29,9 +29,9 @@ open class JooqModelatorPlugin : Plugin { ?: throw IncompletePluginConfigurationException("path to the jOOQ generator configuration (jooqConfigPath)")) jooqOutputPath = Paths.get(config.jooqOutputPath - ?: throw IncompletePluginConfigurationException("path to the output directory (jooqOutputDirectory)")) + ?: throw IncompletePluginConfigurationException("path to the output directory (jooqOutputPath)")) - migrationsPaths = config.migrationsPaths?.map({ strPath -> Paths.get(strPath) }) + migrationsPaths = config.migrationsPaths?.map { strPath -> Paths.get(strPath) } ?: throw IncompletePluginConfigurationException("path to the migration files (migrationsPaths)") dockerLabelKey = config.labelKey @@ -63,7 +63,7 @@ open class JooqModelatorPlugin : Plugin { for (migrationPath in task.migrationsPaths) { task.inputs.dir(migrationPath) } - }) + } } diff --git a/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorTask.kt b/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorTask.kt index 914feb0..663edd4 100644 --- a/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorTask.kt +++ b/src/main/kotlin/ch/ayedo/jooqmodelator/gradle/JooqModelatorTask.kt @@ -10,6 +10,7 @@ import ch.ayedo.jooqmodelator.core.configuration.PortMapping import org.gradle.api.DefaultTask import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction import java.net.URL @@ -17,6 +18,7 @@ import java.net.URLClassLoader import java.nio.file.Path +@Suppress("SpellCheckingInspection") open class JooqModelatorTask : DefaultTask() { @InputFile @@ -25,6 +27,7 @@ open class JooqModelatorTask : DefaultTask() { @OutputDirectory lateinit var jooqOutputPath: Path + @InputFiles lateinit var migrationsPaths: List @Input diff --git a/src/test/kotlin/ch/ayedo/jooqmodelator/IntegrationTest.kt b/src/test/kotlin/ch/ayedo/jooqmodelator/IntegrationTest.kt index 04affdd..21baabb 100644 --- a/src/test/kotlin/ch/ayedo/jooqmodelator/IntegrationTest.kt +++ b/src/test/kotlin/ch/ayedo/jooqmodelator/IntegrationTest.kt @@ -1,3 +1,5 @@ +@file:Suppress("SpellCheckingInspection") + package ch.ayedo.jooqmodelator import ch.ayedo.jooqmodelator.IntegrationTest.Database.MARIADB @@ -22,9 +24,44 @@ import java.nio.file.Path import java.nio.file.Paths import java.nio.file.StandardCopyOption.REPLACE_EXISTING +private const val DEFAULT_JOOQ_VERSION = "3.13.2" +private const val PG_DRIVER_VERSION = "42.2.14" +private const val MARIADB_DRIVER_VERSION = "2.6.0" class IntegrationTest { + private enum class Database( + val version: String, + val defaultPort: Int, + val db: String, + val user: String, + val password: String, + val rootPassword: String = "", + private val dialectVersion: String? = version.replace(".", "_"), + private val subdir: String = "" + ) { + POSTGRES( + version = "12.3", + defaultPort = 5432, + db = "postgres", + user = "postgres", + password = "secret", + dialectVersion = null + ), + MARIADB( + version = "10.2", + defaultPort = 3306, + db = "maria", + user = "root", + password = "pass", + rootPassword = "pass", + subdir = "maria" + ); + + val subdirPath get() = "/$subdir" + val dialect get() = dialectVersion?.let { "name_$dialectVersion" } ?: name + } + @Rule private val tempDir = TemporaryFolder().also { it.create() } @@ -35,117 +72,79 @@ class IntegrationTest { @Test fun flywayPostgres() { - val jooqConfig = createJooqConfig(POSTGRES) - - val config = Configuration( - dockerConfig = DockerConfig( - tag = "postgres:9.5", - env = listOf("POSTGRES_DB=postgres", "POSTGRES_USER=postgres", "POSTGRES_PASSWORD=secret"), - portMapping = PortMapping(5432, 5432)), - healthCheckConfig = HealthCheckConfig(), - migrationConfig = MigrationConfig(engine = MigrationEngine.FLYWAY, migrationsPaths = getMigrationPaths("/migrations", "/migrationsB")), - jooqConfigPath = jooqConfig.toPath() - ) + val database = POSTGRES + val config = createJooqConfig(database) + .asConfig(MigrationEngine.FLYWAY) { + newPostgresConfig() + } createBuildFile(config) assertBuildOutcome(SUCCESS) - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/Tab.java") - - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/TabTwo.java") + assertExistingTables(database, "Tab", "Tabtwo") } @Test fun liquibasePostgres() { - - val jooqConfig = createJooqConfig(POSTGRES) - - val config = Configuration( - dockerConfig = DockerConfig( - tag = "postgres:9.5", - env = listOf("POSTGRES_DB=postgres", "POSTGRES_USER=postgres", "POSTGRES_PASSWORD=secret"), - portMapping = PortMapping(5432, 5432)), - healthCheckConfig = HealthCheckConfig(), - migrationConfig = MigrationConfig(engine = MigrationEngine.LIQUIBASE, migrationsPaths = getMigrationPaths("/migrations", "/migrationsB")), - jooqConfigPath = jooqConfig.toPath() - ) + val database = POSTGRES + val config = createJooqConfig(database) + .asConfig(MigrationEngine.LIQUIBASE) { + newPostgresConfig() + } createBuildFile(config) assertBuildOutcome(SUCCESS) - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/Tab.java") - - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/TabTwo.java") + assertExistingTables(database, "Tab", "Tabtwo") } @Test fun flywayMariaDb() { - val jooqConfig = createJooqConfig(MARIADB) - - val config = Configuration( - dockerConfig = DockerConfig( - tag = "mariadb:10.2", - env = listOf("MYSQL_DATABASE=maria", "MYSQL_ROOT_PASSWORD=pass", "MYSQL_PASSWORD=pass"), - portMapping = PortMapping(3306, 3306)), - healthCheckConfig = HealthCheckConfig(), - migrationConfig = MigrationConfig(engine = MigrationEngine.FLYWAY, migrationsPaths = getMigrationPaths("/migrations", "/migrationsB")), - jooqConfigPath = jooqConfig.toPath() - ) + val database = MARIADB + val config = createJooqConfig(database) + .asConfig(MigrationEngine.FLYWAY) { + newMariaDbConfig() + } createBuildFile(config) assertBuildOutcome(SUCCESS) - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/maria/tables/Tab.java") - - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/maria/tables/TabTwo.java") + assertExistingTables(database, "Tab", "Tabtwo") } @Test fun liquibaseMariaDb() { - val jooqConfig = createJooqConfig(MARIADB) - - val config = Configuration( - dockerConfig = DockerConfig( - tag = "mariadb:10.2", - env = listOf("MYSQL_DATABASE=maria", "MYSQL_ROOT_PASSWORD=pass", "MYSQL_PASSWORD=pass"), - portMapping = PortMapping(3306, 3306)), - healthCheckConfig = HealthCheckConfig(), - migrationConfig = MigrationConfig(engine = MigrationEngine.LIQUIBASE, migrationsPaths = getMigrationPaths("/migrations", "/migrationsB")), - jooqConfigPath = jooqConfig.toPath() - ) + val database = MARIADB + val config = createJooqConfig(database) + .asConfig(MigrationEngine.LIQUIBASE) { + newMariaDbConfig() + } createBuildFile(config) assertBuildOutcome(SUCCESS) - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/maria/tables/Tab.java") - - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/maria/tables/TabTwo.java") + assertExistingTables(database, "Tab", "Tabtwo") } + @Test fun incrementalBuildTest() { - val jooqConfig = createJooqConfig(POSTGRES) - - val config = Configuration( - dockerConfig = DockerConfig( - tag = "postgres:9.5", - env = listOf("POSTGRES_DB=postgres", "POSTGRES_USER=postgres", "POSTGRES_PASSWORD=secret"), - portMapping = PortMapping(5432, 5432)), - healthCheckConfig = HealthCheckConfig(), - migrationConfig = MigrationConfig(engine = MigrationEngine.FLYWAY, migrationsPaths = getMigrationPaths("/migrations", "/migrationsB")), - jooqConfigPath = jooqConfig.toPath() - ) + val database = POSTGRES + val config = createJooqConfig(database) + .asConfig(MigrationEngine.FLYWAY) { + newPostgresConfig() + } createBuildFile(config) @@ -153,28 +152,20 @@ class IntegrationTest { assertBuildOutcome(UP_TO_DATE) - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/Tab.java") - - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/TabTwo.java") + assertExistingTables(database, "Tab", "Tabtwo") } @Test fun incrementalBuildChangeFilesTest() { - val jooqConfig = createJooqConfig(POSTGRES) - val additionalMigrationsDir = tempDir.newFolder("migrationsC").toPath() - val config = Configuration( - dockerConfig = DockerConfig( - tag = "postgres:9.5", - env = listOf("POSTGRES_DB=postgres", "POSTGRES_USER=postgres", "POSTGRES_PASSWORD=secret"), - portMapping = PortMapping(5432, 5432)), - healthCheckConfig = HealthCheckConfig(), - migrationConfig = MigrationConfig(engine = MigrationEngine.FLYWAY, migrationsPaths = getMigrationPaths("/migrations") + listOf(additionalMigrationsDir)), - jooqConfigPath = jooqConfig.toPath() - ) + val database = POSTGRES + val config = createJooqConfig(database) + .asConfig(MigrationEngine.FLYWAY, listOf(additionalMigrationsDir)) { + newPostgresConfig() + } createBuildFile(config) @@ -182,51 +173,53 @@ class IntegrationTest { assertBuildOutcome(UP_TO_DATE) - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/Tab.java") - assertFileNotExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/TabTwo.java") + assertExistingTables(database, "Tab") + assertNotExistingTables(database, "Tabtwo") - Files.copy(getMigrationPaths("/migrationsB/V2__flyway_test.sql").first(), additionalMigrationsDir.resolve("V2__flyway_test.sql"), REPLACE_EXISTING) + Files.copy(migrationsFromResources("/migrationsB/V2__flyway_test.sql").first(), additionalMigrationsDir.resolve("V2__flyway_test.sql"), REPLACE_EXISTING) assertBuildOutcome(SUCCESS) - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/Tab.java") - assertFileExists("${tempDir.root.absolutePath}$jooqPackagePath/tables/TabTwo.java") + assertExistingTables(database, "Tabtwo") } + private fun assertExistingTables(database: Database, vararg tableNames: String) = + tableNames.forEach { + Assertions.assertTrue( + fileExists("${tempDir.root.absolutePath}$jooqPackagePath${database.subdirPath}/tables/$it.java"), + "Expected file does not exist." + ) + } + + private fun assertNotExistingTables(database: Database, vararg tableNames: String) = + tableNames.forEach { + Assertions.assertFalse( + fileExists("${tempDir.root.absolutePath}$jooqPackagePath${database.subdirPath}/tables/$it.java"), + "File was expected to not exist." + ) + } + @Test fun changePortsTest() { val firstPort = 2346 - val secondPort = 5432 + val secondPort = POSTGRES.defaultPort - val jooqConfig = createJooqConfig(POSTGRES, port = firstPort) - - val config = Configuration( - dockerConfig = DockerConfig( - tag = "postgres:9.5", - env = listOf("POSTGRES_DB=postgres", "POSTGRES_USER=postgres", "POSTGRES_PASSWORD=secret"), - portMapping = PortMapping(firstPort, 5432)), - healthCheckConfig = HealthCheckConfig(), - migrationConfig = MigrationConfig(engine = MigrationEngine.FLYWAY, migrationsPaths = getMigrationPaths("/migrations", "/migrationsB")), - jooqConfigPath = jooqConfig.toPath() - ) + val config = createJooqConfig(POSTGRES, port = firstPort) + .asConfig(MigrationEngine.FLYWAY) { + newPostgresConfig(hostPort = firstPort) + } createBuildFile(config) assertBuildOutcome(SUCCESS) - val newJooqConfig = createJooqConfig(POSTGRES, port = secondPort) - val newConfig = Configuration( - dockerConfig = DockerConfig( - tag = "postgres:9.5", - env = listOf("POSTGRES_DB=postgres", "POSTGRES_USER=postgres", "POSTGRES_PASSWORD=secret"), - portMapping = PortMapping(secondPort, 5432)), - healthCheckConfig = HealthCheckConfig(), - migrationConfig = MigrationConfig(engine = MigrationEngine.FLYWAY, migrationsPaths = getMigrationPaths("/migrations", "/migrationsB")), - jooqConfigPath = newJooqConfig.toPath() - ) + val newConfig = createJooqConfig(POSTGRES, port = secondPort) + .asConfig(MigrationEngine.FLYWAY) { + newPostgresConfig(hostPort = secondPort) + } createBuildFile(newConfig) @@ -236,10 +229,12 @@ class IntegrationTest { private fun createJooqConfig( database: Database, - databaseName: String? = null, - port: Int? = null, - user: String? = null, - password: String? = null): File { + databaseName: String = database.db, + port: Int = database.defaultPort, + user: String = database.user, + password: String = database.password, + dialect: String = database.dialect + ): File { File("${tempDir.root.absolutePath}/jooqConfig.xml").delete() @@ -247,28 +242,15 @@ class IntegrationTest { val configFilePath = tempDir.root.absolutePath val content = when (database) { - POSTGRES -> jooqPostgresConfig(configFilePath, - port ?: 5432, - databaseName ?: "postgres", - user ?: "postgres", - password ?: "secret") - MARIADB -> jooqMariaDbConfig(configFilePath, - port ?: 3306, - databaseName ?: "maria", - user ?: "root", - password ?: "pass") + POSTGRES -> jooqPostgresConfig(configFilePath, port, databaseName, user, password, dialect) + MARIADB -> jooqMariaDbConfig(configFilePath, port, databaseName, user, password, dialect) } it.writeText(content) } } - private enum class Database { - POSTGRES, - MARIADB - } - - private fun jooqPostgresConfig(target: String, port: Int = 5432, database: String, user: String, password: String) = """ + private fun jooqPostgresConfig(target: String, port: Int = POSTGRES.defaultPort, database: String, user: String, password: String, dialect: String) = """ @@ -286,11 +268,17 @@ class IntegrationTest { $jooqPackageName $target + + + key = 'dialect' + value = '$dialect' + + """.trimIndent() - private fun jooqMariaDbConfig(target: String, port: Int = 3306, database: String, user: String, password: String) = """ + private fun jooqMariaDbConfig(target: String, port: Int = MARIADB.defaultPort, database: String, user: String, password: String, dialect: String) = """ @@ -307,20 +295,18 @@ class IntegrationTest { $jooqPackageName $target + + + key = 'dialect' + value = '$dialect' + + """.trimIndent() - private fun getMigrationPaths(vararg paths: String): List = paths.map({ path -> getResourcePath(path) }) - - private fun assertFileNotExists(fileName: String) { - Assertions.assertFalse(fileExists(fileName), "File was expected to not exist.") - } - - private fun assertFileExists(fileName: String) { - Assertions.assertTrue(fileExists(fileName), "Expected file does not exist.") - } + private fun migrationsFromResources(vararg paths: String): List = paths.map { path -> getResourcePath(path) } private fun fileExists(fileName: String) = File(fileName).exists() @@ -351,7 +337,17 @@ class IntegrationTest { println(result.output) } - private fun buildFileFromConfiguration(config: Configuration, jooqOutputPath: String, jooqVersion: String = "3.11.4", jooqEdition: String = "OSS") = + private fun newPostgresConfig(hostPort: Int = POSTGRES.defaultPort): DockerConfig = DockerConfig( + tag = "postgres:${POSTGRES.version}", + env = listOf("POSTGRES_DB=${POSTGRES.db}", "POSTGRES_USER=${POSTGRES.user}", "POSTGRES_PASSWORD=${POSTGRES.password}"), + portMapping = PortMapping(hostPort, POSTGRES.defaultPort)) + + private fun newMariaDbConfig(): DockerConfig = DockerConfig( + tag = "mariadb:${MARIADB.version}", + env = listOf("MYSQL_DATABASE=${MARIADB.db}", "MYSQL_ROOT_PASSWORD=${MARIADB.rootPassword}", "MYSQL_PASSWORD=${MARIADB.password}"), + portMapping = PortMapping(MARIADB.defaultPort, MARIADB.defaultPort)) + + private fun buildFileFromConfiguration(config: Configuration, jooqOutputPath: String, jooqVersion: String = DEFAULT_JOOQ_VERSION, jooqEdition: String = "OSS") = """ plugins { id 'ch.ayedo.jooqmodelator' @@ -394,9 +390,26 @@ class IntegrationTest { } dependencies { - jooqModelatorRuntime('org.postgresql:postgresql:42.2.4') - jooqModelatorRuntime('org.mariadb.jdbc:mariadb-java-client:2.2.6') + jooqModelatorRuntime('org.postgresql:postgresql:$PG_DRIVER_VERSION') + jooqModelatorRuntime('org.mariadb.jdbc:mariadb-java-client:$MARIADB_DRIVER_VERSION') } """.trimIndent() + + private fun File.asConfig( + migrationEngine: MigrationEngine, + additionalMigrations: List = emptyList(), + dockerConfigProvider: () -> DockerConfig + ): Configuration = + Configuration( + dockerConfig = dockerConfigProvider(), + healthCheckConfig = HealthCheckConfig(), + migrationConfig = MigrationConfig(engine = migrationEngine, migrationsPaths = getMigrationPaths(additionalMigrations)), + jooqConfigPath = toPath() + ) + + private fun getMigrationPaths(additionalMigrations: List): List = if (additionalMigrations.isEmpty()) + migrationsFromResources("/migrations", "/migrationsB") + else + migrationsFromResources("/migrations") + additionalMigrations }