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

Support absolute-paths in serializers #69

Merged
merged 3 commits into from
Oct 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class KotlinCompiledChangeLogSerializer : ChangeLogSerializer {
val filePath = (changeSets[0] as? ChangeSet)?.filePath
?: return ""
// \a\b\c\Clazz.kt -> a.b.c
// /user/home/b/c/Clazz.kt -> a.b.c
return filePath
.toUnixFileSeparator()
.removePrefix(sourceRootPath.toString().toUnixFileSeparator())
.split(sourceRootPath.toString().toUnixFileSeparator()).last()
.removePrefix("/")
.substringBeforeLast("/")
.replace("/", ".")
Expand Down
1 change: 0 additions & 1 deletion integration-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ configurations.all {

tasks.test {
useJUnitPlatform()
systemProperty("kotest.framework.classpath.scanning.config.disable", "true")
systemProperty("liquibaseVersion", liquibaseVersion)
}

Expand Down
5 changes: 1 addition & 4 deletions integration-test/src/test/kotlin/momosetkn/ChangeLogSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import org.komapper.core.dsl.query.single

class ChangeLogSpec : FunSpec({
beforeEach {
DatabaseServer.start()
}
afterEach {
DatabaseServer.clear()
DatabaseServer.startAndClear()
}
val client = LiquibaseCommandClient {
globalArgs {
Expand Down
5 changes: 1 addition & 4 deletions integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import org.komapper.core.dsl.query.single

class ChangeSetSpec : FunSpec({
beforeEach {
DatabaseServer.start()
}
afterEach {
DatabaseServer.clear()
DatabaseServer.startAndClear()
}
val client = LiquibaseCommandClient {
globalArgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import java.nio.file.Paths

class CustomChangeMigrationSpec : FunSpec({
beforeSpec {
DatabaseServer.start()
KotlinCompiledChangeLogSerializer.sourceRootPath = Paths.get(Constants.TEST_RESOURCE_DIR)
}
afterSpec {
DatabaseServer.clear()
beforeEach {
DatabaseServer.startAndClear()
}

context("Migrate and serialize") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import java.util.UUID

class CustomJooqChangeSetSpec : FunSpec({
beforeEach {
DatabaseServer.start()
}
afterEach {
DatabaseServer.clear()
DatabaseServer.startAndClear()
}
val client = LiquibaseCommandClient {
globalArgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import java.util.UUID

class CustomKomapperJdbcChangeSetSpec : FunSpec({
beforeEach {
DatabaseServer.start()
}
afterEach {
DatabaseServer.clear()
DatabaseServer.startAndClear()
}
val client = LiquibaseCommandClient {
globalArgs {
Expand Down
16 changes: 16 additions & 0 deletions integration-test/src/test/kotlin/momosetkn/KotestProjectConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package momosetkn

import io.kotest.core.config.AbstractProjectConfig
import momosetkn.liquibase.client.configureLiquibase

class KotestProjectConfig : AbstractProjectConfig() {
init {
configureLiquibase {
global {
general {
showBanner = false
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.kotest.core.spec.style.FunSpec
import momosetkn.liquibase.changelogs.CompiledDatabaseChangelogAll
import momosetkn.liquibase.client.LiquibaseClient
import momosetkn.liquibase.client.LiquibaseDatabaseFactory
import momosetkn.liquibase.client.configureLiquibase
import momosetkn.liquibase.kotlin.serializer.KotlinCompiledChangeLogSerializer
import momosetkn.utils.Constants
import momosetkn.utils.DDLUtils.sql
Expand All @@ -18,22 +17,14 @@ import java.nio.file.Paths

class KotlinCompiledMigrateAndSerializeSpec : FunSpec({
beforeSpec {
DatabaseServer.start()
KotlinCompiledChangeLogSerializer.sourceRootPath = Paths.get(Constants.TEST_RESOURCE_DIR)
configureLiquibase {
global {
general {
showBanner = false
}
}
}
}
afterSpec {
DatabaseServer.clear()
beforeEach {
DatabaseServer.startAndClear()
}

context("Migrate and serialize") {
test("can migrate") {
context("Serialize output file is relative path") {
test("can migrate and serialize") {
val container = DatabaseServer.startedContainer
val database = LiquibaseDatabaseFactory.create(
driver = container.driver,
Expand Down Expand Up @@ -78,6 +69,53 @@ class KotlinCompiledMigrateAndSerializeSpec : FunSpec({
actual shouldMatchWithoutLineBreaks expect
}
}

context("Serialize output file is absolute path") {
test("can migrate and serialize") {
val container = DatabaseServer.startedContainer
val database = LiquibaseDatabaseFactory.create(
driver = container.driver,
url = container.jdbcUrl,
username = container.username,
password = container.password,
)
val liquibaseClient = LiquibaseClient(
changeLogFile = PARSER_INPUT_CHANGELOG,
database = database,
)
println("${this::class.simpleName} -- before update")
liquibaseClient.update()
println("${this::class.simpleName} -- before rollback")
liquibaseClient.rollback(tagToRollBackTo = "started")
println("${this::class.simpleName} -- before update(2)")
liquibaseClient.update()
val actualSerializedChangeLogFile =
Paths.get(Constants.TEST_RESOURCE_DIR, SERIALIZER_ACTUAL_CHANGELOG)
val f = actualSerializedChangeLogFile.toFile()
if (f.exists()) f.delete()
val generateLiquibaseClient = LiquibaseClient(
changeLogFile = f.absolutePath.toString(), // absolute
database = database,
)
println("${this::class.simpleName} -- before generateChangeLog")
val baos = ByteArrayOutputStream()
generateLiquibaseClient.generateChangeLog(
outputStream = PrintStream(baos),
)
val generateResult = baos.toString()
println(generateResult) // empty

// check database
val expectedDdl = getResourceAsString(PARSER_EXPECT_DDL)
DatabaseServer.generateDdl().toMainDdl() shouldMatchWithoutLineBreaks sql(expectedDdl)

// check serializer
val actual = f.readText().maskingChangeSet()
val expect = getResourceAsString(SERIALIZER_EXPECT_CHANGELOG)
.maskingChangeSet()
actual shouldMatchWithoutLineBreaks expect
}
}
}) {
companion object {
private val changeSetRegex = Regex("""changeSet\(author = "(.+)", id = "(\d+)-(\d)"\) \{""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package momosetkn
import io.kotest.core.spec.style.FunSpec
import momosetkn.liquibase.client.LiquibaseClient
import momosetkn.liquibase.client.LiquibaseDatabaseFactory
import momosetkn.liquibase.client.configureLiquibase
import momosetkn.utils.Constants
import momosetkn.utils.DDLUtils.sql
import momosetkn.utils.DDLUtils.toMainDdl
Expand All @@ -15,22 +14,12 @@ import java.io.PrintStream
import java.nio.file.Paths

class KotlinScriptMigrateAndSerializeSpec : FunSpec({
beforeSpec {
DatabaseServer.start()
configureLiquibase {
global {
general {
showBanner = false
}
}
}
}
afterSpec {
DatabaseServer.clear()
beforeEach {
DatabaseServer.startAndClear()
}

context("Migrate and serialize") {
test("can migrate") {
context("Serialize output file is relative path") {
test("can migrate and serialize") {
val container = DatabaseServer.startedContainer
val database = LiquibaseDatabaseFactory.create(
driver = container.driver,
Expand Down Expand Up @@ -75,6 +64,53 @@ class KotlinScriptMigrateAndSerializeSpec : FunSpec({
actual shouldMatchWithoutLineBreaks expect
}
}

context("Serialize output file is absolute path") {
test("can migrate and serialize") {
val container = DatabaseServer.startedContainer
val database = LiquibaseDatabaseFactory.create(
driver = container.driver,
url = container.jdbcUrl,
username = container.username,
password = container.password,
)
val liquibaseClient = LiquibaseClient(
changeLogFile = PARSER_INPUT_CHANGELOG,
database = database,
)
println("${this::class.simpleName} -- before update")
liquibaseClient.update()
println("${this::class.simpleName} -- before rollback")
liquibaseClient.rollback(tagToRollBackTo = "started")
println("${this::class.simpleName} -- before update(2)")
liquibaseClient.update()
val actualSerializedChangeLogFile =
Paths.get(Constants.TEST_RESOURCE_DIR, SERIALIZER_ACTUAL_CHANGELOG)
val f = actualSerializedChangeLogFile.toFile()
if (f.exists()) f.delete()
val generateLiquibaseClient = LiquibaseClient(
changeLogFile = f.absolutePath.toString(), // absolute
database = database,
)
println("${this::class.simpleName} -- before generateChangeLog")
val baos = ByteArrayOutputStream()
generateLiquibaseClient.generateChangeLog(
outputStream = PrintStream(baos),
)
val generateResult = baos.toString()
println(generateResult) // empty

// check database
val expectedDdl = getResourceAsString(PARSER_EXPECT_DDL)
DatabaseServer.generateDdl().toMainDdl() shouldMatchWithoutLineBreaks sql(expectedDdl)

// check serializer
val actual = f.readText().maskingChangeSet()
val expect = getResourceAsString(SERIALIZER_EXPECT_CHANGELOG)
.maskingChangeSet()
actual shouldMatchWithoutLineBreaks expect
}
}
}) {
companion object {
private val changeSetRegex = Regex("""changeSet\(author = "(.+)", id = "(\d+)-(\d)"\) \{""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import liquibase.Scope
import liquibase.changelog.ChangeLogParameters
import momosetkn.liquibase.client.LiquibaseClient
import momosetkn.liquibase.client.LiquibaseDatabaseFactory
import momosetkn.liquibase.client.configureLiquibase
import momosetkn.liquibase.kotlin.parser.KotlinCompiledLiquibaseChangeLogParser
import momosetkn.liquibase.kotlin.serializer.KotlinCompiledChangeLogSerializer
import momosetkn.utils.Constants
Expand All @@ -32,17 +31,7 @@ class LiquibaseClientSpec : FunSpec({
KotlinCompiledChangeLogSerializer.sourceRootPath = Paths.get(Constants.TEST_RESOURCE_DIR)
}
beforeEach {
DatabaseServer.start()
configureLiquibase {
global {
general {
showBanner = false
}
}
}
}
afterEach {
DatabaseServer.clear()
DatabaseServer.startAndClear()
}

fun liquibaseClient(changeLogFile: String? = null): LiquibaseClient {
Expand Down
4 changes: 4 additions & 0 deletions integration-test/src/test/resources/kotest.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kotest.framework.classpath.scanning.config.disable=true
kotest.framework.classpath.scanning.autoscan.disable=true
kotest.framework.config.fqn=momosetkn.KotestProjectConfig
kotest.assertions.collection.print.size=100
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object DatabaseServer {
}

@Synchronized
fun start() {
fun startAndClear() {
if (this.container == null) {
this.container = createServer()
}
Expand Down