Liquibase kotlin(DSL, Wrapper client, ORM integration) Help

Get started

This explains how-to migration by liquibase with Kotlin-DSL here.

Let's install

Update your build.gradle.kts.

dependencies { // liquibase implementation("org.liquibase:liquibase-core:4.32.0") // liquibase-kotlin implementation("io.github.momosetkn:liquibase-kotlin-starter-compiled:4.32.0-0.9.2") }
dependencies { // liquibase implementation("org.liquibase:liquibase-core:4.32.0") // liquibase-kotlin implementation("io.github.momosetkn:liquibase-kotlin-starter-script:4.32.0-0.9.2") }

Let's configure

liquibase-kotlin is provide client for kotlin.

momosetkn.liquibase.client.configureLiquibase methods is can global liquibase parameter. If you want confirm each parameter. can confirm by official-document.

import momosetkn.liquibase.client.LiquibaseDatabaseFactory import momosetkn.liquibase.client.configureLiquibase fun main() { configureLiquibase { global { general { showBanner = false } } } // set your database val database = LiquibaseDatabaseFactory.create( driver = "com.mysql.cj.jdbc.Driver", url = "jdbc:mysql://localhost:3306/test_db", username = "root", password = "", ) // set changelog and database val client = momosetkn.liquibase.client.LiquibaseClient( changeLogFile = /* your changeLog file path here */, database = database, ) // execute migrate client.update() }

Create changeLog

Create an extended momosetkn.liquibase.kotlin.parser.KotlinCompiledDatabaseChangeLog class. and add any changeSet.

// src/main/kotlin/example/DatabaseChangelog20241007Employee1.kt package example import momosetkn.liquibase.kotlin.parser.KotlinCompiledDatabaseChangeLog class DatabaseChangelog20241007Employee1 : KotlinCompiledDatabaseChangeLog({ changeSet(author = "your_name", id = "20241007-2000-1") { createTable(tableName = "employee") { column(name = "id", type = "UUID") { constraints(nullable = false, primaryKey = true) } column(name = "company_id", type = "UUID") { constraints(nullable = false) } column(name = "name", type = "VARCHAR(256)") column(name = "not_null_name", type = "VARCHAR(256)") { constraints(nullable = false) } column(name = "not_null_name2", type = "VARCHAR(256)") { constraints(nullable = false) } } } })

specify class-name as changeLogFile to LiquibaseClient.

val client = momosetkn.liquibase.client.LiquibaseClient( // set class-name changeLogFile = example.DatabaseChangelog20241007Employee1::class.qualifiedName!!, database = database, ) client.update()

Put kts-file under the resource directory.

// src/main/resource/db/changelog/db.changelog-20241007-employee-1.kts databaseChangeLog { changeSet(author = "your_name", id = "20241007-2000-1") { createTable(tableName = "employee") { column(name = "id", type = "UUID") { constraints(nullable = false, primaryKey = true) } column(name = "company_id", type = "UUID") { constraints(nullable = false) } column(name = "name", type = "VARCHAR(256)") column(name = "not_null_name", type = "VARCHAR(256)") { constraints(nullable = false) } column(name = "not_null_name2", type = "VARCHAR(256)") { constraints(nullable = false) } } } }

specify file-path as changeLogFile to LiquibaseClient.

val client = momosetkn.liquibase.client.LiquibaseClient( // set a file-path in the resource directory. changeLogFile = "db/changelog/db.changelog-20241007-employee-1.kts", database = database, ) client.update()

Execute multiple changeLog with includeAll

// example) src/main/kotlin/example/DatabaseChangelogAll.kt import momosetkn.liquibase.kotlin.parser.KotlinCompiledDatabaseChangeLog class DatabaseChangelogAll : KotlinCompiledDatabaseChangeLog({ // specify the name of the package that contains a changeLog class. includeAll("example.migration") })

Put kts-file under the resource directory.

// example) src/main/resource/db/changelog/db.changelog-20241007-employee-1.kts databaseChangeLog { // specify migration files directory includeAll("db/changelog/main") }

example project

liquibase-kotlin-example

Last modified: 14 October 2024