Table of Contents
I tried to create Kotlin database migration library.
I released version 0.0.4 at GitHub. It is not fully developed yet.
You can find it also at Gradle Plugin page.
How to use
build.gradle
It requires JDBC driver, and kotlin-script-util
library.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
buildscript { repositories { ... maven { url 'https://jitpack.io' } } dependencies { classpath group: 'org.jetbrains.kotlin', name: 'kotlin-script-util', version: '1.2.41' classpath 'org.postgresql:postgresql:9.4.1212.jre6' classpath 'com.github.KenjiOhtsuka:harmonica:0.0.4' } } apply plugin: 'harmonica' |
Directory Structure and Configuration File
It requires src/main/kotlin/db
and its subdirectories, config
and migration
. It will be able to be changed in later release.
1 2 3 4 5 6 7 8 |
`- src `- main `- kotlin `- db |- config | |- default.kts <- default configuration file | `- develop.kts <- you can create own configuration file `- migration |
And it needs DB configuration.
1 2 3 4 5 6 7 8 9 10 |
import com.improve_future.harmonica.core.DbConfig import com.improve_future.harmonica.core.Dbms DbConfig.create { dbms = Dbms.PostgreSQL dbName = "harmonica_test" host = "127.0.0.1" user = "developer" password = "developer" } |
It must not include package
description, because it is executed as Kotlin script.
Create Migration
Execute harmonicaCreate task.
1 |
./gradlew harmonicaCreate |
If you specify migration name, execute as follows.
1 |
./gradlew harmonicaCreate -PmigrationName=CreateUserTable |
Then, migration file will be created into the migration directory.
1 2 3 4 5 6 7 8 9 10 11 |
import com.improve_future.harmonica.core.AbstractMigration object : AbstractMigration() { override fun up() { createTable("sample_table") { integer("column_1") varchar("column_2") bool("column_3") } } } |
Migrate
1 |
./gradlew harmonicaUp |