Kotlin Database Migration Tool

License: GPL v3 KenjiOhtsuka by harmonica CircleCI Twitter Follow

Gradle Plugin Page: https://plugins.gradle.org/plugin/com.improve_future.harmonica

This is Database Migration Tool, gradle plugin, made with Kotlin. It is made similar to Phinx, Rails.

Now, this tool is for PostgreSQL, MySQL and SQLite.

Overview

With this library, you can write database migration like as follows.

package your.package.migration

import com.improve_future.harmonica.core.AbstractMigration

/**
 * HolloWorld
 */
class M20180624011127699_HolloWorld : AbstractMigration() {
    override fun up() {
        createTable("table_name") {
            // If you add the next line,
            // migration doesn't create auto incremental id column.
            // id = false

            // You can easily define columns with their type name.
            boolean("boolean_column", nullable = false, default = true)
            integer("integer_column", default = 1)
            decimal("decimal_column", 5, 2, default = 3)
            varchar("varchar_column", size = 10, nullable = false)
            text("text_column", default = "default value")
            blob("blob_column", default = "abcde".toByteArray())
            date("date_column_1", default = "2019-01-01")
            date("date_column_2", default = Date())
            date("date_column_3", default = LocalDate.of(2018, 2, 2))
            time("time_column_1", default = "11:22:33")
            time("time_column_2", default = Date())
            time("time_column_3", default = LocalTime.now(), nullable = false)
            dateTime("date_tiem_column_1", default = "2011-11-12 12:34:56")
            dateTime("date_time_column_2", default = Date())
            dateTime("date_time_column_3", default = LocalDateTime.now())
            timestamp("timestamp_column_1", default = "2012-10-04 1:2:3")
            timestamp("timestamp_column_2", default = Date())
            timestamp("timestamp_column_3", default = LocalDateTime.now())
        }

        // When you add column, `add*****Column` method works.
        addBooleanColumn(
            "table_name", "added_boolean",
            default = true, nullable = false
        )
        addIntegerColumn(
            "table_name", "added_integer", nullable = false
        )
        addDecimalColumn("table_name" "added_decimal")
        addVarcharColumn(
            "table_name", "added_boolean",
            default = "default", nullable = false
        )
        addTextColumn("table_name", "added_text")
        addDateColumn(
            "table_name", "added_date",
            default = LocalDate.of(2018, 12, 11)
        )

        // When you add index, use `addIndex`.
        createIndex("table_name", "column_name")

        // You can execute SQL directly.
        executeSql("SELECT 1;")
    }

    override fun down() {
        dropIndex("table_name", "table_name_integer_column_idx")
        dropTable("table_name")
    }
}

Usage, Command

Please look at Wiki.

Caution

When you use harmonica, not jarmonica, sometimes it says “The connection has already been closed” and the migrations fail. Then, execute gradlew --stop to clear dead connections.

Contribute

Pull requests are welcomed!! Please feel free to use it, and to contribute.

Test Project

Test with real database is in different repository, Harmonica Test.

compile "com.github.KenjiOhtsuka:harmonica:1.1.10"

Related Libraries

kuery

Last updated 3 mins ago

rxkotlin-jdbc

Last updated 3 mins ago

sqldelight

Last updated 3 mins ago

kotliquery

Last updated 3 mins ago

tekniq

Last updated 3 mins ago