Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-architect client module #51

Merged
merged 9 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,24 @@ Client module can execute Liquibase commands programmatically.

example
```kotlin
val client = LiquibaseClient {
globalArgs {
configureLiquibase {
global {
general {
showBanner = false
}
}
}
client.update(
val database = LiquibaseDatabaseFactory.create(
driver = "org.postgresql.Driver",
url = "jdbc:postgresql://127.0.0.1:5432/test",
username = "root",
password = "test",
)
val liquibaseClient = LiquibaseClient(
changelogFile = "db.changelog.all.kts",
database = database,
)
liquibaseClient.update()
```

### Use Komapper on customChange
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ val libraryProjects =
"script-serializer",
"compiled-serializer",
// client
"command-client",
"client",
// custom-change
"custom-komapper-jdbc-change",
Expand Down
8 changes: 6 additions & 2 deletions client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
val liquibaseVersion = rootProject.properties["liquibaseVersion"] as String
// liquibase-kotlin-client is tested only this version
val latestLiquibaseVersion = "4.29.2"
val kotestVersion = rootProject.properties["kotestVersion"] as String
val slf4jVersion = rootProject.properties["slf4jVersion"] as String
val kotlinVersion = rootProject.properties["kotlinVersion"] as String

dependencies {
// liquibase
implementation("org.liquibase:liquibase-core:$liquibaseVersion")
implementation("org.liquibase:liquibase-core:$latestLiquibaseVersion")
// log
api("org.slf4j:slf4j-api:$slf4jVersion")

// test
testImplementation("io.kotest:kotest-framework-engine-jvm:$kotestVersion")
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
// reflection
testImplementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,49 @@ package momosetkn.liquibase.client

typealias ConfigureLiquibaseDslBlock = ConfigureLiquibaseDsl.() -> Unit
typealias LiquibaseGlobalArgsDslBlock = LiquibaseGlobalArgsDsl.() -> Unit
typealias LiquibaseCommandArgsDslBlock = LiquibaseCommandArgsDsl.() -> Unit
typealias LiquibaseSystemEnvArgsDslBlock = LiquibaseSystemEnvArgsDsl.() -> Unit
typealias LiquibaseSystemEnvArgsDslBlock = LiquibaseSystemArgs.() -> Unit

interface ConfigureLiquibase {
// require override
val configureBlock: ConfigureLiquibaseDslBlock
private class ConfigureLiquibase(
private val configureBlock: ConfigureLiquibaseDslBlock
) {
fun configure() {
val configuredArgs = configuredArgs()
val allArgs = getAllArgs(configuredArgs)
allArgs
.forEach { (key, value) ->
if (value == null) {
System.clearProperty(key)
} else {
System.setProperty(key, value)
}
}
}

val configuredArgs: ConfiguredArgs
get() {
val dsl = ConfigureLiquibaseDsl()
return dsl(configureBlock)
}
private fun configuredArgs(): ConfiguredArgs {
val dsl = ConfigureLiquibaseDsl()
return dsl(configureBlock)
}

fun getGlobalArgs(): List<Pair<String, String>> {
val dsl = LiquibaseGlobalArgsDsl()
val args = configuredArgs.globalArgsDslBlock?.let { dsl(it) } ?: emptyList()
return args.flatMap { it.serialize() }
private fun getAllArgs(configuredArgs: ConfiguredArgs): List<Pair<String, String?>> {
return getGlobalArgs(configuredArgs) + getSystemEnvArgs(configuredArgs)
}

fun getCommandArgs(): List<Pair<String, String>> {
val dsl = LiquibaseCommandArgsDsl()
val args = configuredArgs.commandArgsDslBlock?.let { dsl(it) } ?: emptyList()
private fun getGlobalArgs(configuredArgs: ConfiguredArgs): List<Pair<String, String?>> {
val dsl = LiquibaseGlobalArgsDsl()
val args = configuredArgs.globalDslBlock?.let { dsl(it) } ?: emptyList()
return args.flatMap { it.serialize() }
}

fun setSystemEnvArgs() {
val dsl = LiquibaseSystemEnvArgsDsl()
val args = configuredArgs.systemEnvArgsDslBlock?.let { dsl(it) } ?: emptyList()
args
.flatMap { it.serialize() }
.forEach { (key, value) ->
System.setProperty(key, value)
}
private fun getSystemEnvArgs(configuredArgs: ConfiguredArgs): List<Pair<String, String?>> {
val liquibaseSystemArgs = LiquibaseSystemArgs()
val args = configuredArgs.systemDslBlock?.let {
it(liquibaseSystemArgs)
liquibaseSystemArgs.serialize()
} ?: emptyList()
return args
}
}

fun configureLiquibase(
block: ConfigureLiquibaseDslBlock,
) = ConfigureLiquibase(block).configure()
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,20 @@ class ConfigureLiquibaseDsl {
return configuredArgs
}

fun globalArgs(block: LiquibaseGlobalArgsDslBlock) {
fun global(block: LiquibaseGlobalArgsDslBlock) {
configuredArgs = configuredArgs.copy(
globalArgsDslBlock = block,
globalDslBlock = block,
)
}

fun commandArgs(block: LiquibaseCommandArgsDslBlock) {
fun system(block: LiquibaseSystemEnvArgsDslBlock) {
configuredArgs = configuredArgs.copy(
commandArgsDslBlock = block,
)
}

fun systemEnvArg(block: LiquibaseSystemEnvArgsDslBlock) {
configuredArgs = configuredArgs.copy(
systemEnvArgsDslBlock = block,
systemDslBlock = block,
)
}
}

data class ConfiguredArgs(
var globalArgsDslBlock: LiquibaseGlobalArgsDslBlock? = null,
var commandArgsDslBlock: LiquibaseCommandArgsDslBlock? = null,
var systemEnvArgsDslBlock: LiquibaseSystemEnvArgsDslBlock? = null,
var globalDslBlock: LiquibaseGlobalArgsDslBlock? = null,
var systemDslBlock: LiquibaseSystemEnvArgsDslBlock? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package momosetkn.liquibase.client

internal object DateUtils {
private val zoneOffset = java.time.ZoneOffset.ofHours(0)

fun java.time.LocalDateTime.toJavaUtilDate(): java.util.Date {
return java.util.Date.from(this.toInstant(zoneOffset))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package momosetkn.liquibase.client

import liquibase.Contexts
import liquibase.LabelExpression
import liquibase.Liquibase
import liquibase.Scope
import liquibase.database.Database
import liquibase.exception.LiquibaseException
import liquibase.resource.ResourceAccessor
import java.io.Writer

internal class ExtendedLiquibase(
changeLogFile: String,
database: Database,
resourceAccessor: ResourceAccessor = Scope.getCurrentScope().resourceAccessor,
) : Liquibase(changeLogFile, resourceAccessor, database,) {
// to public futureRollbackSQL method, because the original futureRollbackSQL method is protected.
@Throws(LiquibaseException::class)
fun extendedFutureRollbackSQL(
count: Int? = null,
tag: String? = null,
contexts: Contexts? = null,
labelExpression: LabelExpression?,
output: Writer? = null,
checkLiquibaseTables: Boolean
) {
return futureRollbackSQL(count, tag, contexts, labelExpression, output, checkLiquibaseTables)
}
}
Loading