From aa92bfce14a6b92e1c1e3823ac565b09fd8b6bea Mon Sep 17 00:00:00 2001 From: davinkevin Date: Sun, 21 Jun 2020 18:29:10 +0200 Subject: [PATCH] chore(backend): attach flyway to a copy of file from database folder Following https://github.com/rohanprabhu/kotlin-dsl-gradle-jooq-plugin/issues/23, we choose to copy files into buildDir before flywayMigrate operations. Thanks to that, we can use the same url between flyWay and JOOQ plugin. Closes #88 --- backend/build.gradle.kts | 21 ++++++++++++------- .../src/main/kotlin/DatabaseParameters.kt | 6 +----- backend/gradle/profile-ci.gradle.kts | 3 ++- backend/gradle/profile-default.gradle.kts | 3 ++- backend/gradle/profile-skaffold.gradle.kts | 3 ++- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index f50aa304e..c959c2eb5 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -1,10 +1,10 @@ -import com.gitlab.davinkevin.podcastserver.backend.build.databaseParameters +import com.gitlab.davinkevin.podcastserver.backend.build.DatabaseParameters import com.rohanprabhu.gradle.plugins.kdjooq.* import org.gradle.internal.deprecation.DeprecatableConfiguration import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import java.time.OffsetDateTime import java.time.format.DateTimeFormatter -import com.gitlab.davinkevin.podcastserver.backend.build.* +import java.nio.file.* plugins { id("org.springframework.boot") version "2.3.1.RELEASE" @@ -76,11 +76,17 @@ gitProperties { dotGitDirectory = "${project.rootDir}/../.git" } +tasks.register("copyMigrations") { + from("${project.rootDir}/../database/migrations/") + include("*.sql") + into(db.sqlFiles) +} + flyway { url = db.url user = db.user password = db.password - locations = db.sqlFiles.toTypedArray() + locations = arrayOf("filesystem:${db.sqlFiles}") } jooqGenerator { @@ -89,6 +95,8 @@ jooqGenerator { configuration("primary", project.sourceSets.getByName("main")) { + databaseSources = listOf(db.sqlFiles) + configuration = jooqCodegenConfiguration { jdbc { url = db.url @@ -130,6 +138,9 @@ jooqGenerator { } } +project.tasks["flywayMigrate"].dependsOn("copyMigrations") +project.tasks["jooq-codegen-primary"].dependsOn("flywayMigrate") + tasks.withType { useJUnitPlatform() systemProperty("user.timezone", "UTC") @@ -148,10 +159,6 @@ tasks.withType { } } -@Suppress("PropertyName") -val `jooq-codegen-primary` by project.tasks -`jooq-codegen-primary`.dependsOn("flywayMigrate") - jib { val imageTags: Set? by project.extra val providedTag = project.findProperty("tag")?.toString() diff --git a/backend/buildSrc/src/main/kotlin/DatabaseParameters.kt b/backend/buildSrc/src/main/kotlin/DatabaseParameters.kt index 3615f5a27..5e150daa4 100644 --- a/backend/buildSrc/src/main/kotlin/DatabaseParameters.kt +++ b/backend/buildSrc/src/main/kotlin/DatabaseParameters.kt @@ -6,9 +6,5 @@ data class DatabaseParameters( val url: String, val user: String, val password: String, - val sqlFiles: List = listOf("filesystem:../database/migrations") + val sqlFiles: String = "/flyway/migrations/" ) - -fun databaseParameters(project: Project): DatabaseParameters { - return project.extensions.extraProperties["databaseParameters"] as DatabaseParameters -} diff --git a/backend/gradle/profile-ci.gradle.kts b/backend/gradle/profile-ci.gradle.kts index 96b4974b0..88cf33d1b 100644 --- a/backend/gradle/profile-ci.gradle.kts +++ b/backend/gradle/profile-ci.gradle.kts @@ -10,7 +10,8 @@ if (env.containsKey("POSTGRES_DB") && env.containsKey("POSTGRES_USER") && env.co val databaseParameters by extra(DatabaseParameters( url = "jdbc:postgresql://postgres:5432/${env["POSTGRES_DB"]}", user = env["POSTGRES_USER"]!!, - password = env["POSTGRES_PASSWORD"]!! + password = env["POSTGRES_PASSWORD"]!!, + sqlFiles = "$buildDir/flyway/migrations/" )) } diff --git a/backend/gradle/profile-default.gradle.kts b/backend/gradle/profile-default.gradle.kts index da51b6042..82bd08dd1 100644 --- a/backend/gradle/profile-default.gradle.kts +++ b/backend/gradle/profile-default.gradle.kts @@ -1,5 +1,6 @@ val databaseParameters by extra(com.gitlab.davinkevin.podcastserver.backend.build.DatabaseParameters( url = "jdbc:postgresql://postgres:5432/podcast-server", user = "podcast-server-user", - password = "nAAdo5wNs7WEF1UxUobpJDfS9Si62PHa" + password = "nAAdo5wNs7WEF1UxUobpJDfS9Si62PHa", + sqlFiles = "$buildDir/flyway/migrations/" )) diff --git a/backend/gradle/profile-skaffold.gradle.kts b/backend/gradle/profile-skaffold.gradle.kts index 6d17af3b2..f8ed55d8a 100644 --- a/backend/gradle/profile-skaffold.gradle.kts +++ b/backend/gradle/profile-skaffold.gradle.kts @@ -6,7 +6,8 @@ if (System.getenv("SKAFFOLD") != null || (skaffold?.toBoolean() == true)) { val databaseParameters by extra(DatabaseParameters( url = "jdbc:postgresql://postgres:${System.getenv("DATABASE_PORT")}/${System.getenv("DATABASE_NAME")}", user = System.getenv("DATABASE_USERNAME"), - password = System.getenv("DATABASE_PASSWORD") + password = System.getenv("DATABASE_PASSWORD"), + sqlFiles = "$buildDir/flyway/migrations/" )) }