Table of Contents
Here, I announce the new release of Harmonica, Kotlin database migration library. The version is 1.0.10.
In this article, gradlew
command is written in Unix style but this tool can also be used in Windows OS. Of course I checked in Windows.
New Main Feature
- It can be used for MySQL, too.
-
Before, it is not completely tested for MySQL. But now, it was tested and it can be used.
- Decimal column is intoroduced.
-
Before, it can’t handle decimal column completely. Now we can use decimal column with precision and scale.
addColumn
function.-
addColumn
function is introduced. - Seze of VARCHAR can be set.
-
Before, it couldn’t be set.
step
parameter is introduced.-
Before,
jarmonicaUp
executes all remain migrations andjarmonicaDown
reverts only one migration. But now,step
parameter can be used as follows and control migration more flexibly.12./gradlew jarmonicaUp -Pstep=2./gradlew jarmonicaDown -Pstep=2 NOT NULL
constraint can be added.-
NOT NULL
constraint can be added as follows.12345678override fun up() {createTable("not_null_name") {integer("integer_column", nullable = false)varchar("varchar_column", nullable = false)decimal("decimal_column", nullable = false)text("text_column", nullable = false)}} DEFAULT
keyword can be used.-
DEFAULT
keyword can be used as follows.12345678override fun up() {createTable("default_table") {integer("integer_column", default = 1)varchar("varchar_column", default = "text")decimal("decimal_column", default = 1.1)text("text_column", default = "text")}} - Fix migration package in the migration jarmonicaCreate creates.
-
Before, jarmonicaCreate creates migration file without package description and it can’t be executed by jarmonicaUp as it is.. Now, jarmonicaCreate insert package description into new migration and jarmonicaUp can use its package.
And there are some bug fixes.
How to Use
I will explain required configuration. Harmonica Test can be other example.
Set up build.gradle
Set up build.gradle
as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
buildscript { ext.kotlin_version = '1.2.51' ext.harmonica_version = '1.0.10' repositories { jcenter() mavenCentral() maven { url 'https://jitpack.io' } } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // required for plugin classpath "com.github.KenjiOhtsuka:harmonica:$harmonica_version" } } group 'com.improve_future' version ext.harmonica_version apply plugin: 'kotlin' // required for gradle commands apply plugin: 'jarmonica' // Default value is src/main/kotlin/db extensions.extraProperties["directoryPath"] = "src/main/kotlin/com/improve_future/harmonica_test/jarmonica" // Default value is db extensions.extraProperties["migrationPackage"] = "com.improve_future.harmonica_test.jarmonica" sourceCompatibility = 1.8 repositories { mavenCentral() // required maven { url 'https://jitpack.io' } } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin _version" // required compile group: 'org.reflections', name: 'reflections', version: '0.9.11' compile group: 'org.jetbrains.kotlin', name: 'kotlin-script-runtime', version: kotlin_version compile group: 'org.jetbrains.kotlin', name: 'kotlin-script-util', version: kotlin_version compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: kotlin_version // JDBC Driver (MySQL or PostgreSQL), prepare for your DBMS compile files('/lib/postgresql-42.2.2.jar') compile files('/lib/mysql-connector-java-8.0.11.jar') //required compile "com.github.KenjiOhtsuka:harmonica:$harmonica_version" } |
Some package such as “org.reflections.reflections
” are required. In addition, you can use Exposed if you want.
Directory Structure
Set up directory structure.
1 2 3 4 5 6 7 8 |
`- src `- main `- kotlin `- db |- config | |- Default.kt <- default configuration file | `- OtherEnvironment.kt < you can use other environment `- migration |
Configuration File
Default.kt
and other environment configuration file must be as follows.
1 2 3 4 5 6 7 8 9 10 11 12 |
package db.config import com.improve_future.harmonica.core.DbConfig import com.improve_future.harmonica.core.Dbms class Default: DbConfig({ dbms = Dbms.PostgreSQL user = "developer" password = "developer" host = "127.0.0.1" dbName = "harmonica_test" }) |
If you want to use other database configuration file, use env
parameter with Harmonica command (-Penv=OtherEnvironment
).
Create Migration
You can create new migration with the following command.
1 |
./gradlew jarmonicaCreate |
If you want to specify the migration name, use migrationName property.
1 |
./gradlew jarmonicaCreate -PmigrationName=HelloWorld |
Then you can get the following file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package db.migration import com.improve_future.harmonica.core.AbstractMigration /** * HolloWorld */ class M20180624011127699_HolloWorld : AbstractMigration() { override fun up() { createTable("table_name") { integer("column_1") varchar("column_2") } } override fun down() { dropTable("table_name") } } |
Migrate Up or Down
Up
Migrate up with the command.
1 |
./gradlew jarmonicaUp |
As default, all migration which are not executed will be executed. If you want to specify the number of migrations to be executed, use step
parameter.
1 |
./gradlew jarmonicaUp -Pstep=2 |
Down
If you want to revert migration, migrate down with the command.
1 |
./gradlew jarmonicaUp |
As default, only one migration will be reverted. If you want to specify the number of migrations to be reverted, use step
parameter.
1 |
./gradlew jarmonicaUp -Pstep=2 |