在Play Framework 2.3中,如何配置和应用演进到内存数据库?

时间:2015-03-29 02:55:27

标签: playframework playframework-2.3 specs2 slick-2.0 play-slick

使用我的Play 2.3.8 / Slick 2.1应用程序,我试图弄清楚如何在我的application.conf(MySQL和MySQL模式下的内存数据库)中设置多个数据库,其中一个是自动用于测试。我想我接近了,但它给了我这个错误:

 InconsistentDatabase: : Database 'test' is in an inconsistent state![An evolution has not been applied properly. Please check the problem and resolve it manually before marking it as resolved.]  (Evolutions.scala:196)

我看到很多相关的问题/文档,但它已经过时了,或者与我尝试做的完全不符。

这是我在application.conf中的内容

db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://localhost/maindb"
db.default.user="admin"
db.default.password="test"

applyEvolutions.default=true

db.test.driver=org.h2.Driver
db.test.url="jdbc:h2:mem:play;MODE=MYSQL"
db.test.user=sa
db.test.password=""

applyEvolutions.test=true

slick.default= "models.database.*"
slick.test= "models.database.*"

我意识到,为了实现这一点,我可能需要将我的进化脚本复制到conf / evolutions / test目录中,所以我就这样做了。我也想知道我是否可以对其进行符号链接,以避免需要保留两份副本。

这是我的Spec2测试之一:

import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.test._
import play.api.test.Helpers._
import org.specs2.execute.{Result, AsResult}
import play.api.db.slick._
import play.api.db.slick.Config.driver.simple._
import models.database.OwnersDatabase._

abstract class WithDbData extends WithApplication(FakeApplication(additionalConfiguration = inMemoryDatabase("test"))) {
  override def around[T: AsResult](callback: => T): Result = super.around {
    prepareDbWithData()
    callback
  }

  def prepareDbWithData() = DB("test").withSession {
    implicit session => owners ++= TestData.inputData
  }
}

@RunWith(classOf[JUnitRunner])
class OwnerControllerSpec extends Specification {

  "OwnerController" should {    

    "on owners return list of owners" in new WithDbData {
      val result = route(FakeRequest(GET, "/owners")).get

      status(result) must equalTo(OK)
      contentType(result) must beSome("application/json")
      contentAsJson(result) mustEqual TestData.expectedOwnersAll
    }

  }
}

这里是用于填充表格的TestData类:

import models.Owner
import play.api.libs.json._

object TestData {
  val inputData = Seq(
    Owner("sam", "password", Some("Sam"), "test@example.com", None, "90210"),
    Owner("herb", "password", Some("Herb"), "test@example.com", None, "90210")
  )

  val expectedOwnersAll = Json.obj(
    "rows" -> JsArray(Seq(
      Json.obj("username" -> "sam", "password" -> "password", "name" -> "Sam", "email" -> "test@example.com", "pic_id" -> "", "zipcode" -> "90210"),
      Json.obj("username" -> "herb", "password" -> "password", "name" -> "Herb", "email" -> "test@example.com", "pic_id" -> "", "zipcode" -> "90210")
    ))
  )
}

出了什么问题?我做了什么被认为是不好的做法吗?

0 个答案:

没有答案