LoginSignup
0
0

More than 3 years have passed since last update.

PlayFrameworkのevolutionsをテストに適用しようとしたらテーブルが作成されずハマった件

Last updated at Posted at 2020-08-16

事の発端

PlayFrameworkを使っていて、DBアクセスの部分のテストを書こうと思ったとき、既にevolutions自体は使っていたのでテストコード用のスキーマを同じように作りたいなぁ、と思って、
https://www.playframework.com/documentation/ja/2.4.x/ScalaTestingWithDatabases
を参考に、
conf/evolutions/hogedb
にevolutionsファイルを設置、以下のようなテストコードを実行すれば、evolutionsが動いてテーブルができるはず!と思ったけれど何も起きない・・・

HogeSpec.scala
// テストコード
class HogeSpec extends PlaySpec with HogeTestHelper {
  withHogeDatabase { database =>
    Evolutions.applyEvolutions(database)
  }
}
HogeTestHelper.scala
// テストコードで使うヘルパー
trait HogeTestHelper {
  def withHogeDatabase[T](block: Database => T) = {
    Databases.withDatabase(
      driver = "com.mysql.cj.jdbc.Driver",
      url = "jdbc:mysql://hogehoge:3306/hoge_test?allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=UTF8",
      name = "hogedb",
      config = Map(
        "user" -> "hoge",
        "password" -> "hoge"
      )
    )(block)
  }
}

解決方法は見つけたけれど・・・

IntelliJでコードを書いていて、ふとevolutionsファイルを設置したフォルダを右クリックしたら、
Rebuild evolutions.hogedb
なるものがメニューにあったので、クリックした後にテストを実行したら、狙い通りにテーブルができました!
はいいのだけれど、これがIntelliJ固有の問題なのか、他に解決策があるのかよくわからずです。

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