简体   繁体   English

Slick 3会话与回滚

[英]Slick 3 session with rollback

I am using slick 3 and I am trying to perform some integration tests with some inserts, some code that uses the db and then I want to rollback all the insert or deletion at the end of the test itself but I cannot find any documentation about it. 我正在使用光滑3并且我正在尝试使用一些插入执行一些集成测试,一些使用db的代码然后我想在测试结束时回滚所有插入或删除但我找不到任何关于它的文档。

Is it possible? 可能吗? How can I achieve it? 我怎样才能实现它?

You need to use . transactionally 你需要使用. transactionally . transactionally around the DBIOAction . transactionally围绕DBIOAction

eg 例如

val a = (for {
  ns <- coffees.filter(_.name.startsWith("ESPRESSO")).map(_.name).result
  _ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally

val f: Future[Unit] = db.run(a)

For more see http://slick.typesafe.com/doc/3.1.1/dbio.html#transactions-and-pinned-sessions 有关更多信息,请参阅http://slick.typesafe.com/doc/3.1.1/dbio.html#transactions-and-pinned-sessions

I can advice to drop and create table schema before and after test using BeforeAndAfter scala-test trait with next code: 我可以建议在测试之前和之后使用BeforeAndAfter scala-test trait和下一个代码来删除和创建表模式:

def createTable(): Future[Unit] = {
        db.run(DBIO.seq(
          MTable.getTables.map(tables =>
            if (!tables.exists(_.name.name == table.baseTableRow.tableName))
              db.run(table.schema.create)
          )
        ))
}

def dropTable(): Future[Unit] = db.run(table.schema.drop)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM