Skip to content

Commit

Permalink
chore(backend): attach flyway to a copy of file from database folder
Browse files Browse the repository at this point in the history
Following rohanprabhu/kotlin-dsl-gradle-jooq-plugin#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
  • Loading branch information
davinkevin committed Jun 26, 2020
1 parent 0cc979a commit aa92bfc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
21 changes: 14 additions & 7 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -76,11 +76,17 @@ gitProperties {
dotGitDirectory = "${project.rootDir}/../.git"
}

tasks.register<Copy>("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 {
Expand All @@ -89,6 +95,8 @@ jooqGenerator {

configuration("primary", project.sourceSets.getByName("main")) {

databaseSources = listOf(db.sqlFiles)

configuration = jooqCodegenConfiguration {
jdbc {
url = db.url
Expand Down Expand Up @@ -130,6 +138,9 @@ jooqGenerator {
}
}

project.tasks["flywayMigrate"].dependsOn("copyMigrations")
project.tasks["jooq-codegen-primary"].dependsOn("flywayMigrate")

tasks.withType<Test> {
useJUnitPlatform()
systemProperty("user.timezone", "UTC")
Expand All @@ -148,10 +159,6 @@ tasks.withType<KotlinCompile> {
}
}

@Suppress("PropertyName")
val `jooq-codegen-primary` by project.tasks
`jooq-codegen-primary`.dependsOn("flywayMigrate")

jib {
val imageTags: Set<String>? by project.extra
val providedTag = project.findProperty("tag")?.toString()
Expand Down
6 changes: 1 addition & 5 deletions backend/buildSrc/src/main/kotlin/DatabaseParameters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ data class DatabaseParameters(
val url: String,
val user: String,
val password: String,
val sqlFiles: List<String> = listOf("filesystem:../database/migrations")
val sqlFiles: String = "/flyway/migrations/"
)

fun databaseParameters(project: Project): DatabaseParameters {
return project.extensions.extraProperties["databaseParameters"] as DatabaseParameters
}
3 changes: 2 additions & 1 deletion backend/gradle/profile-ci.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
))
}

Expand Down
3 changes: 2 additions & 1 deletion backend/gradle/profile-default.gradle.kts
Original file line number Diff line number Diff line change
@@ -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/"
))
3 changes: 2 additions & 1 deletion backend/gradle/profile-skaffold.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
))
}

0 comments on commit aa92bfc

Please sign in to comment.