From 2d9189cd779958b0e80fee7218183cf59a9ce7f0 Mon Sep 17 00:00:00 2001 From: Bean Date: Tue, 6 Feb 2024 22:58:51 +0900 Subject: [PATCH] =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=9A=A9=20?= =?UTF-8?q?=EC=95=B1=20=EC=84=B8=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 81 ++++++++++++++++++++++++++++------------------- db.yaml | 9 ++++++ gradle.properties | 2 ++ 3 files changed, 60 insertions(+), 32 deletions(-) create mode 100644 db.yaml create mode 100644 gradle.properties diff --git a/build.gradle.kts b/build.gradle.kts index fce434e..6d3d74a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,8 @@ +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import java.io.FileInputStream plugins { id("org.springframework.boot") version "3.2.2" @@ -10,6 +14,12 @@ plugins { id("org.liquibase.gradle") version "2.2.1" } +buildscript { + dependencies { + classpath("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.1") + } +} + group = "com.example" version = "0.0.1-SNAPSHOT" @@ -27,6 +37,7 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-validation") + liquibaseRuntime("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.1") liquibaseRuntime("org.liquibase:liquibase-core") liquibaseRuntime("org.liquibase.ext:liquibase-hibernate6:4.25.1") liquibaseRuntime("info.picocli:picocli:4.6.3") @@ -47,45 +58,51 @@ tasks.withType { useJUnitPlatform() } +val profile: String by project +val profileDbConf = getDbConf(profile) +val referencedProfile: String? by project +val referencedProfileDbConf = getDbConf(referencedProfile) +val defaultChangeLogFile: String by project + liquibase { - val profile = System.getProperty("profile") ?: "local" - -// TODO: db.yaml objectMapper 로 읽어서 세팅? - activities.register("local") { - this.arguments = mapOf( - "url" to "jdbc:postgresql://localhost:15432/localdb", - "username" to "flower", - "password" to "flower" - ) - } - activities.register("dev") { - this.arguments = mapOf( - "url" to "jdbc:postgresql://flower-fragrance-stage.cluster-cbth1bnhfbdg.ap-northeast-2.rds.amazonaws.com:5432/devdb", - "username" to "anypoint", - "password" to "*emflaj*" - ) + activities.register(profile) { + this.arguments = (profileDbConf?.toMap() ?: emptyMap()) + + (referencedProfileDbConf?.toReferenceMap() ?: emptyMap()) + + mapOf("changelogFile" to defaultChangeLogFile) } runList = profile } -fun addArguments(name: String, args: Map) { - val activity = liquibase.activities.getByName(name) - activity.name = name - val activityArgs = activity.arguments as Map<*, *> - val newArgs = mutableMapOf() - newArgs.putAll(activityArgs.map { it.key as String to it.value as String }) - newArgs.putAll(args) - - newArgs.forEach { - println("arguments[\"${it.key}\"]=\"${it.value}\"") - } +data class DbConf( + val url: String? = null, + val username: String? = null, + val password: String? = null +) { + fun toMap(): Map = mapOf( + "url" to url, + "username" to username, + "password" to password + ) + + fun toReferenceMap(): Map = mapOf( + "reference-url" to url, + "reference-username" to username, + "reference-password" to password + ) +} - activity.arguments = newArgs +fun getDbConf(profile: String?): DbConf? = profile?.let { + ObjectMapper(YAMLFactory()) + .readValue( + FileInputStream(File(projectDir, "db.yaml")), + object : TypeReference>() {} + )[profile] } tasks.getByName("generateChangelog").doFirst { - val changeLogFile = "src/main/resources/liquibase/postgres/changelog/generated.yaml" - delete(changeLogFile) - val name = System.getProperty("name") ?: "local" - addArguments(name, mapOf("changeLogFile" to changeLogFile)) + delete(defaultChangeLogFile) } + +tasks.getByName("diffChangelog").doFirst { + delete(defaultChangeLogFile) +} \ No newline at end of file diff --git a/db.yaml b/db.yaml new file mode 100644 index 0000000..3dffa8f --- /dev/null +++ b/db.yaml @@ -0,0 +1,9 @@ +local: + url: jdbc:postgresql://localhost:15432/localdb + username: flower + password: flower + +dev: + url: jdbc:postgresql://flower-fragrance-stage.cluster-cbth1bnhfbdg.ap-northeast-2.rds.amazonaws.com:5432/devdb + username: anypoint + password: '*emflaj*' diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..c398873 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +profile=local +defaultChangeLogFile=src/main/resources/liquibase/postgres/changelog/generated.yaml \ No newline at end of file