From 19b699324b07e1247f1fd2e0f432695669058688 Mon Sep 17 00:00:00 2001 From: "t.momose" Date: Sat, 21 Sep 2024 17:58:03 +0900 Subject: [PATCH] Add tagDatabase change spec, Install komapper-annotation for check databasechangelog.tag. --- build.gradle.kts | 5 +++ .../liquibase/kotlin/dsl/ChangeSetDsl.kt | 7 ++++ integration-test/build.gradle.kts | 19 ++++++++++ .../main/kotlin/komapper/liquibaseEntities.kt | 37 +++++++++++++++++++ .../test/kotlin/momosetkn/ChangeSetSpec.kt | 23 +++++++++++- .../utils/DatabaseKomapperExtensions.kt | 11 ++++++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 integration-test/src/main/kotlin/komapper/liquibaseEntities.kt create mode 100644 integration-test/src/test/kotlin/momosetkn/utils/DatabaseKomapperExtensions.kt diff --git a/build.gradle.kts b/build.gradle.kts index 6de5f8c4..b273dd38 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -40,6 +40,11 @@ allprojects { ktlint { version.set("1.3.1") + filter { + exclude { entry -> + entry.file.toString().contains("/generated/") + } + } } } diff --git a/dsl/src/main/kotlin/momosetkn/liquibase/kotlin/dsl/ChangeSetDsl.kt b/dsl/src/main/kotlin/momosetkn/liquibase/kotlin/dsl/ChangeSetDsl.kt index 2b5cf9d7..c8f0233e 100644 --- a/dsl/src/main/kotlin/momosetkn/liquibase/kotlin/dsl/ChangeSetDsl.kt +++ b/dsl/src/main/kotlin/momosetkn/liquibase/kotlin/dsl/ChangeSetDsl.kt @@ -1490,6 +1490,13 @@ class ChangeSetDsl( changeSetSupport.addChange(change) } + /** + * We will tag the current state. + * It will be used for rollback. + * [official-document](https://docs.liquibase.com/commands/utility/tag.html) + * + * @param tag name of tag + */ fun tagDatabase(tag: String) { val change = changeSetSupport.createChange("tagDatabase") as TagDatabaseChange change.tag = tag.evalExpressions(changeLog) diff --git a/integration-test/build.gradle.kts b/integration-test/build.gradle.kts index fda03ff5..e727ffe3 100644 --- a/integration-test/build.gradle.kts +++ b/integration-test/build.gradle.kts @@ -3,6 +3,11 @@ val kotestVersion = rootProject.properties["kotestVersion"] as String val testcontainersVersion = rootProject.properties["testcontainersVersion"] as String val komapperVersion = "3.0.0" +plugins { + id("com.google.devtools.ksp") version "2.0.20-1.0.25" + id("org.komapper.gradle") version "3.0.0" +} + dependencies { implementation(project(":dsl")) implementation(project(":script-serializer")) @@ -20,6 +25,12 @@ dependencies { // komapper implementation("org.komapper:komapper-core:$komapperVersion") implementation("org.komapper:komapper-jdbc:$komapperVersion") + implementation("org.komapper:komapper-annotation:$komapperVersion") + platform("org.komapper:komapper-platform:$komapperVersion").let { + implementation(it) + ksp(it) + } + ksp("org.komapper:komapper-processor") implementation("org.komapper:komapper-dialect-postgresql-jdbc:$komapperVersion") // test @@ -41,3 +52,11 @@ tasks.test { useJUnitPlatform() systemProperty("kotest.framework.classpath.scanning.config.disable", "true") } + +tasks { + withType().configureEach { + compilerOptions { + freeCompilerArgs.add("-opt-in=org.komapper.annotation.KomapperExperimentalAssociation") + } + } +} diff --git a/integration-test/src/main/kotlin/komapper/liquibaseEntities.kt b/integration-test/src/main/kotlin/komapper/liquibaseEntities.kt new file mode 100644 index 00000000..5a188892 --- /dev/null +++ b/integration-test/src/main/kotlin/komapper/liquibaseEntities.kt @@ -0,0 +1,37 @@ +@file:Suppress("ktlint:standard:filename") + +package komapper + +import org.komapper.annotation.KomapperColumn +import org.komapper.annotation.KomapperEntity +import org.komapper.annotation.KomapperId +import org.komapper.annotation.KomapperTable +import java.time.LocalDateTime + +@KomapperEntity +@KomapperTable(name = "databasechangelog") +data class Databasechangelog( + @KomapperId @KomapperColumn(name = "id") val id: String, + @KomapperColumn(name = "author") val author: String, + @KomapperColumn(name = "filename") val filename: String, + @KomapperColumn(name = "dateexecuted") val dateexecuted: LocalDateTime, + @KomapperColumn(name = "orderexecuted") val orderexecuted: Int, + @KomapperColumn(name = "exectype") val exectype: String, + @KomapperColumn(name = "md5sum") val md5sum: String?, + @KomapperColumn(name = "description") val description: String?, + @KomapperColumn(name = "comments") val comments: String?, + @KomapperColumn(name = "tag") val tag: String?, + @KomapperColumn(name = "liquibase") val liquibase: String?, + @KomapperColumn(name = "contexts") val contexts: String?, + @KomapperColumn(name = "labels") val labels: String?, + @KomapperColumn(name = "deployment_id") val deploymentId: String? +) + +@KomapperEntity +@KomapperTable(name = "databasechangeloglock") +data class Databasechangeloglock( + @KomapperId @KomapperColumn(name = "id") val id: Int, + @KomapperColumn(name = "locked") val locked: Boolean, + @KomapperColumn(name = "lockgranted") val lockgranted: LocalDateTime?, + @KomapperColumn(name = "lockedby") val lockedby: String? +) diff --git a/integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt b/integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt index a43d2b7f..d5dd2250 100644 --- a/integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt @@ -2,11 +2,16 @@ package momosetkn import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import komapper.databasechangelog import momosetkn.liquibase.client.LiquibaseClient import momosetkn.utils.DDLUtils.shouldBeEqualDdl import momosetkn.utils.Database +import momosetkn.utils.DatabaseKomapperExtensions.komapperDb import momosetkn.utils.InterchangeableChangeLog import momosetkn.utils.set +import org.komapper.core.dsl.Meta +import org.komapper.core.dsl.QueryDsl class ChangeSetSpec : FunSpec({ beforeEach { @@ -41,7 +46,6 @@ class ChangeSetSpec : FunSpec({ timeout = "10s" ) { arg("ps") -// arg("-h") } } } @@ -185,4 +189,21 @@ class ChangeSetSpec : FunSpec({ ) } } + context("tagDatabase") { + InterchangeableChangeLog.set { + changeSet(author = "user", id = "100") { + tagDatabase("example_tag1") + } + } + test("can migrate") { + subject() + val db = Database.komapperDb() + val d = Meta.databasechangelog + val results = db.runQuery { + QueryDsl.from(d) + } + results.size shouldBe 1 + results[0].tag shouldBe "example_tag1" + } + } }) diff --git a/integration-test/src/test/kotlin/momosetkn/utils/DatabaseKomapperExtensions.kt b/integration-test/src/test/kotlin/momosetkn/utils/DatabaseKomapperExtensions.kt new file mode 100644 index 00000000..12fd5693 --- /dev/null +++ b/integration-test/src/test/kotlin/momosetkn/utils/DatabaseKomapperExtensions.kt @@ -0,0 +1,11 @@ +package momosetkn.utils + +import org.komapper.jdbc.JdbcDatabase + +object DatabaseKomapperExtensions { + fun Database.komapperDb() = JdbcDatabase( + url = this.startedContainer.jdbcUrl, + user = this.startedContainer.username, + password = this.startedContainer.password, + ) +}