Table of Contents
Here, I announce the new release of Kotlin db migration library, Harmonica 1.0.1.
You can see the code in GitHub.
New Feature
- Execute migration after compilation
-
Before, it reads migration script as Kotlin script and execute it. This time, I added new Gradle tasks,
jarmonicaCreate
,jarmonicaUp
,jarmonicaDown
. Those tasks compile Kotlin code as an application, first, and execute the migration.In new tasks, db connection is handled better than before commands.
Of course you can use old commands,
harmonicaUp
,harmonicaDown
,harmonicaCreate
.
How to use
build.gradle
To use new commands, build.gradle
must be changed than before.
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 54 |
buildscript { ext.kotlin_version = '1.2.41' repositories { jcenter() mavenCentral() maven { url 'https://jitpack.io' } // required } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.github.KenjiOhtsuka:harmonica:1.0.1' // required } } group 'yourgroup' version 'yourversion' apply plugin: 'kotlin' apply plugin: 'jarmonica' // enable new commands // specify the directory to put migration classes extensions.extraProperties["directoryPath"] = "src/main/kotlin/db" // specify the package the migration classes have. extensions.extraProperties["migrationPackage"] = "db" sourceCompatibility = 1.8 repositories { mavenCentral() maven { url 'https://jitpack.io' } // required maven { url "https://dl.bintray.com/kotlin/exposed" // required } } dependencies { compile 'org.jetbrains.exposed:exposed:0.10.2' // if you use Exposed compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" compile group: 'org.reflections', name: 'reflections', version: '0.9.11' // required compile files('/lib/postgresql-42.2.2.jar') // JDBC driver compile 'com.github.KenjiOhtsuka:harmonica:1.0.1' // required /* required */ 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 } compileKotlin { kotlinOptions { jvmTarget = "1.8" } } compileTestKotlin { kotlinOptions { jvmTarget = "1.8" } } |
- Plugin preparation
- Add dependency in build script dependency and apply the plugin
jarmonica
- Dependency for compilation
- Add some library to dependencies for compilation. If you want to use Exposed, add it, too.
Directory Structure
Set up directory structure. This part is the same as before.
1 2 3 4 5 6 7 |
`- src `- main `- kotlin `- db |- config | `- Default.kt <- default configuration file `- migration |
Database Configuration
Put the class Default
.
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" }) |
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 see the new migration file as follows.
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 and Down
Now you can execute migration.
Up
1 |
./gradlew jarmonicaUp |
Down
1 |
./gradlew jarmonicaDown |
Warning
I checked the library with PostgreSQL. Not with MySQL.