0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ktor+FlywayでherokuのDBをmigrationする

Posted at

背景

Springを使っている場合、適切にファイルを配置すればFlywayのmigration scriptが自動で実行されるようです。
https://devcenter.heroku.com/articles/running-database-migrations-for-java-apps#running-flyway-automatically-with-spring

そうでない場合は、自動では実行されず、自前で設定する必要があるようです。
migrationを実行するスクリプトを用意して、Procfileに実行コマンドを追加すればOKです。

環境

  • heroku20
    • JVMは16 (system.propertiesで設定)
  • flyway 8.5.11
  • kotlin 1.6.21

サンプルの構成

src/
 ├ main/
 │ ├ kotlin/
 │ │  └ migration/
 │ │    └ script.kt
 │ └ resources/
 │    └ db/
 │      └ migration/
 │        └ V1__Initial.sql
 └ test/

手順

migrationを実行するスクリプトを作る

JDBC_DATABASE_URL などは、DBが作成してあれば、herokuが自動で挿入してくれます。

script.kt
package migration

import org.flywaydb.core.Flyway

fun main() {
  val flyway =
      Flyway.configure()
          .dataSource(
              System.getenv("JDBC_DATABASE_URL"),
              System.getenv("JDBC_DATABASE_USERNAME"),
              System.getenv("JDBC_DATABASE_PASSWORD"))
          .load()
  flyway.migrate()
}

Procfileにmigrationコマンドを追加

  • ${プロジェクト名}は適宜入れ替えてください
  • 最後の引数は、mainからのパス.クラス名
Procfile
release: java -cp .:build/classes/kotlin/main:build/install/${プロジェクト名}/lib/*:build/resources/main migration.ScriptKt

デプロイして確認

デプロイ

git add .
git commit -m "add migration script"
git push heroku

psqlを実行して確認。

heroku pg:psql <database name> --app <app name>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?