【发布时间】:2016-09-18 15:05:12
【问题描述】:
Play 文档说“内存数据库中的 H2 非常便于开发,因为当游戏重新启动时,你的进化是从头开始运行的。”这就是我想要的。这是我到目前为止所做的(播放 2.5.6):
-
我创建了一个测试配置文件以使用带有进化的 H2,如下:
play.evolutions { db.default.enabled = true autoApply = true } db { default { driver = org.h2.Driver url = "jdbc:h2:mem:test;MODE=MYSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1" username = sa password = "" pool = "bonecp" // otherwise you can't log your sql... bonecp.logStatements=true } } -
我添加了一个简单的进化文件“/conf/evolutions/default/1.sql”:
# --- !Ups CREATE TABLE PROJECTS ( id int(11) NOT NULL ) ; # --- !Downs DROP TABLE IF EXISTS PROJECTS; -
我的控制器需要一个数据库。这是应该注入测试数据库而不是 MySQL 的地方。它有一个读取表格的测试操作:
class DataManagementController @Inject()(db: Database) extends Controller { def test() = Action { db.withConnection { conn => val st = conn.createStatement() val res = st.executeQuery("SELECT * FROM PROJECTS") while (res.next()) { println(res.getInt("id")) } } Ok("") } } -
我对该操作的第一个测试如下:
class ControllerSpec extends PlaySpec with OneAppPerSuite { val TestDb = Databases.inMemory("default") val dataCtrl = new DataManagementController(TestDb) "DataManagementController" should { "test" in { dataCtrl.test().apply(FakeRequest()) } } } -
当我运行测试时,我看到进化被应用于 H2 数据库,但在运行查询时,尽管有
DB_CLOSE_DELAY=-1选项,但所有内容都被删除了 (?):~ test-only ControllerSpec [info] ControllerSpec: [debug] c.j.b.BoneCPDataSource - JDBC URL = jdbc:h2:mem:test;MODE=MYSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1, Username = sa, partitions = 1, max (per partition) = 30, min (per partition) = 5, idle max age = 10 min, idle test period = 1 min, strategy = DEFAULT [debug] c.j.b.StatementHandle - select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%' [error] o.j.StatementLogger - java.sql.Statement.executeQuery: select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%'; throws exception: org.h2.jdbc.JdbcSQLException: Table "play_evolutions" not found; SQL statement: select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%' [42102-192] [...] [debug] c.j.b.StatementHandle - create table play_evolutions ( [...] [debug] c.j.b.StatementHandle - CREATE TABLE PROJECTS ( id int(11) NOT NULL AUTO_INCREMENT, person_id int(11) NOT NULL, name varchar(255) NOT NULL, PRIMARY KEY (id), ) [debug] c.j.b.StatementHandle - update play_evolutions set state = 'applied' where id = 1 [debug] c.j.b.StatementHandle - select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%' [info] DataManagementController [info] application - Creating Pool for datasource 'default' [info] - should test *** FAILED *** [info] org.h2.jdbc.JdbcSQLException: Table "PROJECTS" not found; [...]如果我使用
h2-browser(url: "jdbc:h2:mem:test" + args) 探索数据库,它是空的。但是如果我现在修改“1.sql”,我会得到 p>Database 'default' 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.]但它没有说明如何解决问题(因为我在浏览器中看到一个空数据库)。
对于设置测试数据库的任何帮助,我将不胜感激。我在这里看到很多帖子都问过类似的问题,要么没有答案,要么没有被接受,有几种不同的方法,我确实已经尝试了好几天。注:它会在 StackOverflow Docs(和 Play docs btw)中成为一个有用的部分。
【问题讨论】:
-
也可以看看tour.acolyte.eu.org
标签: scala playframework-2.0 h2